Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
mdast-util-toc
Advanced tools
The `mdast-util-toc` package is a utility for generating a Table of Contents (TOC) from Markdown Abstract Syntax Trees (MDAST). It is particularly useful for processing Markdown documents and extracting a structured TOC based on headings.
Generate TOC from MDAST
This feature allows you to generate a Table of Contents from a Markdown Abstract Syntax Tree. The code sample demonstrates how to convert a Markdown string into an MDAST, generate a TOC from it, and then convert the TOC back into a Markdown string.
const { toMarkdown } = require('mdast-util-to-markdown');
const { fromMarkdown } = require('mdast-util-from-markdown');
const { toc } = require('mdast-util-toc');
const markdown = '# Title\n## Subtitle\n### Sub-subtitle';
const tree = fromMarkdown(markdown);
const tocNode = toc(tree);
console.log(toMarkdown(tocNode.map));
Custom Heading Levels
This feature allows you to customize the depth of headings included in the TOC. The code sample demonstrates generating a TOC that includes only headings up to level 2.
const { fromMarkdown } = require('mdast-util-from-markdown');
const { toc } = require('mdast-util-toc');
const markdown = '# Title\n## Subtitle\n### Sub-subtitle';
const tree = fromMarkdown(markdown);
const tocNode = toc(tree, { maxDepth: 2 });
console.log(tocNode.map);
Custom TOC Heading
This feature allows you to specify a custom heading for the TOC. The code sample demonstrates generating a TOC with a custom heading 'Table of Contents'.
const { fromMarkdown } = require('mdast-util-from-markdown');
const { toc } = require('mdast-util-toc');
const markdown = '# Title\n## Subtitle\n### Sub-subtitle';
const tree = fromMarkdown(markdown);
const tocNode = toc(tree, { heading: 'Table of Contents' });
console.log(tocNode.map);
The `remark-toc` package is a plugin for the `remark` Markdown processor that generates a Table of Contents. It is similar to `mdast-util-toc` but is designed to be used directly within the `remark` ecosystem, making it easier to integrate if you are already using `remark`.
The `markdown-toc` package generates a Table of Contents for Markdown files. Unlike `mdast-util-toc`, which works with MDAST, `markdown-toc` operates directly on Markdown strings, making it simpler to use for straightforward TOC generation without needing to parse the Markdown into an abstract syntax tree.
The `markdown-it-toc-done-right` package is a plugin for the `markdown-it` Markdown parser that generates a Table of Contents. It is similar to `mdast-util-toc` in functionality but is designed to work within the `markdown-it` ecosystem, providing seamless integration if you are using `markdown-it` for Markdown processing.
FAQs
mdast utility to generate a table of contents from a tree
The npm package mdast-util-toc receives a total of 264,718 weekly downloads. As such, mdast-util-toc popularity was classified as popular.
We found that mdast-util-toc demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.