README
mdast-util-toc
mdast utility to generate table of contents.
Install
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install mdast-util-toc
Use
Dependencies:
/** @typedef {import('mdast').Root} Root */
import {u} from 'unist-builder'
import {toc} from 'mdast-util-toc'
Now running:
const tree = /** @type {Root} */ (
u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])
)
const table = toc(tree)
Yields:
{
index: null,
endIndex: null,
map: {
type: 'list',
ordered: false,
spread: true,
children: [ { type: 'listItem', spread: true, children: [Array] } ]
}
}
API
This package exports the following identifiers: toc
.
There is no default export.
toc(tree[, options])
Generate a Table of Contents from a tree.
Looks for the first heading matching options.heading
(case insensitive),
and returns a table of contents (list) for all following headings.
If no heading
is specified, creates a table of contents for all headings in
tree
.
tree
is not changed.
Links to headings are based on GitHub’s style.
Only top-level headings (those not in blockquotes or lists), are used.
This default behavior can be changed by passing parents
.
options
options.heading
Heading to look for (string
), wrapped in new RegExp('^(' + value + ')