Transform hast (HTML) to mdast (markdown).
Note: You probably want to use rehype-remark.
npm:
npm install hast-util-to-mdast
Say we have the following example.html
:
<h2>Hello <strong>world!</strong></h2>
…and next to it, example.js
:
var unified = require('unified')
var parse = require('rehype-parse')
var stringify = require('remark-stringify')
var vfile = require('to-vfile')
var toMdast = require('hast-util-to-mdast')
var file = vfile.readSync('example.html')
var hast = unified()
.use(parse)
.parse(file)
var mdast = toMdast(hast)
var doc = unified()
.use(stringify)
.stringify(mdast)
console.log(doc)
Now, running node example.js
yields:
## Hello **world!**
Transform the given hast tree to mdast.
Object mapping tag-names to functions handling those elements.
Take a look at handlers/
for examples.
Whether the given tree is a complete document. If document: true
,
implicit paragraphs are added in the root
node around inline mdast nodes.
Otherwise, inline mdast nodes are wrapped when needed.
Whether to collapse to a newline (\n
) instead of a single space (default) if
a streak of white-space in a text node contains a newline.
The algorithm supports implicit and explicit paragraphs, such as:
<article>
An implicit sentence.
<h1>An explicit sentence.</h1>
</article>
Yields:
An implicit sentence.
# An explicit sentence.
Some nodes are ignored and their content will not be present in mdast.
To ignore custom elements, configure a handler for their tag-name or type that
returns nothing.
For example, to ignore em
elements, pass handlers: {'em': function () {}}
:
<p><strong>Importance</strong> and <em>emphasis</em>.</p>
Yields:
**Importance** and .
To ignore a specific element from HTML, set data-mdast
to ignore
:
<p><strong>Importance</strong> and <em data-mdast="ignore">emphasis</em>.</p>
Yields:
**Importance** and .
hast-util-to-nlcst
— Transform hast to nlcstmdast-util-to-hast
— Transform mdast to hastmdast-util-to-nlcst
— Transform mdast to nlcstremark-rehype
— rehype support for remarkrehype-remark
— remark support for rehype
See contributing.md
in syntax-tree/hast
for ways to get
started.
This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.