The Render Markdown Action.

Credit for the Python code used in this sequence goes to Federico Viticci. Mine is modified to work through Drafts, but you can find his code and more about Pythonista in his review of Pythonista 1.2.

This is an advanced action.

Required Apps: Drafts, Notesy, and Pythonista.

The Render Markdown Action will take Markdown that you have written as a draft in Drafts and convert it to HTML, then it can send it to a variety of places, which you can specify.

Here's the full explanation of what's going on:
Running the renderMarkdown action first takes the text you have written as a draft in Markdown format and sends it to Notesy using the Notesy URL scheme. Notesy receives the Markdown and automatically converts it to HTML, then copies it to the clipboard. Since Drafts cannot (yet) create a new draft with text from the clipboard, we send it instead to Pythonista, which does have access to the clipboard. Pythonista runs a super simple Python script to create a Drafts URL action with the HTML from the clipboard as the text, and follows the URL. Drafts is reopened and creates a new draft prepopulated with your HTML.

Here's where your options come into play. For my workflow, I need to send the HTML to an email address which exists specifically to recieve documents in HTML and post them to my website. For this, I have designed an email action to create a new email message with the recipient address filled in with the necessary email address to post my HTML. Drafts automatically fills in the body of the message with the HTML itself. The only thing I do is type in the subject of the message and hit send.

If this isn't what you want to do with your Markdown, you can change the final Drafts URL in Pythonista to have Drafts run a different action and do something else with your HTML. Or you can leave the action part of the URL blank, and just end by opening a new draft with the HTML in it, and do what you want manually from there.

Importing

Importing the entirety of this action automatically is impossible, as Pythonista does not currently support importing scripts. In other words, you can import all the parts of this action sequence that are run through Drafts, but you'll have to copy the code for the Python script and waste it into a new script in Pythonista yourself.

Direct Import link for renderMarkdown. This is the initial action to initiate the sequence. When you are ready to convert your Markdown, this is the action you will run, everything else is run automatically.

Direct Import link for postbyMail The postbyMail action is the optional one at the end of the sequence. postbyMail will automatically open an email message with the body prepopulated with your HTML code and the recipient an email address of your choice. You just fill in the subject and hit send and you're done. If this isn't what you want to do with your HTML, see below for how to change it.
Important: postbyMail will not send to the email you want unless you open the action in drafts and change the default fake email address I have put in to the address you want to send to.

The Python Script

As I stated above, there is no way to automatically import a Python script into Pythonista. Instead, copy and paste the code below into a new script in Pythonista, and be sure to name it "renderHTML" (case sensitive) in order for it to be run by renderMarkdown. (If you feel inclined to name it something different, you can do that as long as you then change the name at the end of the renderMarkdown action to the same thing, but names don't matter much when the script is run automatically so it seems a bit pointless.)


import webbrowser
import urllib
import clipboard

base = 'drafts://x-callback-url/create?text='
url = clipboard.get()
text = url.encode('utf-8')
text = urllib.quote(text, safe='')
actions = '&action=postbyMail'
webbrowser.open(base + text + actions)

Don't want to send your HTML by email?

Well now you have a few options. What do you want to do with your HTML? If you want Drafts to perform a different action on it when it finishes being generated, then change the part of the Python script above that says "postbyMail" to whatever the name of your action is. As long as the action exists within your Drafts app, it should run as soon as Drafts reopens. (Make sure you leave the "&action=" part alone, and keep the whole thing within the 'apostrophes' without extra spaces.) (As an example, in Federico Viticci's version of this action, he sends his Markdown to Poster at the end of the sequence.)

Second option:

If all you want to do with your Markdown is have it converted to HTML and copied to the clipboard so you can do whatever you want with it, you can actually completely skip the part that requires Pythonista! It can then all be contained within one single Drafts action: The Simple Render Markdown Action.

Post to ADN