Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
mdast-util-gfm-table
Advanced tools
mdast extension to parse and serialize GFM tables
Weekly downloads
Package description
The mdast-util-gfm-table package is a utility to support GitHub Flavored Markdown (GFM) tables in mdast, which is a syntax tree format for Markdown. It allows users to parse and serialize tables in Markdown files following the GFM specification.
Parsing GFM tables
This feature allows you to parse GFM tables from a Markdown string and convert them into an mdast syntax tree. The code sample demonstrates how to use the package in combination with 'mdast-util-from-markdown' to parse a simple table.
import { fromMarkdown } from 'mdast-util-from-markdown';
import { gfmTable } from 'mdast-util-gfm-table';
const markdown = '| Header 1 | Header 2 |\n| -------- | -------- |\n| Cell 1 | Cell 2 |';
const mdastTree = fromMarkdown(markdown, {
extensions: [gfmTable]
});
console.log(mdastTree);
Serializing GFM tables
This feature allows you to serialize an mdast syntax tree containing a GFM table back into a Markdown string. The code sample shows how to use the package with 'mdast-util-to-markdown' to serialize a simple mdast table structure.
import { toMarkdown } from 'mdast-util-to-markdown';
import { gfmTable } from 'mdast-util-gfm-table';
const mdastTree = {
type: 'table',
align: ['left', 'right'],
children: [
{
type: 'tableRow',
children: [
{type: 'tableCell', children: [{type: 'text', value: 'Header 1'}]},
{type: 'tableCell', children: [{type: 'text', value: 'Header 2'}]}
]
},
{
type: 'tableRow',
children: [
{type: 'tableCell', children: [{type: 'text', value: 'Cell 1'}]},
{type: 'tableCell', children: [{type: 'text', value: 'Cell 2'}]}
]
}
]
};
const markdown = toMarkdown(mdastTree, {
extensions: [gfmTable]
});
console.log(markdown);
remark-gfm is a plugin for remark, a Markdown processor powered by plugins part of the unified collective. It adds support for GFM features, including tables. It is similar to mdast-util-gfm-table but is designed to work within the remark ecosystem.
markdown-it is a Markdown parser with GFM table support built-in. It is a full parser, not just a utility for tables, and it is designed to be fast and highly extensible with plugins.
turndown-plugin-gfm is a plugin for Turndown, an HTML to Markdown converter. It adds GFM features, including tables, to the conversion process. It is similar in that it deals with tables in Markdown, but it is for converting from HTML rather than parsing and serializing Markdown directly.
Readme
mdast extensions to parse and serialize GFM tables.
This package contains two extensions that add support for GFM table syntax in
markdown to mdast.
These extensions plug into
mdast-util-from-markdown
(to support parsing
tables in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
tables in syntax trees to markdown).
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-table
.
When you don’t need a syntax tree, you can use micromark
directly with micromark-extension-gfm-table
.
When you are working with syntax trees and want all of GFM, use
mdast-util-gfm
instead.
All these packages are used 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
.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install mdast-util-gfm-table
In Deno with esm.sh
:
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'https://esm.sh/mdast-util-gfm-table@2'
In browsers with esm.sh
:
<script type="module">
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'https://esm.sh/mdast-util-gfm-table@2?bundle'
</script>
Say our document example.md
contains:
| a | b | c | d |
| - | :- | -: | :-: |
| e | f |
| g | h | i | j | k |
…and our module example.js
looks as follows:
import fs from 'node:fs/promises'
import {gfmTable} from 'micromark-extension-gfm-table'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table'
import {toMarkdown} from 'mdast-util-to-markdown'
const doc = await fs.readFile('example.md')
const tree = fromMarkdown(doc, {
extensions: [gfmTable()],
mdastExtensions: [gfmTableFromMarkdown()]
})
console.log(tree)
const out = toMarkdown(tree, {extensions: [gfmTableToMarkdown()]})
console.log(out)
…now running node example.js
yields (positional info removed for brevity):
{
type: 'root',
children: [
{
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: 'tableRow',
children: [
{type: 'tableCell', children: [{type: 'text', value: 'e'}]},
{type: 'tableCell', children: [{type: 'text', value: 'f'}]}
]
},
{
type: 'tableRow',
children: [
{type: 'tableCell', children: [{type: 'text', value: 'g'}]},
{type: 'tableCell', children: [{type: 'text', value: 'h'}]},
{type: 'tableCell', children: [{type: 'text', value: 'i'}]},
{type: 'tableCell', children: [{type: 'text', value: 'j'}]},
{type: 'tableCell', children: [{type: 'text', value: 'k'}]}
]
}
]
}
]
}
| a | b | c | d | |
| - | :- | -: | :-: | - |
| e | f | | | |
| g | h | i | j | k |
This package exports the identifiers
gfmTableFromMarkdown
and
gfmTableToMarkdown
.
There is no default export.
gfmTableFromMarkdown
Create an extension for mdast-util-from-markdown
to enable GFM tables in markdown.
Extension for mdast-util-from-markdown
to enable GFM tables
(FromMarkdownExtension
).
gfmTableToMarkdown(options?)
Create an extension for mdast-util-to-markdown
to
enable GFM tables in markdown.
options
(Options
, optional)
— configurationExtension for mdast-util-to-markdown
to enable GFM tables
(ToMarkdownExtension
).
Options
Configuration (TypeScript type).
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 cellsstringLength
It’s possible to align tables based on the visual width of cells. First, let’s show the problem:
import {gfmTable} from 'micromark-extension-gfm-table'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table'
import {toMarkdown} from 'mdast-util-to-markdown'
const doc = `| Alpha | Bravo |
| - | - |
| 中文 | Charlie |
| 👩❤️👩 | Delta |`
const tree = fromMarkdown(doc, {
extensions: [gfmTable],
mdastExtensions: [gfmTableFromMarkdown]
})
console.log(toMarkdown(tree, {extensions: [gfmTableToMarkdown()]}))
The above code shows how these utilities can be used to format markdown. The output is as follows:
| Alpha | Bravo |
| -------- | ------- |
| 中文 | Charlie |
| 👩❤️👩 | Delta |
To improve the alignment of these full-width characters and emoji, pass a
stringLength
function that calculates the visual width of cells.
One such algorithm is string-width
.
It can be used like so:
@@ -2,6 +2,7 @@ import {gfmTable} from 'micromark-extension-gfm-table'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table'
import {toMarkdown} from 'mdast-util-to-markdown'
+import stringWidth from 'string-width'
const doc = `| Alpha | Bravo |
| - | - |
@@ -13,4 +14,8 @@ const tree = fromMarkdown(doc, {
mdastExtensions: [gfmTableFromMarkdown()]
})
-console.log(toMarkdown(tree, {extensions: [gfmTableToMarkdown()]}))
+console.log(
+ toMarkdown(tree, {
+ extensions: [gfmTableToMarkdown({stringLength: stringWidth})]
+ })
+)
The output of our code with these changes is as follows:
| Alpha | Bravo |
| ----- | ------- |
| 中文 | Charlie |
| 👩❤️👩 | Delta |
This utility does not handle how markdown is turned to HTML.
That’s done by mdast-util-to-hast
.
See Syntax in micromark-extension-gfm-table
.
The following interfaces are added to mdast by this utility.
Table
interface Table <: Parent {
type: 'table'
align: [alignType]?
children: [TableContent]
}
Table (Parent) represents two-dimensional data.
Table can be used where flow content is expected. Its content model is table content.
The head of the node represents the labels of the columns.
An align
field can be present.
If present, it must be a list of alignTypes.
It represents how cells in columns are aligned.
For example, the following markdown:
| foo | bar |
| :-- | :-: |
| baz | qux |
Yields:
{
type: 'table',
align: ['left', 'center'],
children: [
{
type: 'tableRow',
children: [
{
type: 'tableCell',
children: [{type: 'text', value: 'foo'}]
},
{
type: 'tableCell',
children: [{type: 'text', value: 'bar'}]
}
]
},
{
type: 'tableRow',
children: [
{
type: 'tableCell',
children: [{type: 'text', value: 'baz'}]
},
{
type: 'tableCell',
children: [{type: 'text', value: 'qux'}]
}
]
}
]
}
TableRow
interface TableRow <: Parent {
type: "tableRow"
children: [RowContent]
}
TableRow (Parent) represents a row of cells in a table.
TableRow can be used where table content is expected. Its content model is row content.
If the node is a head, it represents the labels of the columns for its parent Table.
For an example, see Table.
TableCell
interface TableCell <: Parent {
type: "tableCell"
children: [PhrasingContent]
}
TableCell (Parent) represents a header cell in a Table, if its parent is a head, or a data cell otherwise.
TableCell can be used where row content is expected. Its content model is phrasing content excluding Break nodes.
For an example, see Table.
alignType
enum alignType {
'center' | 'left' | 'right' | null
}
alignType represents how phrasing content is aligned ([CSSTEXT]).
'left'
: See the left
value of the text-align
CSS
property'right'
: See the right
value of the text-align
CSS property'center'
: See the center
value of the text-align
CSS propertynull
: phrasing content is aligned as defined by the host environmentFlowContent
(GFM table)type FlowContentGfm = Table | FlowContent
TableContent
type TableContent = TableRow
Table content represent the rows in a table.
RowContent
type RowContent = TableCell
Row content represent the cells in a row.
This package is fully typed with TypeScript.
It exports the additional type Options
.
The Table
, TableRow
, and TableCell
types of the mdast nodes are exposed
from @types/mdast
.
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-table@^2
,
compatible with Node.js 16.
This utility works with mdast-util-from-markdown
version 2+ and
mdast-util-to-markdown
version 2+.
remarkjs/remark-gfm
— remark plugin to support GFMsyntax-tree/mdast-util-gfm
— same but all of GFM (autolink literals, footnotes, strikethrough, tables,
tasklists)micromark/micromark-extension-gfm-table
— micromark extension to parse GFM tablesSee 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 tables
The npm package mdast-util-gfm-table receives a total of 2,396,280 weekly downloads. As such, mdast-util-gfm-table popularity was classified as popular.
We found that mdast-util-gfm-table demonstrated a healthy version release cadence and project activity because the last version was released less than 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
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.