This guide walks you through installing remark and running your first markdown transformation. You will start with a markdown-to-markdown pipeline (usingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/remarkjs/remark/llms.txt
Use this file to discover all available pages before exploring further.
remark directly) and then extend it to output HTML by adding remark-rehype and rehype-stringify. Every code snippet here is taken directly from the remark source and is ready to run.
Make sure you are inside a Node.js project (Node.js 16 or later is required). Install the
remark package, which bundles unified, remark-parse, and remark-stringify together.All remark packages are ESM only. Your project must either use
"type": "module" in package.json, or use .mjs file extensions. CommonJS require() is not supported.Create a file called
example.js and paste the following code. It uses remark together with remark-toc to automatically fill in the ## Contents section of a document:import {remark} from 'remark'
import remarkToc from 'remark-toc'
const value = `
# Pluto
Pluto is a dwarf planet in the Kuiper belt.
## Contents
## History
### Discovery
In the 1840s, Urbain Le Verrier used Newtonian mechanics to predict the position of…
### Name and symbol
The name Pluto is for the Roman god of the underworld, from a Greek epithet for Hades…
### Planet X disproved
Once Pluto was found, its faintness and lack of a viewable disc cast doubt…
## Orbit
Pluto's orbital period is about 248 years…
`
const file = await remark().use(remarkToc).process(value)
console.error(String(file))
The
remark() call creates a unified processor that already has remark-parse and remark-stringify wired up. Calling .use(remarkToc) registers the table-of-contents plugin. .process(value) parses the markdown, runs all plugins, and serializes the result back to a markdown string.# Pluto
Pluto is a dwarf planet in the Kuiper belt.
## Contents
* [History](#history)
* [Discovery](#discovery)
* [Name and symbol](#name-and-symbol)
* [Planet X disproved](#planet-x-disproved)
* [Orbit](#orbit)
## History
### Discovery
In the 1840s, Urbain Le Verrier used Newtonian mechanics to predict the position of…
### Name and symbol
The name Pluto is for the Roman god of the underworld, from a Greek epithet for Hades…
### Planet X disproved
Once Pluto was found, its faintness and lack of a viewable disc cast doubt…
## Orbit
Pluto's orbital period is about 248 years…
remark-toc walked the mdast tree, found the heading named “Contents”, and inserted a list of links as its next sibling. The rest of the document was untouched. This is exactly how all remark plugins work — they receive the full AST and can read or mutate any node.Markdown-to-markdown is useful for formatting, but many projects need HTML output. For that you combine
remark-parse with remark-rehype (which converts the mdast tree into an HTML AST) and rehype-stringify (which serializes the HTML AST to a string).import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import {unified} from 'unified'
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeStringify)
.process('# Hello, *Mercury*!')
console.log(String(file)) // => '<h1>Hello, <em>Mercury</em>!</h1>'
Notice that this pipeline uses
unified() directly rather than remark(). When the output is not markdown you only need remark-parse for parsing — there is no need to include remark-stringify. You compose exactly the plugins you need, nothing more.Add
rehype-sanitize between remarkRehype and rehypeStringify when processing untrusted user-supplied markdown. Without sanitization, raw HTML in the markdown source is passed through to the output unchanged, which can expose XSS vulnerabilities.Next steps
Now that you have a working pipeline, here are some directions to explore:- Installation — detailed install instructions for all four packages, including Deno and browser usage.
- Add GFM support — install
remark-gfmand add.use(remarkGfm)to enable GitHub Flavored Markdown features like tables, strikethrough, and task lists. - Lint your markdown — use
remark-preset-lint-recommendedwithremark-clito enforce style rules across your whole project. - Browse plugins — over 150 remark plugins are available. Explore the list of plugins or the
awesome-remarkcollection to find tools for syntax highlighting, math rendering, directives, and more.