UNPKG

15.9 kBMarkdownView Raw
1# markdownlint-cli2
2
3> A fast, flexible, configuration-based command-line interface for linting
4> Markdown/CommonMark files with the `markdownlint` library
5
6[![npm version][npm-image]][npm-url]
7[![License][license-image]][license-url]
8
9## Install
10
11As a global CLI:
12
13```shell
14npm install markdownlint-cli2 --global
15```
16
17As a development dependency of the current package:
18
19```shell
20npm install markdownlint-cli2 --save-dev
21```
22
23## Overview
24
25- [`markdownlint`][markdownlint] is a library for linting [Markdown][markdown]/
26 [CommonMark][commonmark] files on [Node.js][nodejs] using the
27 [markdown-it][markdown-it] parser.
28- [`markdownlint-cli`][markdownlint-cli] is a traditional command-line interface
29 for `markdownlint`.
30- [`markdownlint-cli2`][markdownlint-cli2] is a slightly unconventional
31 command-line interface for `markdownlint`.
32- `markdownlint-cli2` is configuration-based and prioritizes speed and
33 simplicity.
34- `markdownlint-cli2` supports all the features of `markdownlint-cli` (sometimes
35 a little differently).
36- [`vscode-markdownlint`][vscode-markdownlint] is a `markdownlint` extension for
37 the [Visual Studio Code editor][vscode].
38- `markdownlint-cli2` is designed to work well in conjunction with
39 `vscode-markdownlint`.
40- More about the [motivation for `markdownlint-cli2`][markdownlint-cli2-blog].
41
42## Use
43
44```text
45markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)
46https://github.com/DavidAnson/markdownlint-cli2
47
48Syntax: markdownlint-cli2 glob0 [glob1] [...] [globN]
49
50Glob expressions (from the globby library):
51- * matches any number of characters, but not /
52- ? matches a single character, but not /
53- ** matches any number of characters, including / (when it's the only thing in a path part)
54- {} allows for a comma-separated list of "or" expressions
55- ! or # at the beginning of a pattern negate the match
56
57Dot-only glob:
58- The command "markdownlint-cli2 ." would lint every file in the current directory tree which is probably not intended
59- Instead, it is mapped to "markdownlint-cli2 *.{md,markdown}" which lints all Markdown files in the current directory
60- To lint every file in the current directory tree, the command "markdownlint-cli2 **" can be used instead
61
62Configuration via:
63- .markdownlint-cli2.jsonc
64- .markdownlint-cli2.yaml
65- .markdownlint-cli2.js
66- .markdownlint.jsonc or .markdownlint.json
67- .markdownlint.yaml or .markdownlint.yml
68- .markdownlint.js
69
70Cross-platform compatibility:
71- UNIX and Windows shells expand globs according to different rules, so quoting glob arguments is recommended
72- Shells that expand globs do not support negated patterns (!node_modules), so quoting negated globs is required
73- Some Windows shells do not handle single-quoted (') arguments correctly, so double-quotes (") are recommended
74- Some UNIX shells handle exclamation (!) in double-quotes specially, so hashtag (#) is recommended for negated globs
75- Some shells use backslash (\) to escape special characters, so forward slash (/) is the recommended path separator
76
77Therefore, the most compatible glob syntax for cross-platform support:
78$ markdownlint-cli2 "**/*.md" "#node_modules"
79```
80
81For scenarios where it is preferable to specify glob expressions in a
82configuration file, the `globs` property of `.markdownlint-cli2.jsonc` or
83`.markdownlint-cli2.yaml` or `.markdownlint-cli2.js` may be used instead of (or
84in addition to) passing `glob0 ... globN` on the command-line.
85
86As shown above, the default command-line for `markdownlint-cli2` looks something
87like:
88
89```bash
90markdownlint-cli2 "**/*.md" "#node_modules"
91```
92
93However, because sharing configuration between "normal" and "fix" modes is so
94common, the following command defaults the `fix` property (see below) to `true`:
95
96```bash
97markdownlint-cli2-fix "**/*.md" "#node_modules"
98```
99
100Other than the default behavior of the `fix` property (which can be overridden
101in both cases), these two commands behave identically.
102
103### Exit Codes
104
105- `0`: Linting was successful and there were no errors
106- `1`: Linting was successful and there were errors
107- `2`: Linting was not completed due to a runtime issue
108
109## Rule List
110
111- See the [Rules / Aliases][markdownlint-rules-aliases] and
112 [Tags][markdownlint-rules-tags] sections of the `markdownlint` documentation.
113
114## Glob expressions
115
116- Globbing is performed by the [globby][globby] library; refer to that
117 documentation for more information and examples.
118
119## Configuration
120
121- See the [Configuration][markdownlint-configuration] section of the
122 `markdownlint` documentation for information about the inline comment syntax
123 for enabling and disabling rules with HTML comments.
124
125### `.markdownlint-cli2.jsonc`
126
127- The format of this file is a [JSONC][jsonc] object similar to the
128 [`markdownlint` `options` object][markdownlint-options].
129- Valid properties are:
130 - `config`: [`markdownlint` `config` object][markdownlint-config] to configure
131 rules for this part of the directory tree
132 - If a `.markdownlint.{jsonc,json,yaml,yml,js}` file (see below) is present
133 in the same directory, it overrides the value of this property
134 - `customRules`: `Array` of `String`s of module names/paths of
135 [custom rules][markdownlint-custom-rules] to load and use when linting
136 - Each `String` is passed as the `id` parameter to Node's
137 [require function][nodejs-require]
138 - Relative paths are resolved based on the location of the `JSONC` file
139 - Search [`markdownlint-rule` on npm][markdownlint-rule]
140 - `fix`: `Boolean` value to enable fixing of linting errors reported by rules
141 that emit fix information
142 - Fixes are made directly to the relevant file(s); no backup is created
143 - `frontMatter`: `String` defining the [`RegExp`][regexp] used to match and
144 ignore any [front matter][front-matter] at the beginning of a document
145 - The `String` is passed as the `pattern` parameter to the
146 [`RegExp` constructor][regexp-constructor]
147 - For example: `(^---\s*$[^]*?^---\s*$)(\r\n|\r|\n|$)`
148 - `globs`: `Array` of `String`s defining glob expressions to append to the
149 command-line arguments
150 - This setting can be used instead of (or in addition to) passing globs on
151 the command-line and offers identical performance
152 - This top-level setting is valid **only** in the directory from which
153 `markdownlint-cli2` is run
154 - `ignores`: `Array` of `String`s defining glob expressions to ignore when
155 linting
156 - This setting has the best performance when applied to the directory from
157 which `markdownlint-cli2` is run
158 - In this case, glob expressions are negated (by adding a leading `!`) and
159 appended to the command-line arguments before file enumeration
160 - The setting is not inherited by nested configuration files in this case
161 - When this setting is applied in subdirectories, ignoring of files is done
162 after file enumeration, so large directories can negatively impact
163 performance
164 - Nested configuration files inherit and reapply the setting to the
165 contents of nested directories in this case
166 - `markdownItPlugins`: `Array` of `Array`s, each of which has a `String`
167 naming a [markdown-it plugin][markdown-it-syntax-extensions] followed by
168 parameters
169 - Plugins can be used to add support for additional Markdown syntax
170 - Relative paths are resolved based on the location of the `JSONC` file
171 - For example: `[ [ "plugin-name", param_0, param_1, ... ], ... ]`
172 - Search [`markdown-it-plugins` on npm][markdown-it-plugins]
173 - `noInlineConfig`: `Boolean` value to disable the support of
174 [HTML comments][html-comment] within Markdown content
175 - For example: `<!-- markdownlint-disable some-rule -->`
176 - `noProgress`: `Boolean` value to disable the display of progress on `stdout`
177 - This top-level setting is valid **only** in the directory from which
178 `markdownlint-cli2` is run
179 - `outputFormatters`: `Array` of `Array`s, each of which has a `String`
180 naming an [output formatter][output-formatters] followed by parameters
181 - Formatters can be used to customize the tool's output for different
182 scenarios
183 - Relative paths are resolved based on the location of the `JSONC` file
184 - For example: `[ [ "formatter-name", param_0, param_1, ... ], ... ]`
185 - This top-level setting is valid **only** in the directory from which
186 `markdownlint-cli2` is run
187 - Search [`markdownlint-cli2-formatter` on npm][markdownlint-cli2-formatter]
188- Settings in this file apply to the directory it is in and all subdirectories.
189- Settings **merge with** those applied by any versions of this file in a parent
190 directory.
191- For example: [`.markdownlint-cli2.jsonc`][markdownlint-cli2-jsonc] with all
192 properties set
193
194### `.markdownlint-cli2.yaml`
195
196- The format of this file is a [YAML][yaml] object with the structure described
197 above for `.markdownlint-cli2.jsonc`.
198- Other details are the same as for `.markdownlint-cli2.jsonc` described above.
199- If a `.markdownlint-cli2.jsonc` file is present in the same directory, it
200 takes precedence.
201- For example: [`.markdownlint-cli2.yaml`][markdownlint-cli2-yaml] with all
202 properties set
203
204### `.markdownlint-cli2.js`
205
206- The format of this file is a [CommonJS module][commonjs-module] that exports
207 the object described above for `.markdownlint-cli2.jsonc`.
208- Instead of passing a `String` to identify the module name/path to load for
209 `customRules`, `markdownItPlugins`, and `outputFormatters`, the corresponding
210 `Object` or `Function` can be provided directly.
211- Other details are the same as for `.markdownlint-cli2.jsonc` described above.
212- If a `.markdownlint-cli2.jsonc` or `.markdownlint-cli2.yaml` file is present
213 in the same directory, it takes precedence.
214- For example: [`.markdownlint-cli2.js`][markdownlint-cli2-js]
215
216### `.markdownlint.jsonc` or `.markdownlint.json`
217
218- The format of this file is a [JSONC][jsonc] or [JSON][json] object matching
219 the [`markdownlint` `config` object][markdownlint-config].
220- Settings in this file apply to the directory it is in and all subdirectories
221- Settings **override** those applied by any versions of this file in a parent
222 directory.
223- If `jsonc` and `json` files are present in the same directory, the `jsonc`
224 version takes precedence.
225- To merge the settings of these files or share configuration, use the `extends`
226 property (documented in the link above).
227- Both file types support comments in JSON.
228- For example: [`.markdownlint.jsonc`][markdownlint-jsonc]
229
230### `.markdownlint.yaml` or `.markdownlint.yml`
231
232- The format of this file is a [YAML][yaml] object representing the
233 [`markdownlint` `config` object][markdownlint-config].
234- Other details are the same as for `jsonc`/`json` files described above.
235- If `yaml` and `yml` files are present in the same directory, the `yaml`
236 version takes precedence.
237- If a `jsonc` or `json` file is present in the same directory, it takes
238 precedence.
239- For example: [`.markdownlint.yaml`][markdownlint-yaml]
240
241### `.markdownlint.js`
242
243- The format of this file is a [CommonJS module][commonjs-module] that exports
244 the [`markdownlint` `config` object][markdownlint-config].
245- Other details are the same as for `jsonc`/`json` files described above.
246- If a `jsonc`, `json`, `yaml`, or `yml` file is present in the same directory,
247 it takes precedence.
248- For example: [`.markdownlint.js`][markdownlint-js]
249
250## Compatibility
251
252### `markdownlint-cli`
253
254- The glob implementation and handling of pattern matching is slightly
255 different.
256- Configuration files are supported in every directory (vs. only one at the
257 root).
258- The `INI` config format, `.markdownlintrc`, and `.markdownlintignore` are not
259 supported.
260
261### `vscode-markdownlint`
262
263- `.markdownlintrc` and `.markdownlintignore` are not supported.
264
265## pre-commit
266
267To run `markdownlint-cli2` as part of a [pre-commit][pre-commit] workflow, add
268a reference to the `repos` list in that project's `.pre-commit-config.yaml`:
269
270```yaml
271- repo: https://github.com/DavidAnson/markdownlint-cli2
272 rev: v0.0.14
273 hooks:
274 - id: markdownlint-cli2
275```
276
277## History
278
279- 0.0.2 - Initial release
280- 0.0.3 - Feature parity with `markdownlint-cli`
281- 0.0.4 - Support output formatters and `markdown-it` plugins
282- 0.0.5 - Improve support for ignoring files
283- 0.0.6 - Improve handling of very large directory trees
284- 0.0.7 - Support `.markdownlint-cli2.js` and `.markdownlint.js`
285- 0.0.8 - Support `.markdownlint-cli2.yaml`, add progress
286- 0.0.9 - Improve configuration file handling
287- 0.0.10 - Improve performance and configuration
288- 0.0.11 - Improve performance of `fix`, update banner
289- 0.0.12 - Update dependencies (including `markdownlint`)
290- 0.0.13 - Add `markdownlint-cli2-fix` command
291- 0.0.14 - Update dependencies (including `markdownlint`)
292
293<!-- markdownlint-disable line-length -->
294
295[commonmark]: https://commonmark.org/
296[commonjs-module]: https://nodejs.org/api/modules.html#modules_modules_commonjs_modules
297[front-matter]: https://jekyllrb.com/docs/frontmatter/
298[globby]: https://www.npmjs.com/package/globby
299[html-comment]: https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/Getting_started
300[json]: https://wikipedia.org/wiki/JSON
301[jsonc]: https://code.visualstudio.com/Docs/languages/json#_json-with-comments
302[license-image]: https://img.shields.io/npm/l/markdownlint-cli2.svg
303[license-url]: https://opensource.org/licenses/MIT
304[markdown]: https://wikipedia.org/wiki/Markdown
305[markdown-it]: https://www.npmjs.com/package/markdown-it
306[markdown-it-plugins]: https://www.npmjs.com/search?q=keywords:markdown-it-plugin
307[markdown-it-syntax-extensions]: https://github.com/markdown-it/markdown-it#syntax-extensions
308[markdownlint]: https://github.com/DavidAnson/markdownlint
309[markdownlint-config]: https://github.com/DavidAnson/markdownlint/blob/main/README.md#optionsconfig
310[markdownlint-configuration]: https://github.com/DavidAnson/markdownlint/blob/main/README.md#configuration
311[markdownlint-custom-rules]: https://github.com/DavidAnson/markdownlint/blob/main/doc/CustomRules.md
312[markdownlint-options]: https://github.com/DavidAnson/markdownlint/blob/main/README.md#options
313[markdownlint-rules-aliases]: https://github.com/DavidAnson/markdownlint/blob/main/README.md#rules--aliases
314[markdownlint-rules-tags]: https://github.com/DavidAnson/markdownlint/blob/main/README.md#tags
315[markdownlint-cli]: https://github.com/igorshubovych/markdownlint-cli
316[markdownlint-cli2]: https://github.com/DavidAnson/markdownlint-cli2
317[markdownlint-cli2-blog]: https://dlaa.me/blog/post/markdownlintcli2
318[markdownlint-cli2-formatter]: https://www.npmjs.com/search?q=keywords:markdownlint-cli2-formatter
319[markdownlint-cli2-js]: test/markdownlint-cli2-js/.markdownlint-cli2.js
320[markdownlint-cli2-jsonc]: test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc
321[markdownlint-cli2-yaml]: test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml
322[markdownlint-js]: test/markdownlint-js/.markdownlint.js
323[markdownlint-jsonc]: https://github.com/DavidAnson/markdownlint/blob/main/schema/.markdownlint.jsonc
324[markdownlint-rule]: https://www.npmjs.com/search?q=keywords:markdownlint-rule
325[markdownlint-yaml]: https://github.com/DavidAnson/markdownlint/blob/main/schema/.markdownlint.yaml
326[nodejs]: https://nodejs.org/
327[nodejs-require]: https://nodejs.org/api/modules.html#modules_require_id
328[npm-image]: https://img.shields.io/npm/v/markdownlint-cli2.svg
329[npm-url]: https://www.npmjs.com/package/markdownlint-cli2
330[output-formatters]: doc/OutputFormatters.md
331[pre-commit]: https://pre-commit.com/
332[regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
333[regexp-constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
334[vscode]: https://code.visualstudio.com/
335[vscode-markdownlint]: https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint
336[yaml]: https://wikipedia.org/wiki/YAML