What is mdast-util-to-hast?
The mdast-util-to-hast package is a utility that allows for the transformation of MDAST (Markdown Abstract Syntax Tree) to HAST (Hypertext Abstract Syntax Tree). This is particularly useful for applications that need to convert markdown content into HTML or other formats that can be more easily manipulated or displayed in web environments.
What are mdast-util-to-hast's main functionalities?
Convert MDAST to HAST
This feature allows for the conversion of a markdown document represented as an MDAST into a HAST. The code sample demonstrates how to use unified with the remark-parse plugin to parse markdown into MDAST, and then convert that MDAST into HAST using mdast-util-to-hast.
const unified = require('unified')
const markdown = require('remark-parse')
const toHAST = require('mdast-util-to-hast')
unified()
.use(markdown)
.use(() => tree => toHAST(tree))
.process('# Hello world!', function (err, file) {
if (err) throw err
console.log(file)
})
Other packages similar to mdast-util-to-hast
remark-html
remark-html is a plugin for the remark processor that compiles markdown to HTML. It is similar to mdast-util-to-hast in that it also deals with the conversion of markdown content to a more web-friendly format. However, remark-html is more of an end-to-end solution for converting markdown directly to HTML, whereas mdast-util-to-hast provides a lower-level conversion to HAST, which can then be further manipulated or converted to HTML.
rehype
rehype is a processor powered by plugins part of the unified ecosystem that manipulates HTML documents. It is similar to mdast-util-to-hast in the sense that both deal with HAST nodes. However, rehype operates directly on HTML content or HAST, focusing on the manipulation and processing of HTML, whereas mdast-util-to-hast is specifically designed for converting MDAST to HAST.
mdast-util-to-hast
Transform MDAST to HAST.
Note You probably want to use remark-rehype.
Installation
npm:
npm install mdast-util-to-hast
Usage
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)
API
toHAST(node[, options])
Transform the given MDAST tree to a HAST tree.
options.allowDangerousHTML
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
.
options.commonmark
Set to true
(default: false
) to prefer the first when duplicate definitions
are found. The default behaviour is to prefer the last duplicate definition.
options.handlers
Object mapping MDAST nodes to functions
handling those elements.
Take a look at lib/handlers/
for examples.
Returns
HASTNode
.
Note
yaml
and html
nodes are ignoredposition
s are properly patched- Unknown nodes with
children
are transformed to div
elements - Unknown nodes with
value
are transformed to text
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
Related
License
MIT © Titus Wormer