# BaseHub

Publish BaseHub content as docs.

[BaseHub](https://basehub.com) needs no Vuesho package. Its SDK reads the items on the server, with their rich text as Markdown, which [markdown-it](https://github.com/markdown-it/markdown-it) renders.

## Setup [#setup]

### Prepare the Repository [#prepare-the-repository-step]

Create a collection with a rich text field in BaseHub, or fork the [Fumadocs template](https://basehub.com/fuma-nama/fumadocs-template/explore), whose `documentation` collection has a `richText` field.

### Install the Packages [#install-the-packages-step]

```npm
npm i basehub markdown-it
```

### Store the Token [#store-the-token-step]

Put the repository token, from the Developers tab in BaseHub, in `.env`:

```ini title=".env"
BASEHUB_TOKEN=bshb_...
```

`npx basehub` writes your repository's types to `basehub-types.d.ts`. Run it again after the schema changes, or run `npx basehub dev` next to `vuesho dev` to keep the types current.

### Query the Collection [#query-the-collection-step]

Write a function that lists the items as pages. Each page keeps its Markdown under a symbol key, so the browser doesn't receive it with the page data:

```ts title="src/server/basehub.ts"
import { basehub } from "basehub";

export const body = Symbol("body");

export async function listPages() {
  const { documentation } = await basehub().query({
    // [!code highlight:3]
    documentation: {
      items: { _slug: true, _title: true, richText: { markdown: true } },
    },
  });
  return documentation.items.map((item) => ({
    type: "page" as const,
    path: `${item._slug}.md`,
    data: { title: item._title, [body]: item.richText?.markdown ?? "" }, // [!code highlight]
  }));
}
```

`basehub()` reads `BASEHUB_TOKEN` from the environment, and reads the drafts under `vuesho dev`.

### Connect the Source [#connect-the-source-step]

Pass `listPages` as a dynamic source's `files`, and render its pages with a plugin. Create `md` and `render()` in a module, as in [Rendering Markdown](/content-hooks#rendering-markdown):

```ts title="vuesho.config.ts"
import { createSite, recommendedPreset } from "vuesho/config";
import { extractStructuredData, buildToc } from "vuesho/markdown";
import { md, render } from "./src/server/markdown";
import { body, listPages } from "./src/server/basehub";

export default createSite({ files: listPages, staleTime: 60_000 }).plugins(
  recommendedPreset(),
  // [!code highlight:6]
  {
    name: "site:basehub",
    getPageBody: async (page) => ({ html: await render(page.data[body]) }),
    getPageToc: (page) => buildToc(md.parse(page.data[body], {})),
    getPageStructuredData: (page) => extractStructuredData(md.parse(page.data[body], {})),
    getPageText: (page) => page.data[body],
  },
);
```

To use the BaseHub pages next to the Markdown in `content/`, give their source a `baseDir`, as in [Multiple Collections](/markdown/collections#multiple-collections):

```ts title="vuesho.config.ts"
createSite({
  docs: docs.toSource(),
  basehub: { baseDir: "basehub", files: listPages, staleTime: 60_000 }, // [!code highlight]
});
```

## Pages [#pages]

An item's slug sets its URL. A slug like `guides/install` makes a folder in the sidebar, and the slug `index` is its folder's index page.

- A query returns at most 500 items, so page through a larger collection with `skip` in `__args`.
- BaseHub [leaves out](https://docs.basehub.com/nextjs-integration/rendering-rich-text) custom component blocks from the Markdown.
- HTML in the Markdown renders as text.
- With an image optimizer, add `assets.basehub.com` to its `remotePatterns`, as [Remote Images](/markdown/images#remote-images) shows, or the images fail to load.

## Refreshing Content [#refreshing-content]

The server reads the items again once `staleTime` passes, or when a webhook calls `revalidateLoader()`, as in [Dynamic Rendering](/dynamic-rendering#refreshing-content). `authenticateWebhook()` from `basehub/workflows` checks the request's signature:

```ts title="vuesho.config.ts"
import { authenticateWebhook } from "basehub/workflows"; // [!code ++]

createSite({ files: listPages, staleTime: 60_000 }).plugins({
  name: "site:basehub",
  getPageBody: (page) => ({ html: md.render(page.data[body]) }),
  // The other hooks, as above
  // [!code ++:17]
  routes({ createApi }) {
    createApi({
      path: "/api/revalidate",
      POST: async (request) => {
        const secret = process.env.BASEHUB_WEBHOOK_SECRET as `bshb_workflow_${string}:${string}`;
        const result = await authenticateWebhook({
          body: request.body,
          signature: request.headers,
          secret,
        });
        if (!result.success) return new Response(null, { status: 401 });
        await this.revalidateLoader();
        return new Response();
      },
    });
  },
});
```

In BaseHub, add a Workflow block that runs on each commit and calls `https://docs.example.com/api/revalidate`. Put its signing secret in `BASEHUB_WEBHOOK_SECRET`.

A static build queries the collection once and prerenders every page.
