Gathering detailed insights and metrics for mdast-util-heading-range
Gathering detailed insights and metrics for mdast-util-heading-range
Gathering detailed insights and metrics for mdast-util-heading-range
Gathering detailed insights and metrics for mdast-util-heading-range
npm install mdast-util-heading-range
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
10 Stars
161 Commits
2 Forks
9 Watchers
1 Branches
12 Contributors
Updated on Apr 28, 2024
Latest Version
4.0.0
Package Id
mdast-util-heading-range@4.0.0
Unpacked Size
24.16 kB
Size
7.45 kB
File Count
7
NPM Version
9.7.2
Node Version
20.0.0
Published on
Jul 07, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
mdast utility to find headings and replace the content in their section.
This package is a utility that lets you find a certain heading, then takes the content in their section (from it to the next heading of the same or lower depth), and calls a given handler with the result, so that you can change or replace things.
This utility is typically useful when you have certain sections that can be
generated.
For example, this utility is used by remark-toc
to update the
above Contents
heading.
A similar package, mdast-zone
, does the same but uses comments
to mark the start and end of sections.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install mdast-util-heading-range
In Deno with esm.sh
:
1import {headingRange} from 'https://esm.sh/mdast-util-heading-range@4'
In browsers with esm.sh
:
1<script type="module"> 2 import {headingRange} from 'https://esm.sh/mdast-util-heading-range@4?bundle' 3</script>
Say we have the following file, example.md
:
1# Foo 2 3Bar. 4 5# Baz
…and a module example.js
:
1import {read} from 'to-vfile' 2import {remark} from 'remark' 3import {headingRange} from 'mdast-util-heading-range' 4 5const file = await remark() 6 .use(myPluginThatReplacesFoo) 7 .process(await read('example.md')) 8 9console.log(String(file)) 10 11/** @type {import('unified').Plugin<[], import('mdast').Root>} */ 12function myPluginThatReplacesFoo() { 13 return function (tree) { 14 headingRange(tree, 'foo', function (start, nodes, end) { 15 return [ 16 start, 17 {type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]}, 18 end 19 ] 20 }) 21 } 22}
…now running node example.js
yields:
1# Foo 2 3Qux. 4 5# Baz
This package exports the identifier headingRange
.
There is no default export.
headingRange(tree, test|options, handler)
Search tree
for a heading matching test
and change its “section” with
handler
.
A “section” ranges from the matched heading 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.
tree
(Node
)
— tree to changetest
(Test
)
— same as passing {test: Test}
options
(Options
)
— configurationhandler
(Handler
)
— handle a sectionNothing (undefined
).
Handler
Callback called when a section is found (TypeScript type).
start
(Heading
)
— start of section (a heading node matching test
)nodes
(Array<Node>
)
— nodes between start
and end
end
(Node
or undefined
)
— end of section, if anyinfo
(Info
)
— extra infoResults (Array<Node | null | undefined>
, optional).
If nothing is returned, nothing will be changed.
If an array of nodes (can include null
and undefined
) is returned, the
original section will be replaced by those nodes.
Info
Extra info (TypeScript type).
parent
(Node
)
— parent of the sectionstart
(number
)
— index of start
in parent
end
(number
or undefined
)
— index of end
in parent
Options
Configuration (TypeScript type).
test
(Test
)
— test for a headingignoreFinalDefinitions
(boolean
, default: false
)
— ignore final definitions otherwise in the sectionTest
Test for a heading (TypeScript type).
When string
, wrapped in new RegExp('^(' + value + ')$', 'i')
;
when RegExp
, wrapped in (value) => expression.test(value)
1export type Test = RegExp | TestFunction | string
TestFunction
Check if a node matches (TypeScript type).
value
(string
)
— plain-text headingnode
(Heading
)
— heading nodeWhether this is the heading that is searched for (boolean
, optional).
This package is fully typed with TypeScript.
This package exports the types Handler
,
Info
, Options
, Test
,
and TestFunction
.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
mdast-util-heading-range@^4
, compatible with Node.js 16.
Improper use of handler
can open you up to a cross-site scripting (XSS)
attack as the value it returns is injected into the syntax tree.
This can become a problem if the tree is later transformed to hast.
The following example shows how a script is injected that could run when loaded
in a browser.
1/** @type {import('mdast-util-heading-range').Handler} */ 2function handler(start, nodes, end) { 3 return [start, {type: 'html', value: '<script>alert(1)</script>'}, end] 4}
Yields:
1# Foo 2 3<script>alert(1)</script> 4 5# Baz
Either do not use user input in handler
or use
hast-util-santize
.
mdast-zone
— similar but uses comments to mark the start and end of sectionsSee 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, organisation, or community you agree to abide by its terms.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
Project has not signed or included provenance with any releases.
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More