CLI to process markdown with remark
npm install remark-cli[![Build][build-badge]][build]
[![Coverage][coverage-badge]][coverage]
[![Downloads][downloads-badge]][downloads]
[![Sponsors][sponsors-badge]][collective]
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]
Command line interface to inspect and change markdown files with [remark][].
* What is this?
* When should I use this?
* Install
* Use
* CLI
* Examples
* Example: checking and formatting markdown on the CLI
* Example: config files (JSON, YAML, JS)
* Compatibility
* Security
* Contribute
* Sponsor
* License
This package is a command line interface (CLI) that you can use in your terminal
or in npm scripts and the like to inspect and change markdown files.
This CLI is built around remark, which is an ecosystem of plugins that work with
markdown as structured data, specifically ASTs (abstract syntax trees).
You can choose from the 150+ existing plugins or make your own.
See [the monorepo readme][remark] for info on what the remark ecosystem is.
You can use this package when you want to work with the markdown files in your
project from the command line.remark-cli has many options and you can combine it with many plugins, so it
should be possible to do what you want.
If not, you can always use [remark][remark-core] itself manually in a script.
This package is [ESM only][esm].
In Node.js (version 16+), install with [npm][]:
``sh`
npm install remark-cli
Add a table of contents with [remark-toc][remark-toc] to readme.md:
`sh`
remark readme.md --output --use remark-toc
Lint all markdown files in the current directory according to the markdown style
guide with [remark-preset-lint-markdown-style-guide][markdown-style-guide].
`sh`
remark . --use remark-preset-lint-markdown-style-guide
The interface of remark-cli is explained as follows on its help pageremark --help
():
`txt
Usage: remark [options] [path | glob ...]
CLI to process markdown with remark
Options:
--[no-]color specify color in report (on by default)
--[no-]config search for configuration files (on by default)
-e --ext
--file-path
-f --frail exit with 1 on warnings
-h --help output usage information
--[no-]ignore search for ignore files (on by default)
-i --ignore-path
--ignore-path-resolve-from cwd|dir resolve patterns in ignore-path from its directory or cwd
--ignore-pattern
--inspect output formatted syntax tree
-o --output [path] specify output location
-q --quiet output only warnings and errors
-r --rc-path
--report
-s --setting
-S --silent output only errors
--silently-ignore do not fail when given ignored files
--[no-]stdout specify writing to stdout (on by default)
-t --tree specify input and output as syntax tree
--tree-in specify input as syntax tree
--tree-out output syntax tree
-u --use
--verbose report extra info for messages
-v --version output version number
-w --watch watch for changes and reprocess
Examples:
# Process input.md
$ remark input.md -o output.md
# Pipe
$ remark < input.md > output.md
# Rewrite all applicable files
$ remark . -o
`
More info on all these options is available at [unified-args][unified-args],remark-cli
which does the work. is unified-args preconfigured to:
* load remark- plugins.md
* search for markdown extensions
([, .markdown, etc][markdown-extensions]).remarkignore
* ignore paths found in [ files][ignore-file].remarkrc
* load configuration from
[, .remarkrc.js, etc files][config-file]remarkConfig
* use configuration from
[ fields in package.json files][config-file]
This example checks and formats markdown with remark-cli.
It assumes you’re in a Node.js package.
Install the CLI and plugins:
`sh`
npm install remark-cli remark-preset-lint-consistent remark-preset-lint-recommended remark-toc --save-dev
…then add an npm script in your package.json:
`js`
/ … /
"scripts": {
/ … /
"format": "remark . --output",
/ … /
},
/ … /
> 💡 Tip: add ESLint and such in the format script too.
The above change adds a format script, which can be run withnpm run format..
It runs remark on all markdown files () and rewrites them (--output)../node_modules/.bin/remark --help
Run for more info on the CLI.
Then, add a remarkConfig to your package.json to configure remark:
`js
/ … /
"remarkConfig": {
"settings": {
"bullet": "", // Use for list item bullets (default)## Contents
// See
},
"plugins": [
"remark-preset-lint-consistent", // Check that markdown is consistent.
"remark-preset-lint-recommended", // Few recommended rules.
[
// Generate a table of contents in `
"remark-toc",
{
"heading": "contents"
}
]
]
},
/ … /
> 👉 Note: you must remove the comments in the above examples when
> copy/pasting them as comments are not supported in package.json files.
Finally, you can run the npm script to check and format markdown files in your
project:
`sh`
npm run format
In the previous example, we saw that remark-cli was configured from within apackage.json file.package.json
That’s a good place when the configuration is relatively short, when you have a, and when you don’t need comments (which are not allowed in
JSON).
You can also define configuration in separate files in different languages.
With the package.json config as inspiration, here’s a JavaScript version that.remarkrc.js
can be placed in :
`js
import remarkPresetLintConsistent from 'remark-preset-lint-consistent'
import remarkPresetLintRecommended from 'remark-preset-lint-recommended'
import remarkToc from 'remark-toc'
const remarkConfig = {
settings: {
bullet: '', // Use for list item bullets (default)## Contents
// See
},
plugins: [
remarkPresetLintConsistent, // Check that markdown is consistent.
remarkPresetLintRecommended, // Few recommended rules.
// Generate a table of contents in
[remarkToc, {heading: 'contents'}]
]
}
export default remarkConfig
`
This is the same configuration in YAML, which can be placed in .remarkrc.yml:
`yml## Contents
settings:
bullet: "*"
plugins:
# Check that markdown is consistent.
- remark-preset-lint-consistent
# Few recommended rules.
- remark-preset-lint-recommended
# Generate a table of contents in `
- - remark-toc
- heading: contents
When remark-cli is about to process a markdown file it’ll search the file
system upwards for configuration files starting at the folder where that file
exists.
Take the following file structure as an illustration:
`txt`
folder/
├─ subfolder/
│ ├─ .remarkrc.json
│ └─ file.md
├─ .remarkrc.js
├─ package.json
└─ readme.md
When folder/subfolder/file.md is processed, the closest config file isfolder/subfolder/.remarkrc.json.folder/readme.md
For , it’s folder/.remarkrc.js.
The order of precedence is as follows.
Earlier wins (so in the above file structure folder/.remarkrc.js wins overfolder/package.json):
1. .remarkrc (JSON).remarkrc.cjs
2. (CJS).remarkrc.js
3. (CJS or ESM, depending on type: 'module' in package.json).remarkrc.json
4. (JSON).remarkrc.mjs
5. (ESM).remarkrc.yaml
6. (YAML).remarkrc.yml
7. (YAML)package.json
8. with remarkConfig field
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, remark-cli@^12,
compatible with Node.js 16.
As markdown can be turned into HTML and improper use of HTML can open you up to
[cross-site scripting (XSS)][xss] attacks, use of remark can be unsafe.
When going to HTML, you will likely combine remark with [rehype][], in which
case you should use [rehype-sanitize][rehype-sanitize].
Use of remark plugins could also open you up to other attacks.
Carefully assess each plugin and the risks involved in using them.
For info on how to submit a report, see our [security policy][security].
See [contributing.md][contributing] in [remarkjs/.github][health] for wayssupport.md`][support] for ways to get help.
to get started.
See [
Join us in [Discussions][chat] to chat with the community and contributors.
This project has a [code of conduct][coc].
By interacting with this repository, organization, or community you agree to
abide by its terms.
Support this effort and give back by sponsoring on [OpenCollective][collective]!
Vercel | Motif | HashiCorp | GitBook | Gatsby | |||||
Netlify ![]() | Coinbase | ThemeIsle | Expo | Boost Note ![]() | Markdown Space ![]() | Holloway | |||
You? | |||||||||
[MIT][license] © [Titus Wormer][author]
[build-badge]: https://github.com/remarkjs/remark/workflows/main/badge.svg
[build]: https://github.com/remarkjs/remark/actions
[coverage-badge]: https://img.shields.io/codecov/c/github/remarkjs/remark.svg
[coverage]: https://codecov.io/github/remarkjs/remark
[downloads-badge]: https://img.shields.io/npm/dm/remark-cli.svg
[downloads]: https://www.npmjs.com/package/remark-cli
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
[collective]: https://opencollective.com/unified
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
[chat]: https://github.com/remarkjs/remark/discussions
[security]: https://github.com/remarkjs/.github/blob/main/security.md
[health]: https://github.com/remarkjs/.github
[contributing]: https://github.com/remarkjs/.github/blob/main/contributing.md
[support]: https://github.com/remarkjs/.github/blob/main/support.md
[coc]: https://github.com/remarkjs/.github/blob/main/code-of-conduct.md
[license]: https://github.com/remarkjs/remark/blob/main/license
[author]: https://wooorm.com
[npm]: https://docs.npmjs.com/cli/install
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[markdown-extensions]: https://github.com/sindresorhus/markdown-extensions
[rehype]: https://github.com/rehypejs/rehype
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize
[remark]: https://github.com/remarkjs/remark
[remark-core]: ../remark/
[remark-toc]: https://github.com/remarkjs/remark-toc
[config-file]: https://github.com/unifiedjs/unified-engine#config-files
[ignore-file]: https://github.com/unifiedjs/unified-engine#ignore-files
[unified-args]: https://github.com/unifiedjs/unified-args#cli
[markdown-style-guide]: https://github.com/remarkjs/remark-lint/tree/main/packages/remark-preset-lint-markdown-style-guide
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting