Note You probably want to use remark-rehype.
npm:
npm install mdast-util-to-hast
Say we have the following example.md
:
## Hello **World**!
...and next to it, example.js
:
var inspect = require('unist-util-inspect');
var unified = require('unified');
var parse = require('remark-parse');
var vfile = require('to-vfile');
var toHAST = require('mdast-util-to-hast');
var tree = unified().use(parse).parse(vfile.readSync('example.md'));
console.log(inspect(toHAST(tree)));
Which when running with node example
yields:
root[1] (1:1-2:1, 0-20)
└─ element[3] (1:1-1:20, 0-19) [tagName="h2"]
├─ text: "Hello " (1:4-1:10, 3-9)
├─ element[1] (1:10-1:19, 9-18) [tagName="strong"]
│ └─ text: "World" (1:12-1:17, 11-16)
└─ text: "!" (1:19-1:20, 18-19)
Transform the given MDAST tree to a HAST tree.
Whether to allow html
nodes and inject them as raw HTML (boolean
,
default: false
). Only do this when compiling later with
hast-util-to-html
.
Set to true
(default: false
) to prefer the first when duplicate definitions
are found. The default behaviour is to prefer the last duplicate definition.
Object mapping MDAST nodes to functions
handling those elements.
Take a look at lib/handlers/
for examples.
yaml
andhtml
nodes are ignoredposition
s are properly patched- Unknown nodes with
children
are transformed todiv
elements - Unknown nodes with
value
are transformed totext
nodes - If
node.data.hName
is set, it’s used as the HAST element’s tag-name - If
node.data.hProperties
is set, it’s mixed into the HAST element’s properties - If
node.data.hChildren
is set, it’s used as the element’s HAST children
mdast-util-to-nlcst
— Transform MDAST to NLCSThast-util-sanitize
— Sanitize HAST nodeshast-util-to-mdast
— Transform HAST to MDASTremark-rehype
— rehype support for remarkrehype-remark
— remark support for rehype