Developer Guide
Convert Markdown to HTML in JavaScript
Convert Markdown to HTML in JavaScript with the marked library. Install it, then call marked.parse() with a Markdown string to get an HTML string back. markdown-it is an equally capable alternative with a plugin architecture.
Key facts
- Primary library
- marked
- Core call
- marked.parse(markdown)
- GitHub Flavored Markdown
- enabled by default
- Output sanitization
- none built in — pair with DOMPurify
How do you convert Markdown to HTML in JavaScript?
Install the marked library, then call marked.parse() with a Markdown string. It returns an HTML string synchronously. marked is the most widely used JavaScript Markdown parser and needs no configuration for standard conversions.
Install marked from npm with npm install marked. For the methods overview across languages and tools, see How to Convert Markdown to HTML.
import { marked } from "marked";
const html = marked.parse("# Hello");
// "<h1>Hello</h1>"<h1>Hello</h1>How do you convert Markdown in the browser versus Node?
The API is identical in both. In Node, import marked from the installed package. In the browser, load it through a bundler or a CDN module, then call marked.parse() exactly the same way.
marked ships as an ES module, so a single import works in modern bundlers and browsers. No separate browser build is required.
<script type="module">
import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
document.body.innerHTML = marked.parse("# Hello");
</script>How do you enable GitHub Flavored Markdown?
marked enables GitHub Flavored Markdown by default. Tables, task lists, strikethrough, and fenced code blocks convert with no extra options. Pass { gfm: false } only if you need strict CommonMark instead.
This default differs from some parsers that require opt-in flags. For the full GFM feature set, see GitHub Flavored Markdown to HTML.
marked.parse("- [x] done\n- [ ] todo");
// task-list HTML with checkbox inputsHow does markdown-it compare?
markdown-it is an alternative parser built around plugins. Create an instance with markdownit(), then call its render() method with a Markdown string. It returns HTML and adds features through a plugin API.
Choose marked for a minimal default setup, or markdown-it when you need plugin extensibility such as custom containers or footnotes.
import markdownit from "markdown-it";
const md = markdownit();
const html = md.render("# Hello");<h1>Hello</h1>Frequently Asked Questions
- Should I use marked or markdown-it?
- Both convert Markdown to HTML reliably. marked has GitHub Flavored Markdown on by default and a smaller API; markdown-it follows CommonMark strictly and extends through plugins. Pick by whether you need plugin extensibility.
- Does marked run in the browser?
- Yes. marked is dependency-free and runs in the browser, in Node, and in edge runtimes. Import it through a bundler or a CDN ES module and call marked.parse() the same way everywhere.
- How do I sanitize the HTML output?
- marked does not sanitize output, so converting untrusted Markdown can introduce XSS. Run the HTML through DOMPurify before inserting it into the page when the Markdown comes from users.
- Is marked.parse synchronous?
- Yes, by default marked.parse() returns an HTML string synchronously. It only returns a promise if you register an asynchronous extension, which standard conversions do not use.