Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
mdast-util-gfm
Advanced tools
The `mdast-util-gfm` package is a utility for working with GitHub Flavored Markdown (GFM) in the MDAST (Markdown Abstract Syntax Tree) format. It provides functions to parse and transform GFM-specific syntax such as tables, strikethroughs, task lists, and autolinks.
Tables
This feature allows you to parse and transform GitHub Flavored Markdown tables into an MDAST tree structure.
const { fromMarkdown, toMarkdown } = require('mdast-util-gfm');
const markdown = '| Header1 | Header2 |
| ------- | ------- |
| Cell1 | Cell2 |';
const tree = fromMarkdown(markdown);
console.log(tree);
Strikethrough
This feature allows you to parse and transform strikethrough text in GitHub Flavored Markdown into an MDAST tree structure.
const { fromMarkdown, toMarkdown } = require('mdast-util-gfm');
const markdown = 'This is ~~strikethrough~~ text.';
const tree = fromMarkdown(markdown);
console.log(tree);
Task Lists
This feature allows you to parse and transform task lists in GitHub Flavored Markdown into an MDAST tree structure.
const { fromMarkdown, toMarkdown } = require('mdast-util-gfm');
const markdown = '- [x] Task 1
- [ ] Task 2';
const tree = fromMarkdown(markdown);
console.log(tree);
Autolinks
This feature allows you to parse and transform autolinks in GitHub Flavored Markdown into an MDAST tree structure.
const { fromMarkdown, toMarkdown } = require('mdast-util-gfm');
const markdown = 'Visit https://example.com for more info.';
const tree = fromMarkdown(markdown);
console.log(tree);
The `remark-gfm` package is a plugin for the `remark` ecosystem that adds support for GitHub Flavored Markdown. It provides similar functionality to `mdast-util-gfm` but is designed to be used within the `remark` processor.
The `markdown-it` package is a Markdown parser that supports GitHub Flavored Markdown out of the box. It is a more general-purpose parser compared to `mdast-util-gfm`, which is specifically designed for working with MDAST.
The `marked` package is a fast Markdown parser and compiler that supports GitHub Flavored Markdown. It is similar to `markdown-it` in that it is a general-purpose parser, but it is known for its speed and performance.
Extension for mdast-util-from-markdown
and/or
mdast-util-to-markdown
to support GitHub flavored markdown in
mdast.
When parsing (from-markdown
), must be combined with
micromark-extension-gfm
.
Use this if you’re dealing with the AST manually and need all of GFM.
It’s probably nicer to use remark-gfm
with
remark, which includes this but provides a nicer interface and
makes it easier to combine with hundreds of plugins.
Alternatively, the extensions can be used separately:
syntax-tree/mdast-util-gfm-autolink-literal
— support GFM autolink literalssyntax-tree/mdast-util-gfm-footnote
— support GFM footnotessyntax-tree/mdast-util-gfm-strikethrough
— support GFM strikethroughsyntax-tree/mdast-util-gfm-table
— support GFM tablessyntax-tree/mdast-util-gfm-task-list-item
— support GFM tasklistsThis package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install mdast-util-gfm
Say we have the following file, example.md
:
# GFM
## Autolink literals
www.example.com, https://example.com, and contact@example.com.
## Footnote
A note[^1]
[^1]: Big note.
## Strikethrough
~one~ or ~~two~~ tildes.
## Table
| a | b | c | d |
| - | :- | -: | :-: |
## Tasklist
* [ ] to do
* [x] done
And our module, example.js
, looks as follows:
import fs from 'node:fs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {gfm} from 'micromark-extension-gfm'
import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm'
const doc = fs.readFileSync('example.md')
const tree = fromMarkdown(doc, {
extensions: [gfm()],
mdastExtensions: [gfmFromMarkdown()]
})
console.log(tree)
const out = toMarkdown(tree, {extensions: [gfmToMarkdown()]})
console.log(out)
Now, running node example
yields:
{
type: 'root',
children: [
{type: 'heading', depth: 1, children: [{type: 'text', value: 'GFM'}]},
{
type: 'heading',
depth: 2,
children: [{type: 'text', value: 'Autolink literals'}]
},
{
type: 'paragraph',
children: [
{
type: 'link',
title: null,
url: 'http://www.example.com',
children: [{type: 'text', value: 'www.example.com'}]
},
{type: 'text', value: ', '},
{
type: 'link',
title: null,
url: 'https://example.com',
children: [{type: 'text', value: 'https://example.com'}]
},
{type: 'text', value: ', and '},
{
type: 'link',
title: null,
url: 'mailto:contact@example.com',
children: [{type: 'text', value: 'contact@example.com'}]
},
{type: 'text', value: '.'}
]
},
{type: 'heading', depth: 2, children: [{type: 'text', value: 'Footnote'}]},
{
type: 'paragraph',
children: [
{type: 'text', value: 'A note'},
{type: 'footnoteReference', identifier: '1', label: '1'}
]
},
{
type: 'footnoteDefinition',
identifier: '1',
label: '1',
children: [
{type: 'paragraph', children: [{type: 'text', value: 'Big note.'}]}
]
},
{
type: 'heading',
depth: 2,
children: [{type: 'text', value: 'Strikethrough'}]
},
{
type: 'paragraph',
children: [
{
type: 'delete',
children: [{type: 'text', value: 'one'}]
},
{type: 'text', value: ' or '},
{
type: 'delete',
children: [{type: 'text', value: 'two'}]
},
{type: 'text', value: ' tildes.'}
]
},
{type: 'heading', depth: 2, children: [{type: 'text', value: 'Table'}]},
{
type: 'table',
align: [null, 'left', 'right', 'center'],
children: [
{
type: 'tableRow',
children: [
{type: 'tableCell', children: [{type: 'text', value: 'a'}]},
{type: 'tableCell', children: [{type: 'text', value: 'b'}]},
{type: 'tableCell', children: [{type: 'text', value: 'c'}]},
{type: 'tableCell', children: [{type: 'text', value: 'd'}]}
]
}
]
},
{type: 'heading', depth: 2, children: [{type: 'text', value: 'Tasklist'}]},
{
type: 'list',
ordered: false,
start: null,
spread: false,
children: [
{
type: 'listItem',
spread: false,
checked: false,
children: [
{type: 'paragraph', children: [{type: 'text', value: 'to do'}]}
]
},
{
type: 'listItem',
spread: false,
checked: true,
children: [
{type: 'paragraph', children: [{type: 'text', value: 'done'}]}
]
}
]
}
]
}
# GFM
## Autolink literals
[www.example.com](http://www.example.com), <https://example.com>, and <contact@example.com>.
## Footnote
A note[^1]
[^1]: Big note.
## Strikethrough
~~one~~ or ~~two~~ tildes.
## Table
| a | b | c | d |
| - | :- | -: | :-: |
## Tasklist
* [ ] to do
* [x] done
This package exports the following identifiers: gfmFromMarkdown
,
gfmToMarkdown
.
There is no default export.
gfmFromMarkdown()
gfmToMarkdown(options?)
Support GFM.
The export of fromMarkdown
is a function that can be called and returns an
extension for mdast-util-from-markdown
.
The export of toMarkdown
is a function that can be called with options and
returns an extension for mdast-util-to-markdown
.
options
Passed as options
to mdast-util-gfm-table
.
remarkjs/remark
— markdown processor powered by pluginsremarkjs/remark-gfm
— remark plugin to support GFMmicromark/micromark
— the smallest commonmark-compliant markdown parser that existsmicromark/micromark-extension-gfm
— micromark extension to parse GFMsyntax-tree/mdast-util-from-markdown
— mdast parser using micromark
to create mdast from markdownsyntax-tree/mdast-util-to-markdown
— mdast serializer to create markdown from mdastSee 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.
FAQs
mdast extension to parse and serialize GFM (GitHub Flavored Markdown)
The npm package mdast-util-gfm receives a total of 2,200,335 weekly downloads. As such, mdast-util-gfm popularity was classified as popular.
We found that mdast-util-gfm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.