README
hast-util-shift-heading
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 import
ed instead of require
d.
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
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
hast-util-heading
— check if a node is a heading elementhast-util-heading-rank
— get the rank (or depth, level) of headings
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.