Skip to content

Since 0.19.0

MarkdownToc

The headings of a Markdown or MDX body, with their anchor ids, added as `_toc` by `markdown.toc`.

View Markdown
On this page

Set markdown.toc on a Markdown or MDX collection, tree, or item, and every entry gets a _toc field: its headings in document order. Related entries get it too when their primitive sets markdown.toc. JSON primitives can't set it.

qino/collections/docs.ts
export const docsCollection = qino.defineCollection({
  directory: "docs",
  extension: ".mdx",
  schema: DocSchema,
  markdown: { toc: { upTo: "h3" } },
});

const doc = await docsCollection.getEntry("install");
doc._toc; // [{ heading: 2, id: "requirements", text: "Requirements" }, …]

Option

Type

@qino/cms
import type { MarkdownToc, MarkdownTocHeading } from "@qino/cms";

type MarkdownTocHeading = {
  heading: 1 | 2 | 3 | 4 | 5 | 6;
  id: string;
  text: string;
};

type MarkdownToc = Array<MarkdownTocHeading>;

Fields

Anchor ids

Ids are slugged with github-slugger, like rehype-slug does, so #${id} links to the heading when you render the body with rehype-slug. Every heading is slugged in order, including those upTo leaves out, so duplicate suffixes such as -1 match too.

.mdx bodies are parsed as MDX, and .md or .markdown bodies as Markdown, so {name} is an expression in one and text in the other. Both id and text come from what the heading renders as text: MDX expressions, raw HTML, and image alts are left out, and footnote references become their number. text also leaves out MDX components, so ## `watch`: <TypeText>() => T</TypeText> reads watch:, while the id still counts them, as rehype-slug does. Headings in code blocks are skipped. Headings inside blockquotes, lists, MDX components, or used footnotes are listed, since rehype-slug gives them ids as well. Empty headings are left out.

The Markdown parsers are only loaded when a primitive sets markdown.toc.

Custom ids

A heading ending in {#id} keeps that id, and {#id} is left out of text. This is the syntax of remark-custom-heading-id, which sets the same id on the rendered heading:

## Installation {#install}

If your site gives headings ids its own way, pass that function as slug. It receives each heading's text, and duplicates get a -1, -2 suffix, counting every id before them. Only listed headings count, as a renderer that names just those would: a heading deeper than upTo doesn't take an id, and its {#id} doesn't count toward duplicates:

qino/collections/docs.ts
markdown: {
  toc: {
    upTo: "h4",
    // "Register fields" → "Registerfields"
    slug: (text) => text.replace(/[^a-zA-Z0-9]/g, ""),
  },
},

Nesting

_toc is flat, which suits an "On this page" sidebar or a select: indent each item by heading. For a nested list, group the headings in a view's augment:

qino/collections/docs.ts
import type { MarkdownToc, MarkdownTocHeading } from "@qino/cms";

type Section = MarkdownTocHeading & { children: MarkdownToc };

views: (view) => ({
  default: view({
    augment: (doc) => ({
      sections: doc._toc.reduce<Array<Section>>((sections, item) => {
        if (item.heading == 2) sections.push({ ...item, children: [] });
        else sections.at(-1)?.children.push(item);
        return sections;
      }, []),
    }),
  }),
}),