Convert HTML hyperlink code to Markdown (MD) in pure AppleScript? 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: &lt;a href=&quot;https://duck.com&quot;&gt;link&lt;/a&gt;
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.
Here is one example:
set htmlString to &quot;This is a link: &lt;a href=\&quot;https://duck.com\&quot;&gt;link&lt;/a&gt;&quot;

set mdString to do shell script &quot;/usr/bin/sed -E -e 's|&lt;a href=\&quot;|[link](|g' -e 's|\&quot;&gt;link&lt;/a&gt;|)|g' &lt;&lt;&lt; &quot; &amp; htmlString's quoted form

Result:
&quot;This is a link: [link](https://duck.com)&quot;

This can also be down without the use of the do shell script command, as in this example:
set htmlString to &quot;This is a link: &lt;a href=\&quot;https://duck.com\&quot;&gt;link&lt;/a&gt;&quot;

set htmlString to findAndReplaceInText(htmlString, &quot;&lt;a href=\&quot;&quot;, &quot;[link](&quot;)
set htmlString to findAndReplaceInText(htmlString, &quot;\&quot;&gt;link&lt;/a&gt;&quot;, &quot;)&quot;)

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 &quot;&quot;
    return theText
end findAndReplaceInText

Result:
&quot;This is a link: [link](https://duck.com)&quot;


If This is a link: &lt;a href=&quot;https://duck.com&quot;&gt;link&lt;/a&gt; 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 &quot; in the sed command as shown in the example above.

Other examples:
If This is a link: &lt;a href=&quot;https://duck.com&quot;&gt;link&lt;/a&gt; in in a file:
set htmlFile to &quot;/path/to/filename/ext&quot;
set htmlString to read htmlFile
set mdString to do shell script &quot;/usr/bin/sed -E -e 's|&lt;a href=\&quot;|[link](|g' -e 's|\&quot;&gt;link&lt;/a&gt;|)|g' &lt;&lt;&lt; &quot; &amp; htmlString's quoted form

Or, processing the file directly:
set htmlFile to &quot;/path/to/filename.ext&quot;
set mdString to do shell script &quot;/usr/bin/sed -E -e 's|&lt;a href=\&quot;|[link](|g' -e 's|\&quot;&gt;link&lt;/a&gt;|)|g'&quot; &amp; space &amp; htmlFile's quoted form


If This is a link: &lt;a href=&quot;https://duck.com&quot;&gt;link&lt;/a&gt; is on the clipboard:
set htmlString to (the clipboard as text)
set mdString to do shell script &quot;/usr/bin/sed -E -e 's|&lt;a href=\&quot;|[link](|g' -e 's|\&quot;&gt;link&lt;/a&gt;|)|g' &lt;&lt;&lt; &quot; &amp; 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.

See also, Manipulating Text in the Mac Automation Scripting Guide.