Convert HTML back to Markdown

Drafts is very good at displaying and converting Markdown to HTML. Is there a way I could create a note containing simple HTML and get Drafts to convert it to Markdown?

My use case: I keep my calendar in Google. If I put any notes in the Description field that might be considered RTF, e.g. a hyperlink, Google automatically converts the entire Description to basic HTML. If I can convert that back to Markdown it will be human readable again.

Thank you.

Not sure how easy it would be to integrate. I’m aware of the Turndown project that is a pure Javascript HTML > Markdown conversion library which, in theory, could be used in an action if any one is up for trying.

I’ve been using the html2text Python script in Pythonista. I use a Workflow to grab web pages, etc. to bring them into Drafts as Markdown.

If it’s very simple HTML, you could just write your own script to do the replacements.

Nick, I have Pythonista but have never used it. May I see your Workflow and attempt to duplicate your solution? Thank you.

Sure, I’m happy to share what I have. Here’s a screenshot of the Workflow – I’m pretty new to using the app and I’m not sure if there’s a way to share that isn’t public like we can do on the action directory.

Here’s a simple script I created to demonstrate how easy it is.

import html2text
import urllib
import sys
import webbrowser
"""
Simple Web Scraper using html2text
https://github.com/Alir3z4/html2text

Many parameters can be adjusted as needed

Example usage:
pythonista3://webscrape.py?action=run&argv=LINK_GOES_HERE
(or use "Run Script" option in Workflow)
"""
# Read passed-in url and get html
url = sys.argv[1]
with urllib.request.urlopen(url) as response:
  html = response.read().decode('utf-8')

# Set html2text options and get text
h = html2text.HTML2Text()
h.body_width = 0            # Don't wrap text
h.inline_links = False      # Reference-style MD links

md = h.handle(html)

if md:
  mdEscaped = urllib.parse.quote(md)
  xurl = 'drafts5://x-callback-url/create?text=' + mdEscaped
  webbrowser.open(xurl)

Pythonista comes with a version of html2text included, so you can see if this does what you want out of the box. I actually downloaded the current version of the module into site-packages, but I’m not sure how much of a difference there is. I had a specific project for which I needed HTML to Markdown conversion, so I had to make some tweaks.

If you don’t want to pull from a url, you could easily modify the script to accept the HTML directly. I hope this helps!

Nick

2 Likes

Nick, thank you. I will dig in as soon as I can. Take care. Craig

The easiest way to deal with this is probably the workflow app. It has a rich text to markdown action I use for this sort of thing all the time.

Yes I forgot about the Workflow Rich Text to Markdown action. It works for some things, but for my needs, I found html2text to be more customizable. Plus, in Pythonista, I have access to the HTML before and after conversion. I’m sure some of that could be done in Workflow but I’m not very experienced with it.

I love Pythonista though! Along with Drafts it’s one of my most-used apps these days.