What is mdast?
mainunified and remark projects), the specification is language-agnostic and can be used in other programming languages.repository·main·Indexed 23 days ago
https://github.com/syntax-tree/mdastA language-agnostic specification for representing Markdown as an abstract syntax tree (AST). Part of the unist ecosystem and the foundational data structure for the remark and unified projects, mdast supports various Markdown flavors including CommonMark and GitHub Flavored Markdown (GFM). It defines nodes for elements such as blockquotes, headings, lists, and images, and provides a wide range of utilities for parsing, serialization, transformation, and manipulation.
unified and remark projects), the specification is language-agnostic and can be used in other programming languages.mdast is part of a larger ecosystem of syntax tree formats:
If you are developing in TypeScript, you can install the official type definitions for mdast using npm.
npm install @types/mdastBecause mdast can contain HTML, improper use can lead to Cross-Site Scripting (XSS) attacks. When transforming an mdast tree to HTML (typically via hast), you must sanitize the resulting tree.
Always use hast-util-sanitize to ensure the generated HTML is safe from malicious user input.
Frontmatter represents out-of-band metadata for the document, typically using YAML. It must be limited to one node in the tree and can only exist as a head.
YamlFrontmatterContent (which is just Yaml).value: The YAML data as a string.Example markdown:
---
foo: bar
---Yields:
{type: 'yaml', value: 'foo: bar'}A wide range of utilities exists to parse, serialize, transform, and manipulate mdast trees. Key categories include:
mdast-util-from-markdown (parse markdown), mdast-util-to-markdown (serialize markdown), mdast-util-frontmatter (frontmatter), mdast-util-gfm (GFM), mdast-util-mdx (MDX), and mdast-util-math (math).mdast-util-to-hast (transform to HTML AST), mdast-util-to-nlcst (transform to natural language AST), mdast-util-toc (generate table of contents), and mdast-util-compact (compact trees).mdast-util-find-and-replace (text replacement), mdast-util-definitions (find definition nodes), and mdast-squeeze-paragraphs (remove empty paragraphs).mdast-util-assert (assert nodes) and mdast-util-phrasing (check phrasing content).mdast-util-from-adf (Atlassian Document Format) and mdast-util-gridtables (gridtables).The Literal interface is an abstract interface in mdast that represents a node containing a raw value. It extends UnistLiteral.
Properties:
value: A string representing the literal content.interface Literal <: UnistLiteral {
value: string
}A Root node represents the entire document. It is a Parent node that serves as the root of a tree and cannot be a child of another node. Its content can be any mdast content as long as all content belongs to the same category.
interface Root <: Parent {
type: 'root'
}A List node represents a list of items. It is a Parent node containing ListContent.
Key properties:
ordered: boolean. If true, the list is intentionally ordered.start: number. If ordered is true, this is the starting number.spread: boolean. If true, one or more children are separated from siblings by a blank line.Example markdown:
1. fooYields:
{
type: 'list',
ordered: true,
start: 1,
spread: false,
children: [{
type: 'listItem',
spread: false,
children: [{
type: 'paragraph',
children: [{type: 'text', value: 'foo'}]
}]
}]
}interface List <: Parent {
type: 'list'
ordered: boolean?
start: number?
spread: boolean?
children: [ListContent]
}An ImageReference node represents an image through association (referencing a Definition) or its original source. It is a Node that includes the Reference and Alternative mixins and is described by its alt field.
Example markdown:
![alpha][bravo]Yields:
{
type: 'imageReference',
identifier: 'bravo',
label: 'bravo',
referenceType: 'full',
alt: 'alpha'
}interface ImageReference <: Node {
type: 'imageReference'
}
ImageReference includes Reference
ImageReference includes AlternativeAn Html node represents a fragment of raw HTML. It is a Literal node where the content is stored in the value field. These nodes do not need to be valid or complete HTML constructs.
Example markdown:
<div>Yields:
{type: 'html', value: '<div>'}interface Html <: Literal {
type: 'html'
}A Break node represents a line break (e.g., in poems or addresses). It is a Node used where phrasing content is expected and has no content model.
Example markdown:
foo··
barYields:
{
type: 'paragraph',
children: [
{type: 'text', value: 'foo'},
{type: 'break'},
{type: 'text', value: 'bar'}
]
}interface Break <: Node {
type: 'break'
}