
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
mdast-util-heading-range
Advanced tools
mdast utility to use headings as ranges.
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
Say we have the following file, example.md
:
# Foo
Bar.
# Baz
And our script, example.js
, looks as follows:
import toVFile from 'to-vfile'
import remark from 'remark'
import {headingRange} from 'mdast-util-heading-range'
remark()
.use(plugin)
.process(vfile.readSync('example.md'), function(err, file) {
if (err) throw err
console.log(String(file))
})
function plugin() {
return transform
function transform(tree) {
headingRange(tree, 'foo', handler)
}
function handler(start, nodes, end) {
return [
start,
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
end
]
}
}
Now, running node example
yields:
# Foo
Qux.
# Baz
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 + ')$', 'i')
;
when RegExp
, wrapped in function (value) {expression.test(value)}
options.ignoreFinalDefinitions
Ignore final definitions otherwise in the section (boolean
, default: false
).
function test(value, node)
Function invoked for each heading with its content (string
) and node
itself (Heading
) to check if it’s the one to look for.
Boolean?
, true
if this is the heading to use.
function handler(start, nodes, end?, scope)
Callback invoked when a range is found.
start
Start of range (Heading
).
nodes
Nodes between start
and end
(Array.<Node>
).
end
End of range, if any (Node?
).
scope
Extra info (Object
):
parent
(Node
) — Parent of the rangestart
(number
) — Index of start
in parent
end
(number?
) — Index of end
in parent
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.
function handler(start, nodes, end) {
return [start, {type: 'html', value: 'alert(1)'}, end]
}
Yields:
# Foo
<script>alert(1)</script>
# Baz
Either do not use user input in handler
or use
hast-util-santize
.
mdast-zone
— comments as ranges or markers instead of headingsSee 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.
FAQs
mdast utility to use headings as ranges in mdast
The npm package mdast-util-heading-range receives a total of 7,000 weekly downloads. As such, mdast-util-heading-range popularity was classified as popular.
We found that mdast-util-heading-range demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.