README
mdast-util-heading-range
mdast utility to use headings as ranges.
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 mdast-util-heading-range
Use
Say we have the following file, example.md
:
# Foo
Bar.
# Baz
And our script, example.js
, looks as follows:
import {readSync} from 'to-vfile'
import {remark} from 'remark'
import {headingRange} from 'mdast-util-heading-range'
const file = readSync('example.md')
remark()
.use(plugin)
.process(file)
.then((file) => {
console.log(String(file))
})
function plugin() {
return (tree) => {
headingRange(tree, 'foo', (start, nodes, end) => [
start,
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
end
])
}
}
Now, running node example
yields:
# Foo
Qux.
# Baz
API
This package exports the following identifiers: headingRange
.
There is no default export.
headingRange(tree, test|options, handler)
Search tree
(Node
) and transform a section without affecting other
parts with handler
(Function
).
A “section” is a heading that passes test
, until the next heading of the same
or lower depth, or the end of the document.
If ignoreFinalDefinitions: true
, final definitions “in” the section are
excluded.
options
options.test
Heading to look for (string
, RegExp
, Function
).
When string
, wrapped in new RegExp('^(' + value + ')