diff --git a/README.md b/README.md index 46010e737..f7493981f 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ markdownlint --help Options: -V, --version output the version number - -c, --config [configFile] configuration file (JSON, JSONC, JS, or YAML) + -c, --config [configFile] configuration file (JSON, JSONC, JS, YAML, or TOML) -d, --dot include files/folders with a dot (for example `.github`) -f, --fix fix basic errors (does not work with STDIN) -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 `markdownlint-cli` reuses [the rules][rules] from `markdownlint` package. -Configuration is stored in JSON, JSONC, YAML, or INI files in the same [config format][config]. +Configuration is stored in JSON, JSONC, YAML, INI, or TOML files in the same [config format][config]. A sample configuration file: @@ -102,7 +102,7 @@ A sample configuration file: } ``` -For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], or the [style folder][style-folder]. +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]. The CLI argument `--config` is not required. 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 `--enable` and `--disable` override configuration files; if a configuration file disables `MD123` and you pass `--enable MD123`, it will be enabled. If a rule is passed to both `--enable` and `--disable`, it will be disabled. -> JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern. +> - JS configuration files must be provided via the `--config` argument; they are not automatically loaded because running untrusted code is a security concern. +> - TOML configuration files must be provided via the `--config` argument; they are not automatically loaded. ## Exit codes diff --git a/markdownlint.js b/markdownlint.js index 8d6984704..c025abe84 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -36,6 +36,11 @@ function yamlParse(text) { return require('js-yaml').load(text); } +function tomlParse(text) { + // It is necessary to add the prototype manually because of https://github.com/BinaryMuse/toml-node/issues/55 + return require('deep-extend')({}, require('toml').parse(text)); +} + const exitCodes = { lintFindings: 1, failedToWriteOutputFile: 2, @@ -44,7 +49,8 @@ const exitCodes = { }; const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml']; -const configParsers = [jsoncParse, yamlParse]; +// TOML files can be (incorrectly) read by yamlParse (but not vice versa), so tomlParse needs to go before yamlParse +const configParsers = [jsoncParse, tomlParse, yamlParse]; const fsOptions = {encoding: 'utf8'}; const processCwd = process.cwd(); diff --git a/package-lock.json b/package-lock.json index f1746e366..57e22b02b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,8 @@ "jsonc-parser": "~3.2.1", "markdownlint": "~0.33.0", "minimatch": "~9.0.3", - "run-con": "~1.3.2" + "run-con": "~1.3.2", + "toml": "^3.0.0" }, "bin": { "markdownlint": "markdownlint.js" @@ -7015,6 +7016,11 @@ "node": ">=8.0" } }, + "node_modules/toml": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", + "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" + }, "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", @@ -12768,6 +12774,11 @@ "is-number": "^7.0.0" } }, + "toml": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/toml/-/toml-3.0.0.tgz", + "integrity": "sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==" + }, "tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", diff --git a/package.json b/package.json index 979bacd95..77b696968 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "jsonc-parser": "~3.2.1", "markdownlint": "~0.33.0", "minimatch": "~9.0.3", - "run-con": "~1.3.2" + "run-con": "~1.3.2", + "toml": "~3.0.0" }, "devDependencies": { "ava": "^6.1.1", diff --git a/test/md043-config.js b/test/md043-config.js index 77a6849d4..ea1c133e1 100644 --- a/test/md043-config.js +++ b/test/md043-config.js @@ -6,6 +6,6 @@ module.exports = { MD012: false, MD043: { - headers: ['# First', '## Second', '### Third'] + headings: ['# First', '## Second', '### Third'] } }; diff --git a/test/md043-config.json b/test/md043-config.json index d9844a936..990663de2 100644 --- a/test/md043-config.json +++ b/test/md043-config.json @@ -1,7 +1,7 @@ { "MD012": false, "MD043": { - "headers": [ + "headings": [ "# First", "## Second", "### Third" diff --git a/test/md043-config.toml b/test/md043-config.toml new file mode 100644 index 000000000..e40f3c285 --- /dev/null +++ b/test/md043-config.toml @@ -0,0 +1,2 @@ +MD012 = false +MD043 = { headings = ["# First", "## Second", "### Third"] } diff --git a/test/test-config.toml b/test/test-config.toml new file mode 100644 index 000000000..30e4697f6 --- /dev/null +++ b/test/test-config.toml @@ -0,0 +1,14 @@ +default = true +no-hard-tabs = false +whitespace = false +MD033 = false +MD034 = false + +[MD003] +style = "atx" + +[MD007] +indent = 4 + +[MD013] +line_length = 200 diff --git a/test/test-config.yaml b/test/test-config.yaml new file mode 100644 index 000000000..ce01fb632 --- /dev/null +++ b/test/test-config.yaml @@ -0,0 +1,12 @@ +default: true +no-hard-tabs: false +whitespace: false +MD033: false +MD034: false +MD003: + style: atx +MD007: + indent: 2 +MD013: + line_length: 200 + diff --git a/test/test.js b/test/test.js index 70f8d84c6..59a31c518 100644 --- a/test/test.js +++ b/test/test.js @@ -400,6 +400,35 @@ test('configuration file can be JavaScript', async t => { t.is(result.exitCode, 0); }); +test('configuration file can be TOML', async t => { + const result = await execa('../markdownlint.js', ['--config', 'md043-config.toml', 'md043-config.md'], {stripFinalNewline: false}); + t.is(result.stdout, ''); + t.is(result.stderr, ''); + t.is(result.exitCode, 0); +}); + +test('linting using a toml configuration file works', async t => { + try { + await execa('../markdownlint.js', ['--config', 'test-config.toml', '**/*.md'], {stripFinalNewline: false}); + t.fail(); + } catch (error) { + t.is(error.stdout, ''); + t.is(error.stderr.match(errorPattern).length, 15); + t.is(error.exitCode, 1); + } +}); + +test('linting using a yaml configuration file works', async t => { + try { + await execa('../markdownlint.js', ['--config', 'test-config.yaml', '**/*.md'], {stripFinalNewline: false}); + t.fail(); + } catch (error) { + t.is(error.stdout, ''); + t.is(error.stderr.match(errorPattern).length, 15); + t.is(error.exitCode, 1); + } +}); + test('error on configuration file not found', async t => { try { await execa('../markdownlint.js', ['--config', 'non-existent-file-path.yaml', 'correct.md'], {stripFinalNewline: false});