HTML to Markdown Converter: how it works and how to build one
Convert HTML into clean Markdown. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Headings, bold, italics, links, images, lists, quotes and code blocks become Markdown. Scripts and styles are dropped. Useful for moving blog posts from WordPress or a CMS into a static site, README or notes app.
When to use it
- Moving WordPress posts to a static site
- Saving web articles as notes
- Writing READMEs from HTML docs
How to use it
- Paste HTML or open a file.
- Check the Markdown output.
- Copy or download it as .md.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/html-to-markdown
How it works
The converter rewrites HTML tags into Markdown syntax with a series of regex replacements: <h2> becomes ##, <strong> becomes **, <a href> becomes [text](url), list items become – or 1., and <pre><code> becomes a fenced code block.
Scripts, styles and comments are removed first, and remaining tags are stripped at the end. For messy real-world HTML, a DOM-based converter like Turndown is more robust.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
htmlToMd.ts (ts)
export const htmlToMarkdown = (html: string) => html
.replace(/<(script|style)[\s\S]*?<\/\1>/gi, "")
.replace(/<h([1-6])[^>]*>([\s\S]*?)<\/h\1>/gi, (_, n, t) => `\n\n${"#".repeat(+n)} ${t}\n\n`)
.replace(/<(strong|b)>([\s\S]*?)<\/\1>/gi, "**$2**")
.replace(/<(em|i)>([\s\S]*?)<\/\1>/gi, "*$2*")
.replace(/<a [^>]*href="([^"]*)"[^>]*>([\s\S]*?)<\/a>/gi, "[$2]($1)")
.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, "- $1\n")
.replace(/<br\s*\/?>/gi, " \n")
.replace(/<\/?(p|div|ul|ol)[^>]*>/gi, "\n\n")
.replace(/<[^>]+>/g, "")
.replace(/\n{3,}/g, "\n\n").trim();
Make it production-ready
- Number ordered list items.
- Convert tables to Markdown tables.
- Decode HTML entities at the end.
Questions
Are tables converted?
Simple tables come out as text. Use the Markdown Table Generator for tables from a spreadsheet.
Free, runs in your browser, nothing is uploaded.