# mdast-util-to-hast [![Build][build-badge]][build] [![Coverage][coverage-badge]][coverage] [![Downloads][downloads-badge]][downloads] [![Size][size-badge]][size] [![Sponsors][sponsors-badge]][collective] [![Backers][backers-badge]][collective] [![Chat][chat-badge]][chat] [mdast][] utility to transform to [hast][]. ## Contents * [What is this?](#what-is-this) * [When should I use this?](#when-should-i-use-this) * [Install](#install) * [Use](#use) * [API](#api) * [`defaultFootnoteBackContent(referenceIndex, rereferenceIndex)`](#defaultfootnotebackcontentreferenceindex-rereferenceindex) * [`defaultFootnoteBackLabel(referenceIndex, rereferenceIndex)`](#defaultfootnotebacklabelreferenceindex-rereferenceindex) * [`defaultHandlers`](#defaulthandlers) * [`toHast(tree[, options])`](#tohasttree-options) * [`FootnoteBackContentTemplate`](#footnotebackcontenttemplate) * [`FootnoteBackLabelTemplate`](#footnotebacklabeltemplate) * [`Handler`](#handler) * [`Handlers`](#handlers) * [`Options`](#options) * [`Raw`](#raw) * [`State`](#state) * [Examples](#examples) * [Example: supporting HTML in markdown naïvely](#example-supporting-html-in-markdown-naïvely) * [Example: supporting HTML in markdown properly](#example-supporting-html-in-markdown-properly) * [Example: footnotes in languages other than English](#example-footnotes-in-languages-other-than-english) * [Example: supporting custom nodes](#example-supporting-custom-nodes) * [Algorithm](#algorithm) * [Default handling](#default-handling) * [Fields on nodes](#fields-on-nodes) * [CSS](#css) * [Syntax tree](#syntax-tree) * [Nodes](#nodes) * [Types](#types) * [Compatibility](#compatibility) * [Security](#security) * [Related](#related) * [Contribute](#contribute) * [License](#license) ## What is this? This package is a utility that takes an [mdast][] (markdown) syntax tree as input and turns it into a [hast][] (HTML) syntax tree. ## When should I use this? This project is useful when you want to deal with ASTs and turn markdown to HTML. The hast utility [`hast-util-to-mdast`][hast-util-to-mdast] does the inverse of this utility. It turns HTML into markdown. The remark plugin [`remark-rehype`][remark-rehype] wraps this utility to also turn markdown to HTML at a higher-level (easier) abstraction. ## Install This package is [ESM only][esm]. In Node.js (version 16+), install with [npm][]: ```sh npm install mdast-util-to-hast ``` In Deno with [`esm.sh`][esmsh]: ```js import {toHast} from 'https://esm.sh/mdast-util-to-hast@13' ``` In browsers with [`esm.sh`][esmsh]: ```html ``` ## Use Say we have the following `example.md`: ```markdown ## Hello **World**! ``` …and next to it a module `example.js`: ```js import {fs} from 'node:fs/promises' import {toHtml} from 'hast-util-to-html' import {fromMarkdown} from 'mdast-util-from-markdown' import {toHast} from 'mdast-util-to-hast' const markdown = String(await fs.readFile('example.md')) const mdast = fromMarkdown(markdown) const hast = toHast(mdast) const html = toHtml(hast) console.log(html) ``` …now running `node example.js` yields: ```html
It works!
``` > ⚠️ **Danger**: observe that the XSS attack through the `onerror` attribute > is still present. ### Example: supporting HTML in markdown properly If you do not trust the authors of the input markdown, or if you want to make sure that further utilities can see HTML embedded in markdown, use [`hast-util-raw`][hast-util-raw]. The following example passes `allowDangerousHtml` to this utility (`mdast-util-to-hast`), then turns the raw embedded HTML into proper HTML nodes (`hast-util-raw`), and finally sanitizes the HTML by only allowing safe things (`hast-util-sanitize`): ```js import {raw} from 'hast-util-raw' import {sanitize} from 'hast-util-sanitize' import {toHtml} from 'hast-util-to-html' import {fromMarkdown} from 'mdast-util-from-markdown' import {toHast} from 'mdast-util-to-hast' const markdown = 'It works! ' const mdast = fromMarkdown(markdown) const hast = raw(toHast(mdast, {allowDangerousHtml: true})) const safeHast = sanitize(hast) const html = toHtml(safeHast) console.log(html) ``` …now running `node example.js` yields: ```htmlIt works!
``` > 👉 **Note**: observe that the XSS attack through the `onerror` attribute > is no longer present. ### Example: footnotes in languages other than English If you know that the markdown is authored in a language other than English, and you’re using `micromark-extension-gfm` and `mdast-util-gfm` to match how GitHub renders markdown, and you know that footnotes are (or can?) be used, you should translate the labels associated with them. Let’s first set the stage: ```js import {toHtml} from 'hast-util-to-html' import {gfm} from 'micromark-extension-gfm' import {fromMarkdown} from 'mdast-util-from-markdown' import {gfmFromMarkdown} from 'mdast-util-gfm' import {toHast} from 'mdast-util-to-hast' const markdown = 'Bonjour[^1]\n\n[^1]: Monde!' const mdast = fromMarkdown(markdown, { extensions: [gfm()], mdastExtensions: [gfmFromMarkdown()] }) const hast = toHast(mdast) const html = toHtml(hast) console.log(html) ``` …now running `node example.js` yields: ```htmlBonjour1
Monde! ↩
Bonjour1
-mdast node | markdown example | hast node | html example | |
---|---|---|---|---|
[`blockquote`](https://github.com/syntax-tree/mdast#blockquote) | ```markdown > A greater than… ``` | [`element`](https://github.com/syntax-tree/hast#element) (`blockquote`) |
```html
``` |
|
[`break`](https://github.com/syntax-tree/mdast#break) | ```markdown A backslash\ before a line break… ``` | [`element`](https://github.com/syntax-tree/hast#element) (`br`) |
```html
A backslash |
|
[`code`](https://github.com/syntax-tree/mdast#code) | ````markdown ```js backtick.fences('for blocks') ``` ```` | [`element`](https://github.com/syntax-tree/hast#element) (`pre` and `code`) |
```html
```
|
|
[`delete`](https://github.com/syntax-tree/mdast#delete) (GFM) | ```markdown Two ~~tildes~~ for delete. ``` | [`element`](https://github.com/syntax-tree/hast#element) (`del`) |
```html
Two |
|
[`emphasis`](https://github.com/syntax-tree/mdast#emphasis) | ```markdown Some *asterisks* for emphasis. ``` | [`element`](https://github.com/syntax-tree/hast#element) (`em`) |
```html
Some asterisks for emphasis. ``` |
|
[`footnoteReference`](https://github.com/syntax-tree/mdast#footnotereference), [`footnoteDefinition`](https://github.com/syntax-tree/mdast#footnotedefinition) (GFM) | ```markdown With a [^caret]. [^caret]: Stuff ``` | [`element`](https://github.com/syntax-tree/hast#element) (`section`, `sup`, `a`) |
```html
With a 1. … ``` |
|
[`heading`](https://github.com/syntax-tree/mdast#heading) | ```markdown # One number sign… ###### Six number signs… ``` | [`element`](https://github.com/syntax-tree/hast#element) (`h1`…`h6`) |
```html
One number sign…Six number signs…``` |
|
[`html`](https://github.com/syntax-tree/mdast#html) | ```html CMD+S ``` | Nothing (default), `raw` (when `allowDangerousHtml: true`) | n/a | |
[`image`](https://github.com/syntax-tree/mdast#image) | ```markdown ![Alt text](/logo.png "title") ``` | [`element`](https://github.com/syntax-tree/hast#element) (`img`) | ```html ``` | |
[`imageReference`](https://github.com/syntax-tree/mdast#imagereference), [`definition`](https://github.com/syntax-tree/mdast#definition) | ```markdown ![Alt text][logo] [logo]: /logo.png "title" ``` | [`element`](https://github.com/syntax-tree/hast#element) (`img`) | ```html ``` | |
[`inlineCode`](https://github.com/syntax-tree/mdast#inlinecode) | ```markdown Some `backticks` for inline code. ``` | [`element`](https://github.com/syntax-tree/hast#element) (`code`) |
```html
Some |
|
[`link`](https://github.com/syntax-tree/mdast#link) | ```markdown [Example](https://example.com "title") ``` | [`element`](https://github.com/syntax-tree/hast#element) (`a`) | ```html ``` | |
[`linkReference`](https://github.com/syntax-tree/mdast#linkreference), [`definition`](https://github.com/syntax-tree/mdast#definition) | ```markdown [Example][] [example]: https://example.com "title" ``` | [`element`](https://github.com/syntax-tree/hast#element) (`a`) | ```html ``` | |
[`list`](https://github.com/syntax-tree/mdast#list), [`listItem`](https://github.com/syntax-tree/mdast#listitem) | ```markdown * asterisks for unordered items 1. decimals and a dot for ordered items ``` | [`element`](https://github.com/syntax-tree/hast#element) (`li` and `ol` or `ul`) |
```html
|
|
[`paragraph`](https://github.com/syntax-tree/mdast#paragraph) | ```markdown Just some text… ``` | [`element`](https://github.com/syntax-tree/hast#element) (`p`) |
```html
Just some text… ``` |
|
[`root`](https://github.com/syntax-tree/mdast#root) | ```markdown Anything! ``` | [`root`](https://github.com/syntax-tree/hast#root) |
```html
Anything! ``` |
|
[`strong`](https://github.com/syntax-tree/mdast#strong) | ```markdown Two **asterisks** for strong. ``` | [`element`](https://github.com/syntax-tree/hast#element) (`strong`) |
```html
Two asterisks for strong. ``` |
|
[`text`](https://github.com/syntax-tree/mdast#text) | ```markdown Anything! ``` | [`text`](https://github.com/syntax-tree/hast#text) |
```html
Anything! ``` |
|
[`table`](https://github.com/syntax-tree/mdast#table), [`tableRow`](https://github.com/syntax-tree/mdast#tablerow), [`tableCell`](https://github.com/syntax-tree/mdast#tablecell) | ```markdown | Pipes | | ----- | ``` | [`element`](https://github.com/syntax-tree/hast#element) (`table`, `thead`, `tbody`, `tr`, `td`, `th`) |
```html
|
|
[`thematicBreak`](https://github.com/syntax-tree/mdast#thematicbreak) | ```markdown Three asterisks for a thematic break: *** ``` | [`element`](https://github.com/syntax-tree/hast#element) (`hr`) |
```html
Three asterisks for a thematic break: ``` |
|
`toml` (frontmatter) | ```markdown +++ fenced = true +++ ``` | Nothing | n/a | |
[`yaml`](https://github.com/syntax-tree/mdast#yaml) (frontmatter) | ```markdown --- fenced: yes --- ``` | Nothing | n/a |