Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
mdast-util-split-by-heading
Advanced tools
Split MDAST into subtrees relying on the header hierarchy.
mdast-util-split-by-heading splits a markdown AST into several markdown ASTs based on their headings.
It is useful when you want to split a document with many headings into several documents, for instance one by chapter.
npm:
npm install mdast-util-split-by-heading
const unified = require('unified')
const parse = require('remark-parse')
const split = require('mdast-util-split-by-heading')
var tree = unified()
.use(parse)
.parse('# part\n\n## chapter \n\n Hello world \n\n # part *2*')
console.log(split(tree))
split(node, options = { splitDepth: 1 })
Splits a MDAST tree into separate trees by heading depth.
options.splitDepth = 1
An integer greater or equal to 1 determining the heading depth you want to match when splitting.
import dedent from 'dedent'
import unified from 'unified'
import reParse from 'remark-parse'
import split from 'mdast-util-split-by-heading'
const doSplit = (text, options) => {
const { splitDepth = 1 } = options
return split(
unified().use(reParse).parse(text),
{ splitDepth: splitDepth }
)
}
const text = dedent `
a global introduction
# hello
a paragraph
> a quote to *ensure this is parsed*
## a sub title
other paragraph
# conclusion title
paragraph
`
doSplit(text)
/*
{
"introduction": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "a global introduction",
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 22,
"offset": 21
},
"indent": []
}
}
],
"position": {
"start": {
"line": 1,
"column": 1,
"offset": 0
},
"end": {
"line": 1,
"column": 22,
"offset": 21
},
"indent": []
}
}
]
},
"trees": [
{
"title": {
"type": "root",
"children": {
"type": "heading",
"depth": 1,
"children": [
{
"type": "text",
"value": "hello",
"position": {
"start": {
"line": 3,
"column": 3,
"offset": 25
},
"end": {
"line": 3,
"column": 8,
"offset": 30
},
"indent": []
}
}
],
"position": {
"start": {
"line": 3,
"column": 1,
"offset": 23
},
"end": {
"line": 3,
"column": 8,
"offset": 30
},
"indent": []
}
}
},
"children": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "a paragraph",
"position": {
"start": {
"line": 5,
"column": 1,
"offset": 32
},
"end": {
"line": 5,
"column": 12,
"offset": 43
},
"indent": []
}
}
],
"position": {
"start": {
"line": 5,
"column": 1,
"offset": 32
},
"end": {
"line": 5,
"column": 12,
"offset": 43
},
"indent": []
}
},
{
"type": "blockquote",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "a quote to ",
"position": {
"start": {
"line": 7,
"column": 3,
"offset": 47
},
"end": {
"line": 7,
"column": 14,
"offset": 58
},
"indent": []
}
},
{
"type": "emphasis",
"children": [
{
"type": "text",
"value": "ensure this is parsed",
"position": {
"start": {
"line": 7,
"column": 15,
"offset": 59
},
"end": {
"line": 7,
"column": 36,
"offset": 80
},
"indent": []
}
}
],
"position": {
"start": {
"line": 7,
"column": 14,
"offset": 58
},
"end": {
"line": 7,
"column": 37,
"offset": 81
},
"indent": []
}
}
],
"position": {
"start": {
"line": 7,
"column": 3,
"offset": 47
},
"end": {
"line": 7,
"column": 37,
"offset": 81
},
"indent": []
}
}
],
"position": {
"start": {
"line": 7,
"column": 1,
"offset": 45
},
"end": {
"line": 7,
"column": 37,
"offset": 81
},
"indent": []
}
},
{
"type": "heading",
"depth": 2,
"children": [
{
"type": "text",
"value": "a sub title",
"position": {
"start": {
"line": 9,
"column": 4,
"offset": 86
},
"end": {
"line": 9,
"column": 15,
"offset": 97
},
"indent": []
}
}
],
"position": {
"start": {
"line": 9,
"column": 1,
"offset": 83
},
"end": {
"line": 9,
"column": 15,
"offset": 97
},
"indent": []
}
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "other paragraph",
"position": {
"start": {
"line": 11,
"column": 1,
"offset": 99
},
"end": {
"line": 11,
"column": 16,
"offset": 114
},
"indent": []
}
}
],
"position": {
"start": {
"line": 11,
"column": 1,
"offset": 99
},
"end": {
"line": 11,
"column": 16,
"offset": 114
},
"indent": []
}
}
]
}
},
{
"title": {
"type": "root",
"children": {
"type": "heading",
"depth": 1,
"children": [
{
"type": "text",
"value": "conclusion title",
"position": {
"start": {
"line": 13,
"column": 3,
"offset": 118
},
"end": {
"line": 13,
"column": 19,
"offset": 134
},
"indent": []
}
}
],
"position": {
"start": {
"line": 13,
"column": 1,
"offset": 116
},
"end": {
"line": 13,
"column": 19,
"offset": 134
},
"indent": []
}
}
},
"children": {
"type": "root",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "paragraph",
"position": {
"start": {
"line": 15,
"column": 1,
"offset": 136
},
"end": {
"line": 15,
"column": 10,
"offset": 145
},
"indent": []
}
}
],
"position": {
"start": {
"line": 15,
"column": 1,
"offset": 136
},
"end": {
"line": 15,
"column": 10,
"offset": 145
},
"indent": []
}
}
]
}
}
]
}
*/
FAQs
Split MDAST into subtrees relying on the header hierarchy.
We found that mdast-util-split-by-heading demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.