mdast utility to parse markdown.
npm:
npm install mdast-util-to-markdown
Say we have the following script, example.js
:
var toMarkdown = require('mdast-util-to-markdown')
var tree = {
type: 'root',
children: [
{
type: 'blockquote',
children: [
{type: 'thematicBreak'},
{
type: 'paragraph',
children: [
{type: 'text', value: '- a\nb !'},
{
type: 'link',
url: 'example.com',
children: [{type: 'text', value: 'd'}]
}
]
}
]
}
]
}
console.log(toMarkdown(tree))
Now, running node example
yields (note the properly escaped characters which
would otherwise turn into a list and image respectively):
> ***
>
> \- a
> b \![d](example.com)
Serialize mdast to markdown.
Marker to use to for bullets of items in unordered lists ('*'
, '+'
, or '-'
,
default: '*'
).
Whether to add the same number of number signs (#
) at the end of an ATX
heading as the opening sequence (boolean
, default: false
).
Marker to use to serialize emphasis ('*'
or '_'
, default: '*'
).
Marker to use to serialize fenced code ('`'
or '~'
, default: '`'
).
Whether to use fenced code always (boolean
, default: false
).
The default is to fenced code if there is a language defined, if the code is
empty, or if it starts or ends in empty lines.
Whether to increment the value of bullets of items in ordered lists (boolean
,
default: true
).
Whether to indent the content of list items with the size of the bullet plus one
space (when 'one'
) or a tab stop ('tab'
), or depending on the item and its
parent list ('mixed'
, uses 'one'
if the item and list are tight and 'tab'
otherwise) ('one'
, 'tab'
, or 'mixed'
, default: 'tab'
).
Marker to use to serialize titles ('"'
or "'"
, default: '"'
).
Whether to use reference links always (boolean
, default: false
).
The default is to use autolinks (<https://example.com>
) when possible.
Marker to use for thematic breaks ('*'
, '-'
, or '_'
, default: '*'
).
Number of markers to use for thematic breaks (number
, default:
3
, min: 3
).
Whether to add spaces between markers in thematic breaks (boolean
, default:
false
).
Whether to use setext headings when possible (boolean
, default: false
).
Setext headings are not possible for headings with a rank more than 2 or when
they’re empty.
Marker to use to serialize strong ('*'
or '_'
, default: '*'
).
Whether to join definitions w/o a blank line (boolean
, default: false
).
Shortcut for a join function like so:
function (left, right) {
if (left.type === 'definition' && right.type === 'definition') {
return 0
}
}
Object mapping node types to custom handlers.
Useful for syntax extensions.
Take a look at lib/handle
for examples.
List of functions used to determine what to place between two flow nodes.
Often, they are joined by one blank line.
In certain cases, it’s nicer to have them next to each other.
Or, they can’t occur together.
These functions receive two adjacent nodes and their parent and can return
number
or boolean
, referring to how many blank lines to use between them.
A return value of true
is as passing 1
.
A return value of false
means the nodes cannot be joined by a blank line, such
as two adjacent block quotes or indented code after a list, in which case a
comment will be injected to break them up:
> Quote 1
<!---->
> Quote 2
List of patterns to escape.
Useful for syntax extensions.
Take a look at lib/unsafe.js
for examples.
List of extensions (Array.<ToMarkdownExtension>
).
Each ToMarkdownExtension
is an object with the same interface as options
here.
string
— Serialized markdown.
syntax-tree/mdast-util-directive
— serialize directivessyntax-tree/mdast-util-footnote
— serialize footnotessyntax-tree/mdast-util-frontmatter
— serialize frontmatter (YAML, TOML, more)syntax-tree/mdast-util-gfm
— serialize GFMsyntax-tree/mdast-util-gfm-autolink-literal
— serialize GFM autolink literalssyntax-tree/mdast-util-gfm-strikethrough
— serialize GFM strikethroughsyntax-tree/mdast-util-gfm-table
— serialize GFM tablessyntax-tree/mdast-util-gfm-task-list-item
— serialize GFM task list itemssyntax-tree/mdast-util-math
— serialize mathsyntax-tree/mdast-util-mdx
— serialize MDX or MDX.jssyntax-tree/mdast-util-mdx-expression
— serialize MDX or MDX.js expressionssyntax-tree/mdast-util-mdx-jsx
— serialize MDX or MDX.js JSXsyntax-tree/mdast-util-mdxjs-esm
— serialize MDX.js ESM
mdast-util-to-markdown
will do its best to serialize markdown to match the
syntax tree, but there are several cases where that is impossible.
It’ll do its best, but complete roundtripping is impossible given that any value
could be injected into the tree.
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-to-markdown
and parsing it again later could potentially be unsafe.
When parsing markdown afterwards and then going to HTML, use something like
hast-util-sanitize
to make the tree safe.
micromark/micromark
— the smallest commonmark-compliant markdown parser that existsremarkjs/remark
— markdown processor powered by pluginssyntax-tree/mdast-util-from-markdown
— parse markdown to mdast
See contributing.md
in syntax-tree/.github
for ways to get
started.
See support.md
for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.