Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork13
utility to generate a table of contents from an mdast tree
License
syntax-tree/mdast-util-toc
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
mdast utility to generate a table of contents.
This package is a utility that generates a table of contents from a document.
This utility is useful to generate a section so users can more easily navigatethrough a document.
This package is wrapped inremark-toc for ease of use withremark, where it also injects the table of contents into the document.
This package isESM only.In Node.js (version 16+), install withnpm:
npm install mdast-util-toc
In Deno withesm.sh:
import{toc}from'https://esm.sh/mdast-util-toc@7'
In browsers withesm.sh:
<scripttype="module">import{toc}from'https://esm.sh/mdast-util-toc@7?bundle'</script>
import{toc}from'mdast-util-toc'/**@type {import('mdast').Root} */consttree={type:'root',children:[{type:'heading',depth:1,children:[{type:'text',value:'Alpha'}]},{type:'heading',depth:2,children:[{type:'text',value:'Bravo'}]},{type:'heading',depth:3,children:[{type:'text',value:'Charlie'}]},{type:'heading',depth:2,children:[{type:'text',value:'Delta'}]}]}consttable=toc(tree)console.dir(table,{depth:3})
Yields:
{index:undefined,endIndex:undefined,map:{type:'list',ordered:false,spread:true,children:[{type:'listItem',spread:true,children:[Array]}]}}
This package exports the identifiertoc.There is no default export.
Generate a table of contents fromtree.
Looks for the first heading matchingoptions.heading (case insensitive) andreturns a table of contents (a list) for all following headings.If noheading is specified, creates a table of contents for all headings intree.tree is not changed.
Links in the list 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 passingoptions.parents.
Results (Result).
Configuration (TypeScript type).
heading(string, optional)— heading to look for, wrapped innew RegExp('^(' + value + ')$', 'i')maxDepth(number, default:6)— maximum heading depth to include in the table of contents.This is inclusive: when set to3, level three headings are included(those with three hashes,###)minDepth(number, default:1)— minimum heading depth to include in the table of contents.This is inclusive: when set to3, level three headings are included(those with three hashes,###)skip(string, optional)— headings to skip, wrapped innew RegExp('^(' + value + ')$', 'i').Any heading matching this expression will not be present in the table ofcontentsparents(Test, default:tree)— allow headings to be children of certain node types.Can by anyunist-util-iscompatible testtight(boolean, default:false)— whether to compile list items tightlyordered(boolean, default:false)— whether to compile list items as an ordered list, otherwise they areunorderedprefix(string, optional)— add a prefix to links to headings in the table of contents.Useful for example when later going from mdast to hast and sanitizing withhast-util-sanitize.
Results (TypeScript type).
index(numberorundefined)— index of the node right after the table of contents heading,-1if noheading was found,undefinedif noheadingwas givenendIndex(numberorundefined)— index of the first node afterheadingthat is not part of its section,-1if no heading was found,undefinedif noheadingwas given, same asindexif there are no nodes betweenheadingand the first heading inthe table of contentsmap(Listorundefined)— list representing the generated table of contents,undefinedif notable of contents could be created, either because no heading was found orbecause no following headings were found
This package is fully typed withTypeScript.It exports the typesOptions andResult.
Projects maintained by the unified collective are compatible with maintainedversions of Node.js.
When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,mdast-util-toc@^7,compatible with Node.js 16.
Use ofmdast-util-toc does not involvehast, user content, or change thetree, so there are no openings forcross-site scripting (XSS) attacks.
Injectingmap into the syntax tree may open you up to XSS attacks as existingnodes are copied into the table of contents.The following example shows how an existing script is copied into the table ofcontents.
For the following Markdown:
#Alpha##Bravo<script>alert(1)</script>##Charlie
Yields inmap:
-[Alpha](#alpha)-[Bravo<script>alert(1)</script>](#bravoscriptalert1script)-[Charlie](#charlie)
Always usehast-util-santize when transforming tohast.
github-slugger— generate a slug just like GitHub doesunist-util-visit— visit nodesunist-util-visit-parents— likevisit, but with a stack of parents
Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.
This project has acode of conduct.By interacting with this repository, organization, or community you agree toabide by its terms.
About
utility to generate a table of contents from an mdast tree
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors9
Uh oh!
There was an error while loading.Please reload this page.