1

I have a text file or some text to process via the clipboard. It contains a few lines of HTML code, and I'd like to convert it to pure Markdown.

E.g. from:

This is a link: <a href="https://duck.com">link</a>

to:

This is a link: [link](https://duck.com)

Is there pure AppleScript code (using regex substitution) that can do this?

Alternatively, I am happy with AppleScript calling any CLI tool to process the text via do shell script if it's an in-built text which doesn't require homebrew or a third party tool to be installed.

  • 1
    /usr/bin/sed -E -e 's|<a href="|[link](|g' -e 's|">link</a>|)|g' <<< 'This is a link: <a href="https://duck.com">link</a>' produces: This is a link: [link](https://duck.com) – user3439894 Oct 23 '20 at 16:13
  • If not in clipboard but just an XML file then see apple.stackexchange.com/questions/128106/… – mmmmmm Oct 23 '20 at 17:10
  • @mmmmmm yeah I can run that link's "md - Convert - HTML to Markdown" and it does work, but it's python inside automator workflow, is it possible to embed python code in an applescript without having to create a separate .py file? – user163629 Oct 23 '20 at 17:16
1

Here is one example:

set htmlString to "This is a link: <a href=\"https://duck.com\">link</a>"

set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

Result:

"This is a link: [link](https://duck.com)"

This can also be down without the use of the do shell script command, as in this example:

set htmlString to "This is a link: <a href=\"https://duck.com\">link</a>"

set htmlString to findAndReplaceInText(htmlString, "<a href=\"", "[link](")
set htmlString to findAndReplaceInText(htmlString, "\">link</a>", ")")

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Result:

"This is a link: [link](https://duck.com)"

If This is a link: <a href="https://duck.com">link</a> is in a file or on the clipboard, the escaping is done automatically when assigning it to a variable. You then only need to escape the " in the sed command as shown in the example above.


Other examples:

If This is a link: <a href="https://duck.com">link</a> in in a file:

set htmlFile to "/path/to/filename/ext"
set htmlString to read htmlFile
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

Or, processing the file directly:

set htmlFile to "/path/to/filename.ext"
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g'" & space & htmlFile's quoted form

If This is a link: <a href="https://duck.com">link</a> is on the clipboard:

set htmlString to (the clipboard as text)
set mdString to do shell script "/usr/bin/sed -E -e 's|<a href=\"|[link](|g' -e 's|\">link</a>|)|g' <<< " & htmlString's quoted form

Note: The use of the findAndReplaceInText() handler can also be used in place of the do shell script command in these other examples.

  • Superb, thank you! My only comment to anyone is: make sure the input doesn't have other html tags in between the <a> and </a> tags. Substitute that out using other applescript code first, or it won't process fully. – user163629 Oct 23 '20 at 16:58
  • 1
    @ἐλευθερία, You can also extend the sed command with additional substitution commands, e.g.: -e 's|\"><span class=\"s1\">link</span></a>|)|g' – user3439894 Oct 23 '20 at 17:43
  • I was sure this was going to be a bad question on first glance, but an awesome answer turned that around immediately. – bmike Oct 23 '20 at 23:21
  • @user3439894 I've realised your answer only performs this action for that specific hyperlink text example, not for any string as the hyperlink text. This e.g. works for direct text example, can you update the answer? set mdString to do shell script "sed 's/<a href=\"\\(.*\\)\">\\(.*\\)<\\/a>/[\\2](\\1)/' <<< " & htmlString's quoted form – user163629 Oct 24 '20 at 11:01
  • Furthermore, this allows you to convert multiple hyperlinks in the incoming text, not just one: set mdString to do shell script "perl -pe 's/<a.*?href=\"(.*?)\".*?>(.*?)<\\/a>/[\\2](\\1)/g' <<< " & htmlString's quoted form - even more superior. – user163629 Oct 24 '20 at 11:51

You must log in to answer this question.