Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

hast-util-shift-heading

2.0.1 • Public • Published

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
  • 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

Install

npm i hast-util-shift-heading

DownloadsWeekly Downloads

17

Version

2.0.1

License

MIT

Unpacked Size

8.16 kB

Total Files

4

Last publish

Collaborators

  • avatar
  • avatar