4

I have an Automator service that takes selected text and saves it in a file. However, bold, italics, and linked URLS aren't preserved. Is there a way I can convert it to Markdown before I save it? When I searched Google, I only found guides to convert plain text to HTML.

3 Answers 3

5

HTML to Markdown

Brett Terpstra has written these automator services to help manipulate markdown in OSX.

One of these services does convert HTML to Markdown (which answers one part of your question), and any of these services can be individually added and assigned a shortcut if you so please. I myself use Markdown to RTF to send pretty emails written in md.

However, none of the services seem to do RTF to Markdown. And so deeper into the rabbit hole of RTF clipboard extraction I went...

RTF to Markdown

First I found this post and this SO question. The key, I found, is to use OSX built-in pbpaste (or osascript) and textutil.

And so, I've written a simple RTF to Markdown service for anyone who is still looking to compliment Brett's services mentioned above, it simply converts input to HTML and pipes result to the "HTML to Markdown" service.

Note: RTF HTML output is not always what you are looking at because of how the style is converted, so I would go with HTML to Markdown where possible.

2
0

Mark It Down - XSLT transform

I don't know if it's exactly what you wanted, but I found the in browser tool Mark It Down and its source code:

The conversion happens entirely on the client (in the browser) using Saxon-CE and an XSLT.

4
  • 1
    How would you integrate this into an Automator workflow?
    – nohillside
    Sep 22, 2014 at 18:42
  • @GrahamMiln Thanks for fixing the links. It doesn't address the fundamental issue with this answer though (integrating this service into an Automator workflow), Any idea on how to solve this?
    – nohillside
    Sep 18, 2019 at 9:06
  • One approach would be to use /usr/bin/xsltproc with the XSLT files in the above project. An Automator workflow could pass the input file to xsltproc and either pipe out the results, or save the output to a folder. Sep 18, 2019 at 9:25
  • FYI, the Mark it Down extension has been put down by its developer.
    – Mockman
    Jun 15, 2021 at 0:10
-1

The clipboard in HTML format is included for text copied from from Safari, Chrome, NetNewsWire, and Preview, but Skim and Pages only include the clipboard in RTF format. And Chrome only includes the clipboard in HTML format but not RTF format. So this first tries to get the cliboard as HTML, and if it's not available then it converts the RTF clipboard to HTML instead:

# pbpaste Markdown
ppm()(pph|hm|dumb)

# pbpaste HTML
pph()(x=$(osascript -e'get the clipboard as"HTML"' 2>/dev/null);if [[ $? = 0 ]];then sed 's/«data HTML//;s/»//'<<<"$x"|xxd -r -p;else ppr;fi)
ppr()(osascript -e'get the clipboard as"RTF "'|sed 's/«data RTF //;s/»//'|xxd -r -p|textutil -convert html -stdin -stdout)

# HTML to Markdown (`markdown_strict` uses `markdown.pl` syntax without Pandoc extensions)
hm()(pandoc -f html -t markdown_strict --no-wrap --atx-headers|sed 's/\\\$/\$/g')

# convert non-ASCII characters to ASCII
dumb0()(sed $'s/[\u201c\u201d]/"/g;s/[\u2018\u2019\u2032]/\'/g;s/\u2026/.../g;s/[\u2010\uad\u2011\u2013]/-/g;s/\ua0/ /g;s/\u2010/-/g')
dumb()(dumb0|sed -E $'s/ ?\u2013 ?/-/;s/ ?\u2014 ?/ - /g')
1
  • How does this code need to be run? How would it work within an Automator workflow?
    – nohillside
    Dec 14, 2023 at 13:50

You must log in to answer this question.