Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc: HTML-to-Markdown--can I replace elements using templates or scripts?

I'm successfully converting HTML to Markdown, but elements such as <span class="cmd"> are preserved and appear in the MD result.

Is there a way, perhaps by using templates or Pandoc scripting, to replace the <span> element with <strong> or even with asterisks during the HTML-to-Markdown conversion?

For example:

I want to replace

<span class="cmd">This content must be bold</span>

with

<strong>This content must be bold</strong>

or

*This content must be bold*

Thanks very much.

like image 291
Drew Avatar asked Jan 28 '26 01:01

Drew


1 Answers

You could adapt this pandoc filter. Save this as cmd_italics.py and run pandoc myfile.html -o myfile.md -F cmd_italics.py

#!/usr/bin/env python

from pandocfilters import toJSONFilter, Strong


def cmd_italics(key, value, format, meta):
    if key == 'Span':
        [[ident, classes, kvs], contents] = value
        for c in classes:
            if c == "cmd":
                return Strong(contents)

if __name__ == "__main__":
    toJSONFilter(cmd_italics)

You will need the pandocfilter python library installed.

like image 140
scoa Avatar answered Feb 01 '26 00:02

scoa