---
title: "MarkdownToc"
description: "The headings of a Markdown or MDX body, with their anchor ids, added as `_toc` by `markdown.toc`."
canonical: "https://www.qino.works/docs/api/types/markdown-toc"
---

# MarkdownToc

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

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.

```ts title="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

| Value              | Lists                                                             |
| ------------------ | ----------------------------------------------------------------- |
| `false`            | Nothing, and adds no `_toc`. The default                          |
| `true`             | Every heading, `h1` to `h6`, with `rehype-slug` ids               |
| `{ upTo?, slug? }` | Headings down to `upTo`, inclusive, with ids from `slug` when set |

| Option | Type                       | Default | Description                                                         |
| ------ | -------------------------- | ------- | ------------------------------------------------------------------- |
| `upTo` | `"h1" … "h6"`              | `"h6"`  | The deepest heading level to list                                   |
| `slug` | `(text: string) => string` |         | Turns a heading's `text` into its id. See [Custom ids](#custom-ids) |

## Type

```ts title="@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

| Field     | Description                                                                                               |
| --------- | --------------------------------------------------------------------------------------------------------- |
| `heading` | The heading level: `2` for `##`                                                                           |
| `id`      | The anchor id: `{#id}` when set, else from `slug`, else the one `rehype-slug` gives                       |
| `text`    | The heading's plain text, with inline code, emphasis, and links kept as text, and MDX components left out |

## 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:

```md
## 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:

```ts title="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`:

```ts title="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;
      }, []),
    }),
  }),
}),
```
