mdast-util-split-by-heading
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.
Installation
npm:
npm install mdast-util-split-by-heading
Usage
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))
API
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.
Examples:
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)
License
MIT © Zeste de Savoir