Convert one flavor of Markdown header to another

Pandoc is a good free and flexible tool for this kind of thing.

Here, for example, I have installed pandoc

https://pandoc.org/installing.html

checked its install location by entering the command which pandoc in the shell, and copied the path to a KM variable,

27

Paste MD as setx italic.kmmacros (18.8 KB)

Untitled

and then used the following incantation, which you could adjust or fine-tune,

Using the shell to:

  1. read the clipboard -from markdown_mmd -to html with pandoc
  2. search replace html emphasis tags to temporary nonsense strings
  3. read the result -from html -to markdown_strict with pandoc (for setext headings)
  4. search replace the temporary nonsense strings to underscores
"$KMVAR_pandoc" -f markdown_mmd -t html | sed 's|<em>|~~start~~|g' | sed 's|</em>|~~end~~|g' | "$KMVAR_pandoc" -f html -t markdown_strict | sed 's|~~start~~|_|g' | sed 's|~~end~~|_|g'
1 Like