JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5739569
  • Score
    100M100P100Q248084F
  • License MIT

mdast extension to parse and serialize GFM tables

Package Exports

  • mdast-util-gfm-table

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mdast-util-gfm-table) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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