Macro to copy safari or chrome selection as markdown

I have been using Popclip's CopyAsMarkdown extension to copy Safari / Chrome selection as Markdown formatted text. It works most of the time, but not always.

I wanted to be able to accomplish it in KM.
Managed to get it to work using textual and Pandoc.
Macro attached.

However, would like be to use html2text.py written by Aaron Swartz.
The Pandoc solution does not include links to the original image on the webpage where the portion of the webpage is selected. Also the formatting of the resulting markdown document is off.
The html2text script is what Popclip's extension uses and it works very well.
When I tried to use the script, it errors out. :frowning:

Any help in making html2text.py work is welcome. Thanks.

CopySelectionToMarkdown.kmmacros (5.5 KB)

17 PM

Edited a few times to share the latest solution that I have and how it is inadequate.