Fumadocs rebuilt on Hamur, the original is fumadocs.dev
Skip to Content
Fumadocs

Linear

Publish Linear documents with Fumadocs.

@fumadocs/linear loads Linear documents as a content source, and renders their Markdown on the server.

Setup

npm install @fumadocs/linear @linear/sdk

API Key

Create a personal API key in the API settings of Linear, and add it to your environment:

.env.local
LINEAR_API_KEY=lin_api_...

The key can read every document its user can access, while the site publishes only the documents you select in the next step.

Content Source

Create the source with a filter of the documents to publish, and get its loader from getSource():

lib/source.ts
import { LinearClient } from '@linear/sdk';
import { dynamicLoader } from 'fumadocs-core/source';
import { createLinear } from '@fumadocs/linear';

export const linear = createLinear({
  client: new LinearClient({ apiKey: process.env.LINEAR_API_KEY }),
});

const source = dynamicLoader(
  linear.dynamicSource({
    // from the project URL: linear.app/acme/project/docs-1a2b3c4d5e6f
    filter: { project: { slugId: { eq: '1a2b3c4d5e6f' } } },
  }),
  { baseUrl: '/docs' },
);

export async function getSource() {
  return source.get();
}

The pages of a document are published with it, so a filter by ID publishes one document and its pages:

linear.dynamicSource({
  // from the document URL: linear.app/acme/document/handbook-9f8e7d6c5b4a
  filter: { slugId: { eq: '9f8e7d6c5b4a' } },
});

Rendering Pages

page.data.load() compiles the Markdown of a page. Render it with your MDX components:

import { getSource } from '@/lib/source';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';

export default async function Layout({ children }: LayoutProps<'/docs'>) {
  const source = await getSource();

  return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}

File Route

Linear serves uploaded files, like images, to signed-in users only. Pages point them to a route that redirects to a signed URL:

app/api/linear/file/route.ts
import { createLinearFileHandler } from '@fumadocs/linear';
import { getSource, linear } from '@/lib/source';

export const GET = createLinearFileHandler(linear, getSource);

The route serves only the files of published pages. To serve it at another path, set fileRoute:

lib/source.ts
export const linear = createLinear({
  client: new LinearClient({ apiKey: process.env.LINEAR_API_KEY }),
  fileRoute: '/linear/file',
});

Page Tree

A document with pages becomes a folder, and the sidebar follows the order of documents in Linear. URLs are generated from document titles, so renaming a document changes its URL. To keep URLs stable, generate them from the IDs of documents:

lib/source.ts
const source = dynamicLoader(
  linear.dynamicSource({
    filter: { project: { slugId: { eq: '1a2b3c4d5e6f' } } },
  }),
  {
    baseUrl: '/docs',
    slugs: (file) => [file.data.linear.slugId],
  },
);

Markdown

Pages support the Markdown of Linear, including tables and code blocks. Collapsible sections render as accordions, and the sections in a row form one group. To render them differently, pass Accordions and Accordion components to render().

A link to a published document points to its page, and a mention of the document shows its title.

createLinear() accepts remarkPlugins and rehypePlugins to customize the compilation, and options of syntax highlighting:

lib/source.ts
export const linear = createLinear({
  client: new LinearClient({ apiKey: process.env.LINEAR_API_KEY }),
  // or `false` to disable highlighting
  rehypeCodeOptions: {
    themes: { light: 'github-light', dark: 'github-dark' },
  },
});

Revalidation

The loader keeps the documents in memory. Revalidate it after editing Linear, for example from a webhook of document changes:

lib/source.ts
export function revalidateLinear() {
  return source.revalidate();
}

To fetch the latest content on every request, like for previews, invalidate it before reading:

lib/source.ts
export async function getSource() {
  source.invalidate(); 
  return source.get();
}

Limitations

  • The file route needs a server. On a static export, files uploaded to Linear don't load.
  • Mentions of issues, projects and users link to Linear, where only members of the workspace can open them.

How is this guide?

Last updated on