6.0.8 • Published 7 months ago

remark-auto-toc v6.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

remark-auto-toc

ci

npm version npm downloads

A remark plugin for converting toc metadata into MDX exports

Table of Contents

Installation

This package depends on the AST output by remark-toc

npm install remark-toc remark-auto-toc

Usage

This remark plugin takes toc content, and outputs it as JavaScript exports. Both YAML and TOML toc data are supported.

For example, given a file named example.mdx with the following contents:

---
hello: toc
---

Rest of document

The following script:

import { readFile } from 'node:fs/promises'

import { compile } from '@mdx-js/mdx'
import remarkMdxToc from 'remark-auto-toc'

const { value } = await compile(await readFile('example.mdx'), {
  jsx: true,
  remarkPlugins: [remarkMdxToc]
})
console.log(value)

Roughly yields:

export const toc = {
  hello: 'toc'
}

export default function MDXContent() {
  return <p>Rest of document</p>
}

API

The default export is a remark plugin.

Options

  • name: The identifier name of the variable the toc data is assigned to. (Default: toc).
  • parsers: A mapping A mapping of node types to parsers. Each key represents a toc node type. The value is a function that accepts the toc data as a string, and returns the parsed data. By default yaml nodes will be parsed using yaml and toml nodes using toml.

Compatibility

This project is compatible with Node.js 18 or greater.

License

MIT © Mikaleb