unified

Project: syntax-tree/mdast-normalize-headings

Package: mdast-normalize-headings@3.1.0

  1. Dependents: 3
  2. mdast utility to make sure there is no more than a single top-level heading in the document
  1. util 144
  2. utility 140
  3. unist 128
  4. mdast 84
  5. tree 41
  6. mdast-util 31
  7. heading 27
  8. depth 5
  9. level 5
  10. title 4
  11. count 4
  12. header 2

mdast-normalize-headings

Build Coverage Downloads Size Sponsors Backers Chat

mdast utility to make sure that there is only one top-level heading in the document by adjusting headings depths accordingly.

Providing multiple top-level headings per single markdown document is confusing for tools that assume that there is only a single top-level heading that contains some meta-information (usually title) about the document.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install mdast-normalize-headings

Use

import {u} from 'unist-builder'
import {normalizeHeadings} from 'mdast-normalize-headings'

const tree = u('root', [
  u('heading', {depth: 1}, [u('text', 'title')]),
  u('heading', {depth: 2}, [u('text', 'description')]),
  u('heading', {depth: 1}, [u('text', 'example')])
])

console.log(tree)

normalizeHeadings(tree)

console.log(tree)

Yields:

{
  type: 'root',
  children: [
    {type: 'heading', depth: 1, children: [Array]},
    {type: 'heading', depth: 2, children: [Array]},
    {type: 'heading', depth: 1, children: [Array]}
  ]
}
{
  type: 'root',
  children: [
    {type: 'heading', depth: 1, children: [Array]},
    {type: 'heading', depth: 3, children: [Array]},
    {type: 'heading', depth: 2, children: [Array]}
  ]
}

API

This package exports the following identifiers: normalizeHeadings. There is no default export.

normalizeHeadings(tree)

Modifies tree in-place. Returns tree.

Security

Use of mdast-normalize-headings does not involve hast so there are no openings for cross-site scripting (XSS) attacks.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin