markdown-wasm
Very fast Markdown parser & HTML renderer implemented in WebAssembly
- Zero dependencies (31 kB gzipped)
- Portable & safe (WASM executes in isolated memory and can run almost anywhere)
- Simple API
- Very fast
- Based on md4c — compliant to the CommonMark specification
Examples
In NodeJS, single file with embedded compressed WASM
const markdown = require("./dist/markdown.node.js")
console.log(markdown.parse("# hello\n*world*"))
ES module with WASM loaded separately
import * as markdown from "./dist/markdown.es.js"
await markdown.ready
console.log(markdown.parse("# hello\n*world*"))
Web browser
<script src="markdown.js"></script>
<script>
window["markdown"].ready.then(markdown => {
console.log(markdown.parse("# hello\n*world*"))
})
</script>
Install
npm install markdown-wasm
Benchmarks
The test/benchmark
directory contain a benchmark suite which you can
run yourself. It tests a few popular markdown parser-renderers by parsing & rendering a bunch
of different sample markdown files.
The following results were samples on a 2.9 GHz MacBook running macOS 10.15, NodeJS v14.11.0
Average ops/second
Ops/second represents how many times a library is able to parse markdown and render HTML
during a second, on average across all sample files.
Average throughput
Throughput is the average amount of markdown data processed during a second while both parsing
and rendering to HTML. The statistics does not include HTML generated but only bytes of markdown
source text parsed.
Min–max parse time
This graph shows the spread between the fastest and slowest parse-and-render operations
for each library. Lower numbers are better.
See test/benchmark
for more information.
API
export function parse(s :Source, o? :ParseOptions & { asMemoryView? :never|false }) :string
export function parse(s :Source, o? :ParseOptions & { asMemoryView :true }) :Uint8Array
type Source = string|ArrayLike<number>
export interface ParseOptions {
parseFlags? :ParseFlags
asMemoryView? :boolean
}
export enum ParseFlags {
COLLAPSE_WHITESPACE,
LATEX_MATH_SPANS,
NO_HTML_BLOCKS,
NO_HTML_SPANS,
NO_INDENTED_CODE_BLOCKS,
PERMISSIVE_ATX_HEADERS,
PERMISSIVE_EMAIL_AUTO_LINKS,
PERMISSIVE_URL_AUTO_LINKS,
PERMISSIVE_WWW_AUTOLINKS,
STRIKETHROUGH,
TABLES,
TASK_LISTS,
WIKI_LINKS,
DEFAULT,
NO_HTML,
}
See markdown.d.ts
Building from source
npm install
npx wasmc
Build debug version of markdown into ./build/debug and watch source files:
npx wasmc -g -w
If you need special builds, like for example an ES module with embedded WASM,
edit the wasmc.js
file and add module({...})
directives.
Example:
module({ ...m,
name: "markdown-custom",
out: outdir + "/markdown.custom.js",
embed: true,
format: "es",
})