Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

utility to generate a table of contents from an mdast tree

License

NotificationsYou must be signed in to change notification settings

syntax-tree/mdast-util-toc

BuildCoverageDownloadsSizeSponsorsBackersChat

mdast utility to generate a table of contents.

Contents

What is this?

This package is a utility that generates a table of contents from a document.

When should I use this?

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.

Install

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>

Use

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]}]}}

API

This package exports the identifiertoc.There is no default export.

toc(tree[, options])

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.

Parameters
  • tree (Node)— tree to search and generate from
  • options (Options, optional)— configuration
Returns

Results (Result).

Options

Configuration (TypeScript type).

Fields
  • 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 ofcontents
  • parents (Test, default:tree)— allow headings to be children of certain node types.Can by anyunist-util-is compatible test
  • tight (boolean, default:false)— whether to compile list items tightly
  • ordered (boolean, default:false)— whether to compile list items as an ordered list, otherwise they areunordered
  • prefix (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.

Result

Results (TypeScript type).

Fields
  • index (number orundefined)— index of the node right after the table of contents heading,-1 if noheading was found,undefined if noheading was given
  • endIndex (number orundefined)— index of the first node afterheading that is not part of its section,-1 if no heading was found,undefined if noheading was given, same asindex if there are no nodes betweenheading and the first heading inthe table of contents
  • map (List orundefined)— list representing the generated table of contents,undefined if notable of contents could be created, either because no heading was found orbecause no following headings were found

Types

This package is fully typed withTypeScript.It exports the typesOptions andResult.

Compatibility

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.

Security

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.

Related

Contribute

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.

License

MIT ©Jonathan Haines

About

utility to generate a table of contents from an mdast tree

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Contributors9


[8]ページ先頭

©2009-2025 Movatter.jp