HTML To Markdown
In addition to containing parser the convert Markdown to HTML, Drafts also contains tools to convert HTML back into Markdown, which are used in a number of different context in the app.
Table of Contents
- About HTML to Markdown Conversion
- Paste HTML as Markdown
- “Paste as…” Actions
- Scripting HTML to Markdown
About HTML to Markdown Conversion
Markdown was designed as a text-to-HTML conversion tool, but occassionally it’s nice to be able to convert HTML back into Markdown. Not all HTML tags can be converted, but at least many common basic tags like headings, lists, emphasis, etc., can be restored from HTML. Drafts includes the html2text library to allow such conversions, and it can be used in several ways, as described below.
Paste HTML as Markdown
The system clipboard on iOS and macOS can contain more than one type of information. In many apps (such as most web browsers), when you copy to the clipboard both the HTML and plain text version of the information is put in the clipboard.
With “Paste HTML as Markdown” enabled, Drafts will prefer the HTML content, if available, convert it to Markdown, and paste the Markdown instead of the plain text. This is useful to preserve links, emphasis, and other common HTML style information.
There are several options available in Drafts Settings > HTML to configure output of the Markdown conversion:
- Strong Mark: Characters to use to replace
<b>tags, should be either
__to generate valid Markdown.
- Emphasis Mark: Character to use to replace
<i>tags, should be either
_to generate valid Markdown.
- List Mark: Prefered Markdown list character. Typically
*. Will be used replacing
<li>tags in unordered lists.
- Inline links: If enabled, use
[title](url)format for conversion of
<a>tags. If disabled, use reference style links adding the URL at the end of the text.
- Links after each paragraph: When inline links is disabled, this option controls whether the reference style link URLs should be inserted after the paragraph where they appear, or at the end of the document.
- Ignore links: Do not convert link tags.
- Ignore images: Do not include image tags.
- Ignore emphasis: Do not convert emphasis tags.
“Paste as…” Actions
Regardless of the default setting for pasting, as discussed above, you can force use of HTML or Plain Text via actions:
- Paste as Plain Text: Paste contents of clipboard as plain text only.
- Paste as Markdown: If HTML content is available in clipboard, convert it to Markdown and paste it in the currrent draft. Falls back to plain text content if HTML is not available.
Scripting HTML to Markdown
Conversion of HTML to Markdown can also be done via scripting. For more information and example code, see the
HTMLToMarkdown object documentation.