Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to parse toml files like pyproject.toml #457

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add possibility to parse toml files like pyproject.toml
add small fix to js and json config file
  • Loading branch information
Benjamin Wilking committed Feb 13, 2024
commit c7994f39e8720bad61e604faa45e0c8a476ac62d
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, or INI files in the same [config format][config]. Further, configuration can be stored as TOML file under ```[tool.markdownlint]```

A sample configuration file:

Expand All @@ -102,6 +102,15 @@ A sample configuration file:
}
```

```toml
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
[tool.markdownlint]
default = true
heading-style = { style = "atx_closed" }
ul-indent = { indent = 4 }
no-hard-tabs = false
whitespace = false
```

For more examples, see [.markdownlint.jsonc][markdownlint-jsonc], [.markdownlint.yaml][markdownlint-yaml], or the [style folder][style-folder].

The CLI argument `--config` is not required.
Expand All @@ -118,6 +127,8 @@ 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.

> TOML configuration files must be provided via the `--config` argument; they are not automatically loaded.

## Exit codes

`markdownlint-cli` returns one of the following exit codes:
Expand Down
10 changes: 9 additions & 1 deletion markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function yamlParse(text) {
return require('js-yaml').load(text);
}

function tomlParse(text) {
return require('toml').parse(text).tool.markdownlint;
}

const exitCodes = {
lintFindings: 1,
failedToWriteOutputFile: 2,
Expand All @@ -44,7 +48,7 @@ const exitCodes = {
};

const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
const configParsers = [jsoncParse, yamlParse];
const configParsers = [jsoncParse, yamlParse, tomlParse];
const fsOptions = {encoding: 'utf8'};
const processCwd = process.cwd();

Expand Down Expand Up @@ -330,3 +334,7 @@ try {
console.error(error);
process.exit(exitCodes.unexpectedError);
}



"Unable to parse '../ci-analytics-poc/pyproject.toml'; Parser 0: Unable to parse JSON(C) content, InvalidSymbol (offset 1, length 23), InvalidSymbol (offset 26, length 10), InvalidSymbol (offset 37, length 1), EndOfFileExpected (offset 39, length 1); Parser 1: end of the stream or a document separator is expected (2:1)\n\n 1 | [tool.pytest.ini_options]\n 2 | pythonpath = [\n-----^\n 3 | \"ci_analytics_automation/issue ...\n 4 | ]; Parser 2: a is not defined"
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"ava": "^6.1.1",
Expand Down
2 changes: 1 addition & 1 deletion test/md043-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
module.exports = {
MD012: false,
MD043: {
headers: ['# First', '## Second', '### Third']
headings: ['# First', '## Second', '### Third']
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
}
};
2 changes: 1 addition & 1 deletion test/md043-config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"MD012": false,
"MD043": {
"headers": [
"headings": [
"# First",
"## Second",
"### Third"
Expand Down
3 changes: 3 additions & 0 deletions test/md043-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tool.markdownlint]
MD012 = false
MD043 = { headings = ["# First", "## Second", "### Third"] }
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ 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('error on configuration file not found', async t => {
try {
await execa('../markdownlint.js', ['--config', 'non-existent-file-path.yaml', 'correct.md'], {stripFinalNewline: false});
Expand Down