mdast extension to parse and serialize GFM (GitHub Flavored Markdown)
Installations
npm install mdast-util-gfm
Developer
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
20.0.0
NPM Version
9.7.2
Statistics
19 Stars
64 Commits
6 Forks
9 Watching
1 Branches
11 Contributors
Updated on 30 Oct 2024
Bundle Size
24.17 kB
Minified
7.28 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
288,040,944
Last day
-1%
777,334
Compared to previous day
Last week
5.8%
4,103,662
Compared to previous week
Last month
17%
16,752,390
Compared to previous month
Last year
95.1%
158,147,516
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
7
mdast-util-gfm
mdast extensions to parse and serialize GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
Contents
- What is this?
- When to use this
- Install
- Use
- API
- HTML
- Syntax
- Syntax tree
- Types
- Compatibility
- Related
- Contribute
- License
What is this?
This package contains two extensions that add support for GFM syntax in
markdown to mdast: autolink literals (www.x.com
), footnotes ([^1]
),
strikethrough (~~stuff~~
), tables (| cell |…
), and tasklists (* [x]
).
These extensions plug into
mdast-util-from-markdown
(to support parsing
GFM in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
GFM in syntax trees to markdown).
When to use this
This project is useful when you want to support the same features that GitHub
does in files in a repo, Gists, and several other places.
Users frequently believe that some of these extensions, specifically autolink
literals and tables, are part of normal markdown, so using mdast-util-gfm
will
help match your implementation to their understanding of markdown.
There are several edge cases where GitHub’s implementation works in unexpected
ways or even different than described in their spec, so writing in GFM is not
always the best choice.
You can use these extensions when you are working with
mdast-util-from-markdown
and mdast-util-to-markdown
already.
When working with mdast-util-from-markdown
, you must combine this package
with micromark-extension-gfm
.
Instead of this package, you can also use the extensions separately:
mdast-util-gfm-autolink-literal
— support GFM autolink literalsmdast-util-gfm-footnote
— support GFM footnotesmdast-util-gfm-strikethrough
— support GFM strikethroughmdast-util-gfm-table
— support GFM tablesmdast-util-gfm-task-list-item
— support GFM tasklists
A different utility, mdast-util-frontmatter
, adds
support for frontmatter.
GitHub supports YAML frontmatter for files in repos and Gists but they don’t
treat it as part of GFM.
All these packages are used in remark-gfm
, which
focusses on making it easier to transform content by abstracting these
internals away.
This utility does not handle how markdown is turned to HTML.
That’s done by mdast-util-to-hast
.
If your content is not in English, you should configure that utility.
Install
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install mdast-util-gfm
In Deno with esm.sh
:
1import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@3'
In browsers with esm.sh
:
1<script type="module"> 2 import {gfmFromMarkdown, gfmToMarkdown} from 'https://esm.sh/mdast-util-gfm@3?bundle' 3</script>
Use
Say our document example.md
contains:
1# GFM 2 3## Autolink literals 4 5www.example.com, https://example.com, and contact@example.com. 6 7## Footnote 8 9A note[^1] 10 11[^1]: Big note. 12 13## Strikethrough 14 15~one~ or ~~two~~ tildes. 16 17## Table 18 19| a | b | c | d | 20| - | :- | -: | :-: | 21 22## Tasklist 23 24* [ ] to do 25* [x] done
…and our module example.js
looks as follows:
1import fs from 'node:fs/promises' 2import {gfm} from 'micromark-extension-gfm' 3import {fromMarkdown} from 'mdast-util-from-markdown' 4import {gfmFromMarkdown, gfmToMarkdown} from 'mdast-util-gfm' 5import {toMarkdown} from 'mdast-util-to-markdown' 6 7const doc = await fs.readFile('example.md') 8 9const tree = fromMarkdown(doc, { 10 extensions: [gfm()], 11 mdastExtensions: [gfmFromMarkdown()] 12}) 13 14console.log(tree) 15 16const out = toMarkdown(tree, {extensions: [gfmToMarkdown()]}) 17 18console.log(out)
…now running node example.js
yields (positional info removed for brevity):
1{ 2 type: 'root', 3 children: [ 4 {type: 'heading', depth: 1, children: [{type: 'text', value: 'GFM'}]}, 5 { 6 type: 'heading', 7 depth: 2, 8 children: [{type: 'text', value: 'Autolink literals'}] 9 }, 10 { 11 type: 'paragraph', 12 children: [ 13 { 14 type: 'link', 15 title: null, 16 url: 'http://www.example.com', 17 children: [{type: 'text', value: 'www.example.com'}] 18 }, 19 {type: 'text', value: ', '}, 20 { 21 type: 'link', 22 title: null, 23 url: 'https://example.com', 24 children: [{type: 'text', value: 'https://example.com'}] 25 }, 26 {type: 'text', value: ', and '}, 27 { 28 type: 'link', 29 title: null, 30 url: 'mailto:contact@example.com', 31 children: [{type: 'text', value: 'contact@example.com'}] 32 }, 33 {type: 'text', value: '.'} 34 ] 35 }, 36 {type: 'heading', depth: 2, children: [{type: 'text', value: 'Footnote'}]}, 37 { 38 type: 'paragraph', 39 children: [ 40 {type: 'text', value: 'A note'}, 41 {type: 'footnoteReference', identifier: '1', label: '1'} 42 ] 43 }, 44 { 45 type: 'footnoteDefinition', 46 identifier: '1', 47 label: '1', 48 children: [ 49 {type: 'paragraph', children: [{type: 'text', value: 'Big note.'}]} 50 ] 51 }, 52 { 53 type: 'heading', 54 depth: 2, 55 children: [{type: 'text', value: 'Strikethrough'}] 56 }, 57 { 58 type: 'paragraph', 59 children: [ 60 { 61 type: 'delete', 62 children: [{type: 'text', value: 'one'}] 63 }, 64 {type: 'text', value: ' or '}, 65 { 66 type: 'delete', 67 children: [{type: 'text', value: 'two'}] 68 }, 69 {type: 'text', value: ' tildes.'} 70 ] 71 }, 72 {type: 'heading', depth: 2, children: [{type: 'text', value: 'Table'}]}, 73 { 74 type: 'table', 75 align: [null, 'left', 'right', 'center'], 76 children: [ 77 { 78 type: 'tableRow', 79 children: [ 80 {type: 'tableCell', children: [{type: 'text', value: 'a'}]}, 81 {type: 'tableCell', children: [{type: 'text', value: 'b'}]}, 82 {type: 'tableCell', children: [{type: 'text', value: 'c'}]}, 83 {type: 'tableCell', children: [{type: 'text', value: 'd'}]} 84 ] 85 } 86 ] 87 }, 88 {type: 'heading', depth: 2, children: [{type: 'text', value: 'Tasklist'}]}, 89 { 90 type: 'list', 91 ordered: false, 92 start: null, 93 spread: false, 94 children: [ 95 { 96 type: 'listItem', 97 spread: false, 98 checked: false, 99 children: [ 100 {type: 'paragraph', children: [{type: 'text', value: 'to do'}]} 101 ] 102 }, 103 { 104 type: 'listItem', 105 spread: false, 106 checked: true, 107 children: [ 108 {type: 'paragraph', children: [{type: 'text', value: 'done'}]} 109 ] 110 } 111 ] 112 } 113 ] 114}
1# GFM 2 3## Autolink literals 4 5[www.example.com](http://www.example.com), <https://example.com>, and <contact@example.com>. 6 7## Footnote 8 9A note[^1] 10 11[^1]: Big note. 12 13## Strikethrough 14 15~~one~~ or ~~two~~ tildes. 16 17## Table 18 19| a | b | c | d | 20| - | :- | -: | :-: | 21 22## Tasklist 23 24* [ ] to do 25* [x] done
API
This package exports the identifiers gfmFromMarkdown
and gfmToMarkdown
.
There is no default export.
gfmFromMarkdown()
Create an extension for mdast-util-from-markdown
to enable GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
Returns
Extension for mdast-util-from-markdown
to enable GFM
(Array<FromMarkdownExtension>
).
gfmToMarkdown(options?)
Create an extension for mdast-util-to-markdown
to enable GFM (autolink literals, footnotes, strikethrough, tables, tasklists).
Parameters
options
(Options
) — configuration
Returns
Extension for mdast-util-to-markdown
to enable GFM
(Array<ToMarkdownExtension>
).
Options
Configuration (TypeScript type).
Fields
tableCellPadding
(boolean
, default:true
) — whether to add a space of padding between delimiters and cellstablePipeAlign
(boolean
, default:true
) — whether to align the delimitersstringLength
(((value: string) => number)
, default:s => s.length
) — function to detect the length of table cell content, used when aligning the delimiters between cells
HTML
This utility does not handle how markdown is turned to HTML.
That’s done by mdast-util-to-hast
.
Syntax
See Syntax in micromark-extension-gfm
.
Syntax tree
This utility combines several mdast utilities. See their readmes for the node types supported in the tree:
mdast-util-gfm-autolink-literal
— GFM autolink literalsmdast-util-gfm-footnote
— GFM footnotesmdast-util-gfm-strikethrough
— GFM strikethroughmdast-util-gfm-table
— GFM tablesmdast-util-gfm-task-list-item
— GFM tasklists
Types
This package is fully typed with TypeScript.
It exports the additional type Options
.
The Delete
, FootnoteDefinition
, FootnoteReference
, Table
, TableRow
,
and TableCell
types of the mdast nodes are exposed from @types/mdast
.
Compatibility
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, mdast-util-gfm@^3
,
compatible with Node.js 16.
Related
remark-gfm
— remark plugin to support GFMmicromark-extension-gfm
— micromark extension to parse GFM
Contribute
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.
License
MIT © Titus Wormer
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/syntax-tree/.github/security.md:1
- Info: Found linked content: github.com/syntax-tree/.github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/syntax-tree/.github/security.md:1
- Info: Found text in security policy: github.com/syntax-tree/.github/security.md:1
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bb.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/mdast-util-gfm/bb.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/mdast-util-gfm/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/mdast-util-gfm/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/mdast-util-gfm/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:15
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/bb.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to mdast-util-gfm
mdast-util-gfm-strikethrough
mdast extension to parse and serialize GFM strikethrough
mdast-util-gfm-table
mdast extension to parse and serialize GFM tables
mdast-util-gfm-footnote
mdast extension to parse and serialize GFM footnotes
mdast-util-gfm-autolink-literal
mdast extension to parse and serialize GFM autolink literals