Convert one flavor of Markdown header to another

I'm trying to figure out a way to convert this type of Markdown header:

# Sample Header

to this type:

Sample Header
====

Backstory: My firm's case management app uses underscores instead of asterisks for bold/italics, and also use equal signs (====) under headers, instead of a hashtag before them.

I take notes in Agenda, which has a "Copy to Markdown" feature. I have a KM macro that, when activated with a keystroke, will delete the first line of the current Agenda note (my client name, which I don't need in the client file), copy the note as Markdown, replace all the "*" with "_", and paste into my case management system. (I brute force the pasting step, using a found image to put it at the correct spot).

The final piece of the puzzle is to convert the headers to the correct format. I've looked at and played around with regular expressions, but this is beyond my limited ability. If there's anyone that can help, I'd be really grateful.

Here is what I have so far:

Keystroke - Convert clipboard to FV markdown.kmmacros (21.5 KB)

Thank you!!

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

Thanks for bringing Pandoc onto my radar. Looks interesting, and powerful. I dove into the Pandoc site a bit - enough to learn that I'm looking to convert atx headers into setext headers. The actual syntax to do that, though, looks like it has a pretty steep learning curve.

The example above doesn't do that in the way you need ?

( markdown_strict uses setext for the top two levels. Is setext defined for levels lower than that ? )

Actually, my bad - it does. Thank you! I was testing it going from Agenda, and it isn't working correctly from there. But I just tested it from a plain text file, and it worked, so there must be something wonky going on in Agenda that I need to troubleshoot.

Thank you!!!

1 Like

I just took ComplexPoint's generous work and integrated it into the macro, and all is working well. I put in my steps to convert the asterisk flavor Markdown to underscore, and the final brute force steps to paste into my case management system. All this happens with one keystroke, as long as my cursor is somewhere in the desired note in Agenda. Thank you again to ComplexPoint for this.

On the chance this might help someone else, here is the entire macro:

Keystroke - Convert clipboard to FV markdown.kmmacros (22.6 KB)

Somewhat of an unrelated question, but any idea why the lines might be getting truncated? In the attached screenshot, if I type in text manually, the "This is line 3" text will go all the way to the right edge. Using the macro, it appears some hard or soft returns are inserted into the text. If I try remove the line returns using search and replace in the macro, it removes them from everything.

Only double line-feeds are syntactically noticed by Markdown to HTML generators, so the line-wrapping done by pandoc may not matter much, but if you like you can switch it off on the pandoc command line:

--wrap=none
or
--wrap=preserve

( see the --wrap option in https://pandoc.org/MANUAL.html )

1 Like

Thank you. That did the trick. The final syntax of the "Execute Shell Script" step ended up as follows, for anyone wondering:

"$KMVAR_pandoc" -f markdown_mmd -t html | sed 's|<em>|~~start~~|g' | sed 's|</em>|~~end~~|g' | "$KMVAR_pandoc" -f html -t markdown_strict --wrap=none | sed 's|~~start~~|_|g' | sed 's|~~end~~|_|g'
1 Like

2 posts were split to a new topic: Agenda -- Note Taking App for Projects