htmlmarkdownpandoc

how to prevent pandoc from removing leading underscore from sections in markdown?


Pandoc when processing markdown sections removes the leading _ when producing html with id tag:

$ pandoc -f markdown - <<<'Go to [_section](#_section)'$'\n\n''### _section'
<p>Go to <a href="#_section">_section</a></p>
<h3 id="section">_section</h3>

The id="section' is wrong, it should id="_section". This breaks the links, href="#_section" navigates to _section not to section.

What can I do to fix?


Solution

  • You'll see that ### _section is converted to <h3 id="section">_section</h3> due to how pandoc normalizes ids (see the manual). What you can do instead is:

    ### \_section {#_section}
    

    which is converted to:

    <h3 id="_section">_section</h3>
    

    (tested in pandoc 3.5)