Skip to content

Commit ca05bed

Browse files
authored
Add support for TOML configuration files via --config (refs #113, refs #458).
1 parent 5fa53b1 commit ca05bed

10 files changed

+85
-9
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ markdownlint --help
2727

2828
Options:
2929
-V, --version output the version number
30-
-c, --config [configFile] configuration file (JSON, JSONC, JS, or YAML)
30+
-c, --config [configFile] configuration file (JSON, JSONC, JS, YAML, or TOML)
3131
-d, --dot include files/folders with a dot (for example `.github`)
3232
-f, --fix fix basic errors (does not work with STDIN)
3333
-i, --ignore [file|directory|glob] file(s) to ignore/exclude (default: [])
@@ -88,7 +88,7 @@ Because this option makes changes to the input files, it is good to make a backu
8888

8989
`markdownlint-cli` reuses [the rules][rules] from `markdownlint` package.
9090

91-
Configuration is stored in JSON, JSONC, YAML, or INI files in the same [config format][config].
91+
Configuration is stored in JSON, JSONC, YAML, INI, or TOML files in the same [config format][config].
9292

9393
A sample configuration file:
9494

@@ -102,7 +102,7 @@ A sample configuration file:
102102
}
103103
```
104104

105-
For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], or the [style folder][style-folder].
105+
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].
106106

107107
The CLI argument `--config` is not required.
108108
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.
@@ -116,7 +116,8 @@ A JS configuration file may internally `require` one or more npm packages as a w
116116
`--enable` and `--disable` override configuration files; if a configuration file disables `MD123` and you pass `--enable MD123`, it will be enabled.
117117
If a rule is passed to both `--enable` and `--disable`, it will be disabled.
118118

119-
> JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern.
119+
> - JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern.
120+
> - TOML configuration files must be provided via the `--config` argument; they are not automatically loaded.
120121
121122
## Exit codes
122123

markdownlint.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ function yamlParse(text) {
3636
return require('js-yaml').load(text);
3737
}
3838

39+
function tomlParse(text) {
40+
// It is necessary to add the prototype manually because of https://github.com/BinaryMuse/toml-node/issues/55
41+
return require('deep-extend')({}, require('toml').parse(text));
42+
}
43+
3944
const exitCodes = {
4045
lintFindings: 1,
4146
failedToWriteOutputFile: 2,
@@ -44,7 +49,8 @@ const exitCodes = {
4449
};
4550

4651
const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
47-
const configParsers = [jsoncParse, yamlParse];
52+
// TOML files can be (incorrectly) read by yamlParse (but not vice versa), so tomlParse needs to go before yamlParse
53+
const configParsers = [jsoncParse, tomlParse, yamlParse];
4854
const fsOptions = {encoding: 'utf8'};
4955
const processCwd = process.cwd();
5056

package-lock.json

+12-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"jsonc-parser": "~3.2.1",
4646
"markdownlint": "~0.33.0",
4747
"minimatch": "~9.0.3",
48-
"run-con": "~1.3.2"
48+
"run-con": "~1.3.2",
49+
"toml": "~3.0.0"
4950
},
5051
"devDependencies": {
5152
"ava": "^6.1.1",

test/md043-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
module.exports = {
77
MD012: false,
88
MD043: {
9-
headers: ['# First', '## Second', '### Third']
9+
headings: ['# First', '## Second', '### Third']
1010
}
1111
};

test/md043-config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"MD012": false,
33
"MD043": {
4-
"headers": [
4+
"headings": [
55
"# First",
66
"## Second",
77
"### Third"

test/md043-config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MD012 = false
2+
MD043 = { headings = ["# First", "## Second", "### Third"] }

test/test-config.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
default = true
2+
no-hard-tabs = false
3+
whitespace = false
4+
MD033 = false
5+
MD034 = false
6+
7+
[MD003]
8+
style = "atx"
9+
10+
[MD007]
11+
indent = 4
12+
13+
[MD013]
14+
line_length = 200

test/test-config.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
default: true
2+
no-hard-tabs: false
3+
whitespace: false
4+
MD033: false
5+
MD034: false
6+
MD003:
7+
style: atx
8+
MD007:
9+
indent: 2
10+
MD013:
11+
line_length: 200
12+

test/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,35 @@ test('configuration file can be JavaScript', async t => {
400400
t.is(result.exitCode, 0);
401401
});
402402

403+
test('configuration file can be TOML', async t => {
404+
const result = await execa('../markdownlint.js', ['--config', 'md043-config.toml', 'md043-config.md'], {stripFinalNewline: false});
405+
t.is(result.stdout, '');
406+
t.is(result.stderr, '');
407+
t.is(result.exitCode, 0);
408+
});
409+
410+
test('linting using a toml configuration file works', async t => {
411+
try {
412+
await execa('../markdownlint.js', ['--config', 'test-config.toml', '**/*.md'], {stripFinalNewline: false});
413+
t.fail();
414+
} catch (error) {
415+
t.is(error.stdout, '');
416+
t.is(error.stderr.match(errorPattern).length, 15);
417+
t.is(error.exitCode, 1);
418+
}
419+
});
420+
421+
test('linting using a yaml configuration file works', async t => {
422+
try {
423+
await execa('../markdownlint.js', ['--config', 'test-config.yaml', '**/*.md'], {stripFinalNewline: false});
424+
t.fail();
425+
} catch (error) {
426+
t.is(error.stdout, '');
427+
t.is(error.stderr.match(errorPattern).length, 15);
428+
t.is(error.exitCode, 1);
429+
}
430+
});
431+
403432
test('error on configuration file not found', async t => {
404433
try {
405434
await execa('../markdownlint.js', ['--config', 'non-existent-file-path.yaml', 'correct.md'], {stripFinalNewline: false});

0 commit comments

Comments
 (0)