unified

Project: syntax-tree/hast-util-shift-heading

Package: hast-util-shift-heading@3.0.0

  1. Dependents: 0
  2. hast utility to change heading rank (or depth, level)
  1. util 137
  2. utility 133
  3. unist 120
  4. html 112
  5. hast 61
  6. hast-util 40
  7. heading 26
  8. depth 5
  9. level 5
  10. change 4
  11. rank 2

hast-util-shift-heading

Build Coverage Downloads Size Sponsors Backers Chat

hast utility to change the rank (depth, level) of headings.

Install

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

npm:

npm install hast-util-shift-heading

Use

import {h} from 'hastscript'
import {shiftHeading} from 'hast-util-shift-heading'

const tree = h('main', [
  h('h1', 'Alpha'),
  h('p', 'Bravo'),
  h('h2', 'Charlie'),
  h('p', 'Delta'),
  h('h5', 'Echo'),
  h('p', 'Foxtrot'),
  h('h6', 'Golf')
])

shiftHeading(tree, -1)

console.log(tree)

Yields:

{ type: 'element',
  tagName: 'main',
  properties: {},
  children:
   [ { type: 'element',
       tagName: 'h1',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'p',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'h1',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'p',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'h4',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'p',
       properties: {},
       children: [Array] },
     { type: 'element',
       tagName: 'h5',
       properties: {},
       children: [Array] } ] }

API

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

shiftHeading(tree, shift)

Change the rank of all headings (h1 to h6) in tree. Mutates the tree. Caps the rank so that shifting would not create invalid headings (such as h0 or h7).

Parameters
Returns

tree (Node) — The given, mutated, tree.

Throws

Error — When shift is not a valid non-null finite integer.

Security

hast-util-shift-heading changes the syntax tree but can only generated headings from h1 through h6. 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 © Titus Wormer