Socket
Socket
Sign inDemoInstall

mdast-util-gfm-table

Package Overview
Dependencies
18
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mdast-util-gfm-table

mdast extension to parse and serialize GFM tables


Version published
Weekly downloads
2.9M
decreased by-6.23%
Maintainers
2
Install size
418 kB
Created
Weekly downloads
 

Package description

What is mdast-util-gfm-table?

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.

What are mdast-util-gfm-table's main functionalities?

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);

Other packages similar to mdast-util-gfm-table

Readme

Source

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.

When to use this

Use mdast-util-gfm if you want all of GFM. Use this otherwise.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

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 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 {gfmTable} from 'micromark-extension-gfm-table'
import {gfmTableFromMarkdown, gfmTableToMarkdown} from 'mdast-util-gfm-table'

const doc = fs.readFileSync('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 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

This package exports the following identifier: gfmTableFromMarkdown, gfmTableToMarkdown. There is no default export.

gfmTableFromMarkdown

gfmTableToMarkdown(options?)

Support GFM 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

Keywords

FAQs

Last updated on 31 Jan 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc