hast-util-shift-heading

hast utility to change heading rank (or depth, level)

Usage no npm install needed!

<script type="module">
  import hastUtilShiftHeading from 'https://cdn.skypack.dev/hast-util-shift-heading';
</script>

README

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'

var 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
  • tree (Node) — Tree to walk
  • shift (number) — Non-null finite integer to use to shift ranks
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.

Related

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