remark-gfm

repository·main·Indexed 22 days ago

https://github.com/remarkjs/remark-gfm

A unified/remark plugin that adds support for GitHub Flavored Markdown (GFM) extensions, including autolink literals, footnotes, strikethrough, tables, and tasklists. Version 4.0.1 is ESM only and supports Node.js 16+.

Tokens
1.1K
Snippets
4
Records
6
Agent score
29%

What's inside remark-gfm

  1. Install remark-gfm

    main

    Install remark-gfm via npm for Node.js (version 16+). Note that this package is ESM only.

    In Deno, use esm.sh:

    import remarkGfm from 'https://esm.sh/remark-gfm@4'

    In browsers, use esm.sh with the ?bundle query parameter:

    <script type="module">
      import remarkGfm from 'https://esm.sh/remark-gfm@4?bundle'
    </script>
    npm install remark-gfm
  2. Disable single tilde strikethrough

    main

    By default, remark-gfm supports strikethrough with a single tilde (~text~) to match GitHub's behavior. To disable this and only support the standard GFM double tilde (~~text~~), set singleTilde: false in the options.

    const file = await unified()
      .use(remarkParse)
      .use(remarkGfm, {singleTilde: false})
      .use(remarkRehype)
      .use(rehypeStringify)
      .process('~one~ and ~~two~~')
    
    console.log(String(file)) // Yields: <p>~one~ and <del>two</del></p>
  3. Use remark-gfm in a unified pipeline

    main

    To enable GitHub Flavored Markdown (GFM) support—including autolink literals, footnotes, strikethrough, tables, and tasklists—add remarkGfm to your unified processor pipeline using .use(remarkGfm).

    This plugin handles parsing and serializing GFM syntax but does not handle the conversion to HTML; that is typically performed by remark-rehype and rehype-stringify.

    import rehypeStringify from 'rehype-stringify'
    import remarkGfm from 'remark-gfm'
    import remarkParse from 'remark-parse'
    import remarkRehype from 'remark-rehype'
    import {read} from 'to-vfile'
    import {unified} from 'unified'
    
    const file = await unified()
      .use(remarkParse)
      .use(remarkGfm)
      .use(remarkRehype)
      .use(rehypeStringify)
      .process(await read('example.md'))
    
    console.log(String(file))
  4. Align tables using stringLength

    main

    When formatting markdown tables, remark-gfm uses the stringLength option to determine the visual width of cells. This is useful for correctly aligning tables containing full-width characters (like Chinese characters) or emojis, which standard .length properties may miscalculate.

    Pass a function like string-width to the stringLength option to ensure accurate alignment.

    import {remark} from 'remark'
    import remarkGfm from 'remark-gfm'
    import stringWidth from 'string-width'
    
    const input = `| Alpha | Bravo |
    | - | - |
    | 中文 | Charlie |
    | 👩‍❤️‍👩 | Delta |`
    
    const file = await remark()
      .use(remarkGfm, {stringLength: stringWidth})
      .process(input)
    
    console.log(String(file))
  5. Configure remark-gfm options

    main

    The remarkGfm plugin accepts an optional options object to configure its behavior.

    FieldTypeDefaultDescription
    firstLineBlankbooleanfalseSerialize with a blank line for the first line of footnote definitions
    stringLength(value: string) => numberd => d.lengthDetect the size of table cells, used when aligning cells
    singleTildebooleantrueWhether to support strikethrough with a single tilde (e.g., ~text~). GitHub supports this, but it is technically prohibited by GFM spec
    tablePipeAlignbooleantrueWhether to align table pipes
    tableCellPaddingbooleantrueWhether to add a space of padding between table pipes and cells
  6. Use remark-gfm as a plugin

    main

    The remarkGfm plugin adds support for GitHub Flavored Markdown (GFM) extensions to a unified or remark processor. This includes support for:

    • Autolink literals
    • Footnotes
    • Strikethrough
    • Tables
    • Tasklists

    To use it, import the default export and pass it to the .use() method of your processor. You can optionally provide a configuration object.