mdast-util-heading-range
Markdown heading as ranges in mdast.
Installation
npm:
npm install mdast-util-heading-range
mdast-util-heading-range is also available for bower, component, and duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.
Table of Contents
Usage
var heading = require('mdast-util-heading-range');
var mdast = require('mdast');
Callback invoked when a heading is found.
function onrun(start, nodes, end) {
return [
start,
{
'type': 'paragraph',
'children': [
{
'type': 'text',
'value': 'Qux.'
}
]
},
end
];
}
Process a document.
var doc = mdast().use(heading('foo', onrun)).process(
'# Foo\n' +
'\n' +
'Bar.\n' +
'\n' +
'# Baz\n'
);
Yields:
# Foo
Qux.
# Baz
API
heading(test, onrun)
Transform part of a document without affecting other parts, by changing a
section: a heading which passes test
, until the next heading of the same
or lower depth, or the end of the document.
Parameters
-
test
(string
,RegExp
,function(string, Node): boolean
) — Heading to look for:-
When
string
, wrapped innew RegExp('^(' + value + ')$', 'i')
; -
Then, when
RegExp
, wrapped infunction (value) {expression.test(value)}
.
-
-
onrun
(Array.<Node>? = function (start, nodes, end)
) — Callback invoked when a range is found.
Returns
Function
— Should be passed to mdast.use()
.
function onrun(start, nodes, end?, scope)
Parameters
-
start
(Heading
) — Start of range; -
nodes
(Array.<Node>
) — Nodes betweenstart
andend
; -
end
(Heading?
) — End of range, if any. -
scope
(Object
):parent
(Node
) — Parent of the range;start
(number
) — Index ofstart
inparent
;end
(number?
) — Index ofend
inparent
.
Returns
Array.<Node>?
— Zero or more nodes to replace the range (including
start
, and end
) with.