small commonmark compliant markdown parser with positional info and concrete tokens
npm install micromark[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Size][bundle-size-badge]][bundle-size]
[![Sponsors][sponsors-badge]][opencollective]
[![Backers][backers-badge]][opencollective]
[![Chat][chat-badge]][chat]
Markdown parser.
> Note: this is the micromark package from the micromark monorepo.
> See the [monorepo readme][micromark] for more on the project.
> See this readme for how to use it.
* [x] [compliant][commonmark] (100% to CommonMark)
* [x] [extensions][] (100% [GFM][], 100% [MDX.js][mdxjs], [directives][],
[frontmatter][], [math][])
* [x] [safe][security] (by default)
* [x] [robust][test] (±2k tests, 100% coverage, fuzz testing)
* [x] [small][size-debug] (smallest CM parser at ±14kb)
* When should I use this?
* What is this?
* Install
* Use
* API
* [micromark(value[, encoding][, options])](#micromarkvalue-encoding-options)
* stream(options?)
* Options
* Types
* Compatibility
* Security
* Contribute
* Sponsor
* License
If you just* want to turn markdown into HTML (with maybe a few extensions)
If you want to do really complex things* with markdown
See [§ Comparison][comparison] for more info
micromark is an open source markdown parser written in JavaScript.
It’s implemented as a state machine that emits concrete tokens, so that every
byte is accounted for, with positional info.
It then compiles those tokens directly to HTML, but other tools can take the
data and for example build an AST which is easier to work with
([mdast-util-to-markdown][mdast-util-to-markdown]).
While most markdown parsers work towards compliancy with CommonMark (or GFM),
this project goes further by following how the reference parsers (cmark,cmark-gfm) work, which is confirmed with thousands of extra tests.
Other than CommonMark and GFM, micromark also supports common extensions to
markdown such as MDX, math, and frontmatter.
These npm packages have a sibling project in Rust:
[markdown-rs][markdown-rs].
* to learn markdown, see this [cheatsheet and tutorial][cheat]
* for more about us, see [unifiedjs.com][site]
* for questions, see [Discussions][chat]
* to help, see [contribute][] and [sponsor][] below
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install micromark
In Deno with [esm.sh][esmsh]:
`js`
import {micromark} from 'https://esm.sh/micromark@3'
In browsers with [esm.sh][esmsh]:
`html`
Typical use (buffering):
`js
import {micromark} from 'micromark'
console.log(micromark('## Hello, world!'))
`
Yields:
`html`Hello, world!
You can pass extensions (in this case [micromark-extension-gfm][gfm]):
`js
import {micromark} from 'micromark'
import {gfmHtml, gfm} from 'micromark-extension-gfm'
const value = '* [x] contact@example.com ~~strikethrough~~'
const result = micromark(value, {
extensions: [gfm()],
htmlExtensions: [gfmHtml()]
})
console.log(result)
`
Yields:
`html`strikethrough
Streaming interface:
`js
import {createReadStream} from 'node:fs'
import {stream} from 'micromark/stream'
createReadStream('example.md')
.on('error', handleError)
.pipe(stream())
.pipe(process.stdout)
function handleError(error) {
// Handle your error here!
throw error
}
`
micromark core has two entries in its export map: micromark andmicromark/stream.
micromark exports the identifier [micromark][api-micromark].micromark/stream exports the identifier [stream][api-stream].
There are no default exports.
The export map supports the [development condition][development].node --conditions development module.js
Run to get instrumented dev code.
Without this condition, production code is loaded.
See [§ Size & debug][size-debug] for more info.
Compile markdown to HTML.
> Note: which encodings are supported depends on the engine.
> For info on Node.js, see [WHATWG supported encodings][encoding].
###### Parameters
* value (string or [Uint8Array][uint8-array])encoding
— markdown to parse
* (string, default: 'utf8')value
— [character encoding][encoding] to understand as when it’s aUint8Array
[][uint8-array]options
* ([Options][api-options], optional)
— configuration
###### Returns
Compiled HTML (string).
Create a duplex (readable and writable) stream.
Some of the work to parse markdown can be done streaming, but in the
end buffering is required.
micromark does not handle errors for you, so you must handle errors on whatever
streams you pipe into it.
As markdown does not know errors, micromark itself does not emit errors.
###### Parameters
* options ([Options][api-options], optional)
— configuration
###### Returns
Duplex stream.
Configuration (TypeScript type).
##### Fields
###### allowDangerousHtml
Whether to allow (dangerous) HTML (boolean, default: false).
The default is false, which still parses the HTML according to CommonMark
but shows the HTML as text instead of as elements.
Pass true for trusted content to get actual HTML elements.
See [§ Security][security].
###### allowDangerousProtocol
Whether to allow dangerous protocols in links and images (boolean, default:false).
The default is false, which drops URLs in links and images that use dangerous
protocols.
Pass true for trusted content to support all protocols.
URLs that have no protocol (which means it’s relative to the current page, such
as ./some/page.html) and URLs that have a safe protocol (for images: http,https; for links: http, https, irc, ircs, mailto, xmpp), are
safe.
All other URLs are dangerous and dropped.
See [§ Security][security].
###### defaultLineEnding
Default line ending to use when compiling to HTML, for line endings not in
value ('\r', '\n', or '\r\n'; default: first line ending or '\n').
Generally, micromark copies line endings (\r, \n, \r\n) in the markdown> a
document over to the compiled HTML.
In some cases, such as , CommonMark requires that extra line endings are
added:
\na
\n
.To create that line ending, the document is checked for the first line ending
that is used.
If there is no line ending,
defaultLineEnding is used.
If that isn’t configured, \n is used.######
extensionsArray of syntax extensions (
Array, default: []).
See [§ Extensions][extensions].######
htmlExtensionsArray of syntax extensions (
Array, default: []).
See [§ Extensions][extensions].Types
This package is fully typed with [TypeScript][].
It exports the additional type [
Options][api-options].Compatibility
Projects maintained by the unified collective are compatible with maintained
versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line,
micromark@4, compatible
with Node.js 16.Security
This package is safe.
See [
security.md][securitymd] in [micromark/.github][health] for how to
submit a security report.Contribute
See [
contributing.md][contributing] in [micromark/.github][health] for ways
to get started.
See [support.md`][support] for ways to get help.This project has a [code of conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.
Support this effort and give back by sponsoring on [OpenCollective][]!
Salesforce 🏅 ![]() | |||||||||
Vercel | Motif | HashiCorp | GitBook | Gatsby | |||||
Netlify ![]() | Coinbase | ThemeIsle | Expo | Boost Note ![]() | Markdown Space ![]() | Holloway | |||
You? | |||||||||
[MIT][license] © [Titus Wormer][author]
[api-micromark]: #micromarkvalue-encoding-options
[api-options]: #options
[api-stream]: #streamoptions
[author]: https://wooorm.com
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[build]: https://github.com/micromark/micromark/actions
[build-badge]: https://github.com/micromark/micromark/workflows/main/badge.svg
[bundle-size]: https://bundlejs.com/?q=micromark
[bundle-size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=micromark
[chat]: https://github.com/micromark/micromark/discussions
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[cheat]: https://commonmark.org/help/
[coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md
[commonmark]: https://commonmark.org
[comparison]: https://github.com/micromark/micromark#comparison
[contribute]: #contribute
[contributing]: https://github.com/micromark/.github/blob/main/contributing.md
[coverage]: https://codecov.io/github/micromark/micromark
[coverage-badge]: https://img.shields.io/codecov/c/github/micromark/micromark.svg
[development]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
[directives]: https://github.com/micromark/micromark-extension-directive
[downloads]: https://www.npmjs.com/package/micromark
[downloads-badge]: https://img.shields.io/npm/dm/micromark.svg
[encoding]: https://nodejs.org/api/util.html#whatwg-supported-encodings
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[extensions]: https://github.com/micromark/micromark#extensions
[frontmatter]: https://github.com/micromark/micromark-extension-frontmatter
[gfm]: https://github.com/micromark/micromark-extension-gfm
[health]: https://github.com/micromark/.github
[license]: https://github.com/micromark/micromark/blob/main/license
[markdown-rs]: https://github.com/wooorm/markdown-rs
[math]: https://github.com/micromark/micromark-extension-math
[mdast-util-to-markdown]: https://github.com/syntax-tree/mdast-util-to-markdown
[mdxjs]: https://github.com/micromark/micromark-extension-mdxjs
[micromark]: https://github.com/micromark/micromark
[npm]: https://docs.npmjs.com/cli/install
[opencollective]: https://opencollective.com/unified
[security]: #security
[securitymd]: https://github.com/micromark/.github/blob/main/security.md
[site]: https://unifiedjs.com
[size-debug]: https://github.com/micromark/micromark#size--debug
[sponsor]: #sponsor
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[support]: https://github.com/micromark/.github/blob/main/support.md
[test]: https://github.com/micromark/micromark#test
[typescript]: https://www.typescriptlang.org
[uint8-array]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array