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.
and then used the following incantation, which you could adjust or fine-tune,
Using the shell to:
read the clipboard -from markdown_mmd-to html with pandoc
search replace html emphasis tags to temporary nonsense strings
read the result -from html-to markdown_strict with pandoc (for setext headings)
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'
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.
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.
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:
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:
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'