toc

import { toc } from "jsr:@ende/markdown";

function

function toc(ast: Root, options: TocOptions = [UNSUPPORTED]): TocEntry[]

Lists the headings of a document for a table of contents: each heading's level, plain text and id, in document order. The list is flat; use depth to indent or nest it. Headings inside block quotes, lists and admonitions count too.

The slug is the heading's id, so it matches the id attribute render() writes on the heading (unless the tree has duplicate ids, which parse() never makes).

ast: Root

The root of the syntax tree, from parse().

options: TocOptions = [UNSUPPORTED]

Which heading levels to include.

Returns

The headings, in document order.

Example: Usage

import { parse } from "@ende/markdown/parse";
import { toc } from "@ende/markdown/toc";
import { assertEquals } from "@ende/assert";

const ast = parse("# Guide\n\n## Install\n\n### From *source*\n\n## Install");
assertEquals(toc(ast, { maxDepth: 2 }), [
  { depth: 1, text: "Guide", slug: "guide" },
  { depth: 2, text: "Install", slug: "install" },
  { depth: 2, text: "Install", slug: "install-1" }
]);
assertEquals(toc(ast, { minDepth: 3 }), [{ depth: 3, text: "From source", slug: "from-source" }]);

Source