markdownlint-cli 0.38.0 → 0.40.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +24 -18
  2. package/markdownlint.js +40 -25
  3. package/package.json +12 -10
package/README.md CHANGED
@@ -26,20 +26,21 @@ markdownlint --help
26
26
  MarkdownLint Command Line Interface
27
27
 
28
28
  Options:
29
- -V, --version output the version number
30
- -c, --config [configFile] configuration file (JSON, JSONC, JS, or YAML)
31
- -d, --dot include files/folders with a dot (for example `.github`)
32
- -f, --fix fix basic errors (does not work with STDIN)
33
- -i, --ignore [file|directory|glob] file(s) to ignore/exclude (default: [])
34
- -j, --json write issues in json format
35
- -o, --output [outputFile] write issues to file (no console)
36
- -p, --ignore-path [file] path to file with ignore pattern(s)
37
- -q, --quiet do not write issues to STDOUT
38
- -r, --rules [file|directory|glob|package] include custom rule files (default: [])
39
- -s, --stdin read from STDIN (does not work with files)
40
- --enable [rules...] Enable certain rules, e.g. --enable MD013 MD041 --
41
- --disable [rules...] Disable certain rules, e.g. --disable MD013 MD041 --
42
- -h, --help display help for command
29
+ -V, --version output the version number
30
+ -c, --config <configFile> configuration file (JSON, JSONC, JS, YAML, or TOML)
31
+ --configPointer <pointer> JSON Pointer to object within configuration file (default: "")
32
+ -d, --dot include files/folders with a dot (for example `.github`)
33
+ -f, --fix fix basic errors (does not work with STDIN)
34
+ -i, --ignore <file|directory|glob> file(s) to ignore/exclude (default: [])
35
+ -j, --json write issues in json format
36
+ -o, --output <outputFile> write issues to file (no console)
37
+ -p, --ignore-path <file> path to file with ignore pattern(s)
38
+ -q, --quiet do not write issues to STDOUT
39
+ -r, --rules <file|directory|glob|package> include custom rule files (default: [])
40
+ -s, --stdin read from STDIN (does not work with files)
41
+ --enable <rules...> Enable certain rules, e.g. --enable MD013 MD041 --
42
+ --disable <rules...> Disable certain rules, e.g. --disable MD013 MD041 --
43
+ -h, --help display help for command
43
44
  ```
44
45
 
45
46
  Or run using [Docker](https://www.docker.com) and [GitHub Packages](https://github.com/features/packages):
@@ -88,7 +89,7 @@ Because this option makes changes to the input files, it is good to make a backu
88
89
 
89
90
  `markdownlint-cli` reuses [the rules][rules] from `markdownlint` package.
90
91
 
91
- Configuration is stored in JSON, JSONC, YAML, or INI files in the same [config format][config].
92
+ Configuration is stored in JSON, JSONC, YAML, INI, or TOML files in the same [config format][config].
92
93
 
93
94
  A sample configuration file:
94
95
 
@@ -102,7 +103,7 @@ A sample configuration file:
102
103
  }
103
104
  ```
104
105
 
105
- For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], or the [style folder][style-folder].
106
+ For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], [test-config.toml](test/test-config.toml) or the [style folder][style-folder].
106
107
 
107
108
  The CLI argument `--config` is not required.
108
109
  If it is not provided, `markdownlint-cli` looks for the file `.markdownlint.jsonc`/`.markdownlint.json`/`.markdownlint.yaml`/`.markdownlint.yml` in current folder, or for the file `.markdownlintrc` in the current or all parent folders.
@@ -113,10 +114,14 @@ JS configuration files contain JavaScript code, must have the `.js` or `.cjs` fi
113
114
  If your workspace _(project)_ is [ESM-only] _(`"type": "module"` set in the root `package.json` file)_, then the configuration file **should end with `.cjs` file extension**.
114
115
  A JS configuration file may internally `require` one or more npm packages as a way of reusing configuration across projects.
115
116
 
117
+ The `--configPointer` argument allows the use of [JSON Pointer][json-pointer] syntax to identify a sub-object within the configuration object (per above).
118
+ This argument can be used with any configuration file type and makes it possible to nest a configuration object within another file like `package.json` or `pyproject.toml` (e.g., via `/key` or `/key/subkey`).
119
+
116
120
  `--enable` and `--disable` override configuration files; if a configuration file disables `MD123` and you pass `--enable MD123`, it will be enabled.
117
121
  If a rule is passed to both `--enable` and `--disable`, it will be disabled.
118
122
 
119
- > JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern.
123
+ > - JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern.
124
+ > - TOML configuration files must be provided via the `--config` argument; they are not automatically loaded.
120
125
 
121
126
  ## Exit codes
122
127
 
@@ -134,7 +139,7 @@ To run `markdownlint-cli` as part of a [pre-commit][pre-commit] workflow, add so
134
139
 
135
140
  ```yaml
136
141
  - repo: https://github.com/igorshubovych/markdownlint-cli
137
- rev: v0.38.0
142
+ rev: v0.40.0
138
143
  hooks:
139
144
  - id: markdownlint
140
145
  ```
@@ -155,6 +160,7 @@ MIT © Igor Shubovych
155
160
  [actions-badge]: https://github.com/igorshubovych/markdownlint-cli/workflows/CI/badge.svg?branch=master
156
161
  [actions-url]: https://github.com/igorshubovych/markdownlint-cli/actions?query=workflow%3ACI
157
162
  [commander-variadic]: https://github.com/tj/commander.js#variadic-option
163
+ [json-pointer]: https://datatracker.ietf.org/doc/html/rfc6901
158
164
  [markdownlint]: https://github.com/DavidAnson/markdownlint
159
165
  [markdownlint-cli2]: https://github.com/DavidAnson/markdownlint-cli2
160
166
  [markdownlint-jsonc]: https://github.com/DavidAnson/markdownlint/blob/main/schema/.markdownlint.jsonc
package/markdownlint.js CHANGED
@@ -7,11 +7,12 @@ const path = require('node:path');
7
7
  const Module = require('node:module');
8
8
  const os = require('node:os');
9
9
  const process = require('node:process');
10
- const program = require('commander');
10
+ const {program} = require('commander');
11
11
  const glob = require('glob');
12
12
  const markdownlint = require('markdownlint');
13
13
  const rc = require('run-con');
14
14
  const minimatch = require('minimatch');
15
+ const jsonpointer = require('jsonpointer');
15
16
  const pkg = require('./package.json');
16
17
 
17
18
  const options = program.opts();
@@ -21,13 +22,26 @@ function posixPath(p) {
21
22
  }
22
23
 
23
24
  function jsoncParse(text) {
24
- return JSON.parse(require('jsonc-parser').stripComments(text));
25
+ const {parse, printParseErrorCode} = require('jsonc-parser');
26
+ const errors = [];
27
+ const result = parse(text, errors, {allowTrailingComma: true});
28
+ if (errors.length > 0) {
29
+ const aggregate = errors.map(error => `${printParseErrorCode(error.error)} (offset ${error.offset}, length ${error.length})`).join(', ');
30
+ throw new Error(`Unable to parse JSON(C) content, ${aggregate}`);
31
+ }
32
+
33
+ return result;
25
34
  }
26
35
 
27
- function jsYamlSafeLoad(text) {
36
+ function yamlParse(text) {
28
37
  return require('js-yaml').load(text);
29
38
  }
30
39
 
40
+ function tomlParse(text) {
41
+ // It is necessary to add the prototype manually because of https://github.com/BinaryMuse/toml-node/issues/55
42
+ return require('deep-extend')({}, require('toml').parse(text));
43
+ }
44
+
31
45
  const exitCodes = {
32
46
  lintFindings: 1,
33
47
  failedToWriteOutputFile: 2,
@@ -36,13 +50,12 @@ const exitCodes = {
36
50
  };
37
51
 
38
52
  const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
39
- const configParsers = [jsoncParse, jsYamlSafeLoad];
53
+ // TOML files can be (incorrectly) read by yamlParse (but not vice versa), so tomlParse needs to go before yamlParse
54
+ const configParsers = [jsoncParse, tomlParse, yamlParse];
40
55
  const fsOptions = {encoding: 'utf8'};
41
56
  const processCwd = process.cwd();
42
57
 
43
58
  function readConfiguration(userConfigFile) {
44
- const jsConfigFile = /\.c?js$/i.test(userConfigFile);
45
-
46
59
  // Load from well-known config files
47
60
  let config = rc('markdownlint', {});
48
61
  for (const projectConfigFile of projectConfigFiles) {
@@ -57,9 +70,10 @@ function readConfiguration(userConfigFile) {
57
70
  }
58
71
 
59
72
  // Normally parsing this file is not needed, because it is already parsed by rc package.
60
- // However I have to do it to overwrite configuration from .markdownlint.{json,yaml,yml}.
73
+ // However I have to do it to overwrite configuration from .markdownlint.{jsonc,json,yaml,yml}.
61
74
  if (userConfigFile) {
62
75
  try {
76
+ const jsConfigFile = /\.c?js$/i.test(userConfigFile);
63
77
  const userConfig = jsConfigFile ? require(path.resolve(processCwd, userConfigFile)) : markdownlint.readConfigSync(userConfigFile, configParsers);
64
78
  config = require('deep-extend')(config, userConfig);
65
79
  } catch (error) {
@@ -77,7 +91,10 @@ function prepareFileList(files, fileExtensions, previousResults) {
77
91
  nodir: true
78
92
  };
79
93
  let extensionGlobPart = '*.';
80
- if (fileExtensions.length === 1) {
94
+ if (!fileExtensions) {
95
+ // Match everything
96
+ extensionGlobPart = '';
97
+ } else if (fileExtensions.length === 1) {
81
98
  // Glob seems not to match patterns like 'foo.{js}'
82
99
  extensionGlobPart += fileExtensions[0];
83
100
  } else {
@@ -182,24 +199,25 @@ program
182
199
  .version(pkg.version)
183
200
  .description(pkg.description)
184
201
  .usage('[options] <files|directories|globs>')
185
- .option('-c, --config [configFile]', 'configuration file (JSON, JSONC, JS, or YAML)')
202
+ .option('-c, --config <configFile>', 'configuration file (JSON, JSONC, JS, YAML, or TOML)')
203
+ .option('--configPointer <pointer>', 'JSON Pointer to object within configuration file', '')
186
204
  .option('-d, --dot', 'include files/folders with a dot (for example `.github`)')
187
205
  .option('-f, --fix', 'fix basic errors (does not work with STDIN)')
188
- .option('-i, --ignore [file|directory|glob]', 'file(s) to ignore/exclude', concatArray, [])
206
+ .option('-i, --ignore <file|directory|glob>', 'file(s) to ignore/exclude', concatArray, [])
189
207
  .option('-j, --json', 'write issues in json format')
190
- .option('-o, --output [outputFile]', 'write issues to file (no console)')
191
- .option('-p, --ignore-path [file]', 'path to file with ignore pattern(s)')
208
+ .option('-o, --output <outputFile>', 'write issues to file (no console)')
209
+ .option('-p, --ignore-path <file>', 'path to file with ignore pattern(s)')
192
210
  .option('-q, --quiet', 'do not write issues to STDOUT')
193
- .option('-r, --rules [file|directory|glob|package]', 'include custom rule files', concatArray, [])
211
+ .option('-r, --rules <file|directory|glob|package>', 'include custom rule files', concatArray, [])
194
212
  .option('-s, --stdin', 'read from STDIN (does not work with files)')
195
- .option('--enable [rules...]', 'Enable certain rules, e.g. --enable MD013 MD041 --')
196
- .option('--disable [rules...]', 'Disable certain rules, e.g. --disable MD013 MD041 --');
213
+ .option('--enable <rules...>', 'Enable certain rules, e.g. --enable MD013 MD041 --')
214
+ .option('--disable <rules...>', 'Disable certain rules, e.g. --disable MD013 MD041 --');
197
215
 
198
216
  program.parse(process.argv);
199
217
 
200
218
  function tryResolvePath(filepath) {
201
219
  try {
202
- if (path.basename(filepath) === filepath && path.extname(filepath) === '') {
220
+ if ((path.basename(filepath) === filepath || filepath.startsWith('@')) && path.extname(filepath) === '') {
203
221
  // Looks like a package name, resolve it relative to cwd
204
222
  // Get list of directories, where requested module can be.
205
223
  let paths = Module._nodeModulePaths(processCwd);
@@ -253,19 +271,18 @@ if (existsSync(ignorePath)) {
253
271
  }
254
272
 
255
273
  const files = prepareFileList(program.args, ['md', 'markdown']).filter(value => ignoreFilter(value));
256
- const ignores = prepareFileList(options.ignore, ['md', 'markdown'], files);
274
+ const ignores = prepareFileList(options.ignore, null, files);
257
275
  const customRules = loadCustomRules(options.rules);
258
276
  const diff = files.filter(file => !ignores.some(ignore => ignore.absolute === file.absolute)).map(paths => paths.original);
259
277
 
260
278
  function lintAndPrint(stdin, files) {
261
- files = files || [];
262
- const config = readConfiguration(options.config);
279
+ files ||= [];
280
+ const configuration = readConfiguration(options.config);
281
+ const config = jsonpointer.get(configuration, options.configPointer) || {};
263
282
 
264
283
  for (const rule of options.enable || []) {
265
284
  // Leave default values in place if rule is an object
266
- if (!config[rule]) {
267
- config[rule] = true;
268
- }
285
+ config[rule] ||= true;
269
286
  }
270
287
 
271
288
  for (const rule of options.disable || []) {
@@ -316,9 +333,7 @@ try {
316
333
  if (files.length > 0 && !options.stdin) {
317
334
  lintAndPrint(null, diff);
318
335
  } else if (files.length === 0 && options.stdin && !options.fix) {
319
- import('get-stdin')
320
- .then(module => module.default())
321
- .then(lintAndPrint);
336
+ import('get-stdin').then(module => module.default()).then(lintAndPrint);
322
337
  } else {
323
338
  program.help();
324
339
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdownlint-cli",
3
- "version": "0.38.0",
3
+ "version": "0.40.0",
4
4
  "description": "MarkdownLint Command Line Interface",
5
5
  "main": "markdownlint.js",
6
6
  "bin": {
@@ -37,21 +37,23 @@
37
37
  },
38
38
  "homepage": "https://github.com/igorshubovych/markdownlint-cli#readme",
39
39
  "dependencies": {
40
- "commander": "~11.1.0",
40
+ "commander": "~12.0.0",
41
41
  "get-stdin": "~9.0.0",
42
- "glob": "~10.3.10",
43
- "ignore": "~5.3.0",
42
+ "glob": "~10.3.12",
43
+ "ignore": "~5.3.1",
44
44
  "js-yaml": "^4.1.0",
45
- "jsonc-parser": "~3.2.0",
46
- "markdownlint": "~0.32.1",
47
- "minimatch": "~9.0.3",
48
- "run-con": "~1.3.2"
45
+ "jsonc-parser": "~3.2.1",
46
+ "jsonpointer": "5.0.1",
47
+ "markdownlint": "~0.34.0",
48
+ "minimatch": "~9.0.4",
49
+ "run-con": "~1.3.2",
50
+ "toml": "~3.0.0"
49
51
  },
50
52
  "devDependencies": {
51
- "ava": "^6.0.1",
53
+ "ava": "^6.1.2",
52
54
  "execa": "^8.0.1",
53
55
  "markdownlint-cli-local-test-rule": "./test/custom-rules/markdownlint-cli-local-test-rule",
54
- "xo": "^0.56.0"
56
+ "xo": "^0.58.0"
55
57
  },
56
58
  "xo": {
57
59
  "prettier": true,