1.0.1 • Published 5 years ago
md-hierarchical-parser v1.0.1
Markdown Hierarchical Parser
MDAST well describes the syntax and structure of markdown, but it has a single problem - there are no hierarchical relations between headings. It is obvious the h2 following h1 is the children of h1, but MDAST treats them as the same heading and just gives depth info as an attribute.
This parser parses markdown and generates JSON structure with hierarchical info. The parser is implemented based on streamich/md-mdast.
Install
npm i md-hierarchical-parserExample
let mdParser = require('md-hierarchical-parser')
await mdParser.run("path/to/markdown.md", true, true)Description
run(path, isHierarchy = true, isString=false)Reads markdown file in the path and generates json object representing the structure.
path: denotes the path to the markdownisHierarchy: iftrue, returns JSON with hierarchy structure. Else, return vanilla MDAST. Default value=trueisString: if false, returns JSON object. Else, returns stringified value. Default value=false
Example
.md file:
## Header 2
paragraph 1
### Header 3
paragraph 2
#### Header 4
- first
- second
- third
paragraph 3
##### Header 5
`print("hello world")`
## Header 2will be converted to JSON which follows below structure:
├── heading
│ ├── paragraph
│ ├── heading
│ │ ├── paragraph
│ │ ├── heading
│ │ │ ├── list
│ │ │ ├── paragraph
│ │ │ ├── heading
│ │ │ │ ├── code
├── heading