{"library":"mdast-util-toc","title":"mdast-util-toc: Markdown Table of Contents Utility","description":"mdast-util-toc is a utility within the unified (specifically mdast) ecosystem for programmatically generating a table of contents from a markdown abstract syntax tree (AST). It provides a `toc` function that processes an `mdast` tree, identifying headings and constructing a new `mdast` list node representing the table of contents. The package is currently stable at version 7.1.0, with minor and patch releases occurring periodically, and major versions introducing breaking changes like ESM-only support or Node.js version bumps. Key differentiators include its tight integration with the `mdast` AST format, allowing for flexible programmatic manipulation, and its robust options for controlling the TOC generation, such as specifying heading depth (`minDepth`, `maxDepth`), skipping specific headings, and defining parent node types. It's often used indirectly via `remark-toc` for simpler integration into `remark` pipelines, which handles the injection of the generated TOC back into the document. Its focus is purely on AST transformation, making it highly composable.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install mdast-util-toc"],"cli":null},"imports":["import { toc } from 'mdast-util-toc'","import type { Options, Result } from 'mdast-util-toc'","import {toc} from 'https://esm.sh/mdast-util-toc@7'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import {toc} from 'mdast-util-toc';\n\n/** @type {import('mdast').Root} */\nconst tree = {\n  type: 'root',\n  children: [\n    {type: 'heading', depth: 1, children: [{type: 'text', value: 'Alpha'}]},\n    {type: 'heading', depth: 2, children: [{type: 'text', value: 'Bravo'}]},\n    {type: 'heading', depth: 3, children: [{type: 'text', value: 'Charlie'}]},\n    {type: 'heading', depth: 2, children: [{type: 'text', value: 'Delta'}]},\n    {type: 'paragraph', children: [{type: 'text', value: 'Some content here.'}]},\n    {type: 'heading', depth: 1, children: [{type: 'text', value: 'Gamma'}]}\n  ]\n};\n\n// Generate a table of contents for the entire tree\nconst table = toc(tree, { maxDepth: 2 });\n\nconsole.dir(table, {depth: 3});\n\n/* Expected Output (simplified):\n{\n  index: undefined,\n  endIndex: undefined,\n  map: {\n    type: 'list',\n    ordered: false,\n    spread: true,\n    children: [\n      { type: 'listItem', spread: true, children: [ [Object] ] }, // Alpha\n      { type: 'listItem', spread: true, children: [ [Object], [Object] ] } // Gamma\n    ]\n  }\n}\n*/","lang":"typescript","description":"This example demonstrates how to import the `toc` function and use it to generate a table of contents from a simple mdast syntax tree, configured to include headings up to depth 2.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}