This package is a utility that takes markdown input and turns it into an
mdast syntax tree.
This utility uses micromark,
which turns markdown into tokens,
and then turns those tokens into nodes.
This package is used inside remark-parse,
which focusses on
making it easier to transform content by abstracting these internals away.
When should I use this?
If you want to handle syntax trees manually, use this.
When you just want to turn markdown into HTML,
use micromark instead.
For an easier time processing content,
use the remark ecosystem instead.
<script type="module"> import {fromMarkdown} from 'https://esm.sh/mdast-util-from-markdown@2?bundle'</script>
Use
Say we have the following markdown file example.md:
## Hello, *World*!
…and our module example.js looks as follows:
import fs from 'node:fs/promises'import {fromMarkdown} from 'mdast-util-from-markdown'const doc = await fs.readFile('example.md')const tree = fromMarkdown(doc)console.log(tree)
…now running node example.js yields (positional info removed for brevity):
This package exports the identifier fromMarkdown.
There is no default export.
The export map supports the development condition.
Run node --conditions development example.js to get instrumented dev code.
Without this condition, production code is loaded.
Handles (enter, exit) create the tree;
a later handle for a token type from an extension overwrites earlier ones.
Listeners (afterExit, beforeEnter) observe all tokens including unhandled
ones.
The point of listeners is that they do not interfere with handling,
potentially changed by extensions.
Don’t modify unexpected things!
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
mdast-util-from-markdown@^2, compatible with Node.js 16.
Security
As markdown is sometimes used for HTML, and improper use of HTML can open you up
to a cross-site scripting (XSS) attack, use of mdast-util-from-markdown
can also be unsafe.
When going to HTML, use this utility in combination with
hast-util-sanitize to make the tree safe.