跳转至

mdast-util-gfm-table

Build Coverage Downloads Size Sponsors Backers Chat

Extension for mdast-util-from-markdown and/or mdast-util-to-markdown to support GitHub flavored markdown tables in mdast. When parsing (from-markdown), must be combined with micromark-extension-gfm-table.

You probably shouldn’t use this package directly, but instead use remark-gfm with remark.

Install

npm:

npm install mdast-util-gfm-table

Use

Say we have the following file, example.md:

| a | b | c | d |
| - | :- | -: | :-: |
| e | f |
| g | h | i | j | k |

And our script, example.js, looks as follows:

var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var syntax = require('micromark-extension-gfm-table')
var table = require('mdast-util-gfm-table')

var doc = fs.readFileSync('example.md')

var tree = fromMarkdown(doc, {
  extensions: [syntax],
  mdastExtensions: [table.fromMarkdown]
})

console.log(tree)

var out = toMarkdown(tree, {extensions: [table.toMarkdown()]})

console.log(out)

Now, running node example yields (positional info removed for the sake of 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 |

API

table.fromMarkdown

table.toMarkdown(options?)

Note: the separate extensions are also available at mdast-util-gfm-table/from-markdown and mdast-util-gfm-table/to-markdown.

Support tables. The exports of fromMarkdown is 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
options.tableCellPadding

Create tables with a space between cell delimiters (|) and content (boolean, default: true).

options.tablePipeAlign

Align the delimiters (|) between table cells so that they all align nicely and form a grid (boolean, default: true).

options.stringLength

Function passed to markdown-table to detect the length of a table cell (Function, default: s => s.length). Used to pad tables.

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