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:
-
Lorem ipsum dolor: Nam nibh. Nunc varius facilisis eros. Sed erat. In in velit quis arcu ornare laoreet. Curabitur adipiscing luctus massa.
-
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.