Search code examples
javascriptmarkdownremarkjs

Changing rehype-remark defaults (eg. emphasis using _ instead of *)


The rehype-remark plugin uses Github Flavored Markdown to parse HTML into Markdown.

In other words, it uses * for italic and ** for bold.

However, I want to use _ for italic, and keep ** for bold. Because using only * is causing errors in my parser, while _ works well.

Is there a plugin or config that changes the markdown default?


Solution

  • Note that the rehyphe-remark plugin doesn't generate the markdown, it transforms the HTML AST (hast) to a markdown AST (mdast).

    The actual markdown is generated by the remark-stringify plugin which by default uses CommonMark (not GFM).

    Markdown is serialized according to CommonMark

    You can use the options to change the syntax of the markdown. For example, to use underscore for emphasized text (italic) instead of star you can set the emphasis property to _.

    Here's their usage example.

    import {unified} from 'unified'
    import rehypeParse from 'rehype-parse'
    import rehypeRemark from 'rehype-remark'
    import remarkStringify from 'remark-stringify'
    
    main()
    
    async function main() {
      const file = await unified()
        .use(rehypeParse)
        .use(rehypeRemark)
        .use(remarkStringify, {
          bullet: '*',
          fence: '~',
          fences: true,
          incrementListMarker: false
        })
        .process('<h1>Hello, world!</h1>')
    
      console.log(String(file))
    }