unified

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

Package: hast-util-shift-heading@2.0.0

  1. Dependencies: 1·Dependents: 0
  2. hast utility to change heading rank (or depth, level)
  1. util 115
  2. utility 113
  3. html 108
  4. unist 106
  5. hast 59
  6. hast-util 40
  7. heading 23
  8. depth 4
  9. level 4
  10. change 3
  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

npm:

npm install hast-util-shift-heading

Use

var h = require('hastscript')
var shift = require('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')
])

shift(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

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