Skip to content

utility to transform hast (HTML) to mdast (markdown)

License

Notifications You must be signed in to change notification settings

syntax-tree/hast-util-to-mdast

Repository files navigation

hast-util-to-mdast

Build Coverage Downloads Chat

Transform hast (HTML) to mdast (markdown).

Note: You probably want to use rehype-remark.

Installation

npm:

npm install hast-util-to-mdast

Usage

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!**

API

toMdast(node[, options])

Transform the given hast tree to mdast.

Options
options.handlers

Object mapping tag-names to functions handling those elements. Take a look at handlers/ for examples.

options.document

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.

options.newlines

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.

Returns

MDASTNode.

Notes
Implied sentences

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.
Ignoring nodes

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 .

Related

Contribute

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.

License

MIT © Titus Wormer