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 4 commits
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
10 changes: 9 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, INI, or TOML files in the same [config format][config].

A sample configuration file:

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

```toml
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
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 Down
9 changes: 7 additions & 2 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@ function yamlParse(text) {
return require('js-yaml').load(text);
}

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

const exitCodes = {
lintFindings: 1,
failedToWriteOutputFile: 2,
failedToLoadCustomRules: 3,
unexpectedError: 4
};

const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml'];
const configParsers = [jsoncParse, yamlParse];
const projectConfigFiles = ['.markdownlint.jsonc', '.markdownlint.json', '.markdownlint.yaml', '.markdownlint.yml', '.markdownlint.toml'];
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
const configParsers = [jsoncParse, yamlParse, tomlParse];
const fsOptions = {encoding: 'utf8'};
const processCwd = process.cwd();

Expand All @@ -53,6 +57,7 @@ function readConfiguration(userConfigFile) {

// Load from well-known config files
let config = rc('markdownlint', {});

for (const projectConfigFile of projectConfigFiles) {
try {
fs.accessSync(projectConfigFile, fs.R_OK);
Expand Down
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: 2 additions & 0 deletions test/config-files/toml/.markdownlint.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[no-trailing-punctuation]
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved
punctuation="$"
3 changes: 3 additions & 0 deletions test/config-files/toml/heading-dollar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Heading$
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved

Text
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
2 changes: 2 additions & 0 deletions test/md043-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MD012 = false
MD043 = { headings = ["# First", "## Second", "### Third"] }
9 changes: 9 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 Expand Up @@ -441,6 +448,8 @@ test('.markdownlint.jsonc in cwd is used automatically', getCwdConfigFileTest('j

test('.markdownlint.json in cwd is used automatically', getCwdConfigFileTest('json'));

test('.markdownlint.toml in cwd is used automatically', getCwdConfigFileTest('toml'));
DavidAnson marked this conversation as resolved.
Show resolved Hide resolved

test('.markdownlint.yaml in cwd is used automatically', getCwdConfigFileTest('yaml'));

test('.markdownlint.yml in cwd is used automatically', getCwdConfigFileTest('yml'));
Expand Down
Loading