Trouble removing Markdown double asterisks and converting to rich bold text

I regularly get text that is in Markdown, wrapped in double asterisks and I’d like to be able to remove the double asterisks and change it to regular bold rich text using a Keyboard Maestro macro. The text I get looks like:

  1. Lorem ipsum dolor: Nam nibh. Nunc varius facilisis eros. Sed erat. In in velit quis arcu ornare laoreet. Curabitur adipiscing luctus massa.

  2. Integer ut purus: Nunc nec mi eu justo tempor consectetuer. Etiam vitae nisl. In dignissim lacus ut ante. Cras elit lectus, bibendum a, adipiscing vitae, commodo et, dui.

I don’t really do anything much with Markdown and I know people have talked about Pandoc, but I was looking for a way to just do this natively in Keyboard Maestro with some scripting or Regular Expressions. The basic macro is:

"Copy to Clipboard"
"Run Script" or [Apply Style to System Clipboard and then Search and Replace System Clipboard Using *]
"Paste Clipboard"

I feel like this should be straightforward but for the life of me I can’t figure it out.

I’ve tried a "Run Shell Script" action using the following but it didn’t work:

Get clipboard text

text=$(pbpaste)

Replace ** with RTF bold tags

rtf=$(echo "$text" | sed 's/\\\(.\)\\*/{\\rtf1\\b \1\\b0}/g')

Set clipboard to RTF text

echo "$rtf" | pbcopy -Prefer rtf

And I tried an AppleScript, but it didn’t work either:

set text to clipboard as «class ttxt»
set delimiters to {"**"}
set textItems to text items of text

repeat with i from 1 to count textItems
if i mod 2 is 0 then
set textItem i of textItems to "" & textItem i of textItems & ""
end if
end repeat

set richText to textItems as text
set the clipboard to richText as «class RTF »

Any ideas on how to do this? Please excuse if this has already been addressed. I couldn't find it on the forum.

Pandoc.

Much simpler than what you are trying.

Thanks! I might have to just try that...

After installing pandoc, for example with something like brew install pandoc at the Terminal.app prompt, you will need, in a KM shell script action, the full path to the pandoc executable.

(In Terminal.app again, which pandoc)

then, depending on the details of your workflow, you can obtain a variety of rich text formats (html, rtf, etc etc)


or perhaps, to create an RTF clipboard:

Wow, thanks for your help with that. I’ll have to give Pandoc a look.

1 Like