diff --git a/README.md b/README.md index a6d09aa4..17873d92 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,10 @@ of the rules within. - The `String` is passed as the `pattern` parameter to the [`RegExp` constructor][regexp-constructor] - For example: `(^---\s*$[^]*?^---\s*$)(\r\n|\r|\n|$)` + - `gitignore`: `Boolean` value to ignore files referenced by `.gitignore` when + linting + - This top-level setting is valid **only** in the directory from which + `markdownlint-cli2` is run - `globs`: `Array` of `String`s defining glob expressions to append to the command-line arguments - This setting can be used instead of (or in addition to) passing globs on diff --git a/markdownlint-cli2.js b/markdownlint-cli2.js index 129a035f..9818f231 100755 --- a/markdownlint-cli2.js +++ b/markdownlint-cli2.js @@ -476,14 +476,17 @@ const enumerateFiles = async ( baseDir, globPatterns, dirToDirInfo, + gitignore, noRequire ) => { const tasks = []; + /** @type {import("globby").Options} */ const globbyOptions = { "absolute": true, "cwd": baseDir, "dot": true, "expandDirectories": false, + gitignore, "suppressErrors": true, fs }; @@ -608,6 +611,7 @@ const createDirInfos = async ( globPatterns, dirToDirInfo, optionsOverride, + gitignore, noRequire ) => { await enumerateFiles( @@ -616,6 +620,7 @@ const createDirInfos = async ( baseDir, globPatterns, dirToDirInfo, + gitignore, noRequire ); await enumerateParents( @@ -1000,6 +1005,7 @@ const main = async (params) => { globPatterns, dirToDirInfo, optionsOverride, + Boolean(baseMarkdownlintOptions.gitignore), noRequire ); // Output linting status diff --git a/schema/markdownlint-cli2-config-schema.json b/schema/markdownlint-cli2-config-schema.json index bb807b8e..934e30a7 100644 --- a/schema/markdownlint-cli2-config-schema.json +++ b/schema/markdownlint-cli2-config-schema.json @@ -35,6 +35,11 @@ "minLength": 1, "default": "" }, + "gitignore": { + "description": "Whether to ignore files referenced by .gitignore when linting (only valid at the root) : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.12.1/README.md#markdownlint-cli2jsonc", + "type": "boolean", + "default": false + }, "globs": { "description": "Glob expressions to include when linting (only valid at the root) : https://github.com/DavidAnson/markdownlint-cli2/blob/v0.12.1/README.md#markdownlint-cli2jsonc", "type": "array", diff --git a/test/gitignore/.gitignore b/test/gitignore/.gitignore new file mode 100644 index 00000000..b7671aeb --- /dev/null +++ b/test/gitignore/.gitignore @@ -0,0 +1 @@ +viewme.md \ No newline at end of file diff --git a/test/gitignore/.markdownlint-cli2.jsonc b/test/gitignore/.markdownlint-cli2.jsonc new file mode 100644 index 00000000..3fe7bdc4 --- /dev/null +++ b/test/gitignore/.markdownlint-cli2.jsonc @@ -0,0 +1,3 @@ +{ + "gitignore": true +} diff --git a/test/gitignore/dir/.gitignore b/test/gitignore/dir/.gitignore new file mode 100644 index 00000000..8bbe8a53 --- /dev/null +++ b/test/gitignore/dir/.gitignore @@ -0,0 +1 @@ +subdir \ No newline at end of file diff --git a/test/gitignore/dir/UPPER.MD b/test/gitignore/dir/UPPER.MD new file mode 100644 index 00000000..d60ebf5a --- /dev/null +++ b/test/gitignore/dir/UPPER.MD @@ -0,0 +1,14 @@ +# Title + +> Tagline + + +# Description + +Text text text +Text text text +Text text text + +## Summary + +Text text text \ No newline at end of file diff --git a/test/gitignore/dir/about.md b/test/gitignore/dir/about.md new file mode 100644 index 00000000..56d31c78 --- /dev/null +++ b/test/gitignore/dir/about.md @@ -0,0 +1,6 @@ +# About # + +Text text text +1. List +3. List +3. List diff --git a/test/gitignore/dir/subdir/info.md b/test/gitignore/dir/subdir/info.md new file mode 100644 index 00000000..fc9dba79 --- /dev/null +++ b/test/gitignore/dir/subdir/info.md @@ -0,0 +1,3 @@ +## Information +Text ` code1` text `code2 ` text + diff --git a/test/gitignore/viewme.md b/test/gitignore/viewme.md new file mode 100644 index 00000000..d60ebf5a --- /dev/null +++ b/test/gitignore/viewme.md @@ -0,0 +1,14 @@ +# Title + +> Tagline + + +# Description + +Text text text +Text text text +Text text text + +## Summary + +Text text text \ No newline at end of file diff --git a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc index babf3e7e..9ae03241 100644 --- a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc +++ b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc @@ -19,6 +19,9 @@ // Define a custom front matter pattern "frontMatter": "[^]*<\/head>", + // Ignore files referenced by .gitignore (only valid at root) + "gitignore": true, + // Define glob expressions to use (only valid at root) "globs": [ "!*bout.md" diff --git a/test/markdownlint-cli2-test-cases.js b/test/markdownlint-cli2-test-cases.js index 598408da..e7912902 100644 --- a/test/markdownlint-cli2-test-cases.js +++ b/test/markdownlint-cli2-test-cases.js @@ -563,6 +563,12 @@ const testCases = ({ "exitCode": 0 }); + testCase({ + "name": "gitignore", + "args": [ "**/*.{md,MD}" ], + "exitCode": 1 + }); + testCase({ "name": "literal-files", "args": [ diff --git a/test/markdownlint-cli2-test.js b/test/markdownlint-cli2-test.js index 39708993..31e47bb8 100644 --- a/test/markdownlint-cli2-test.js +++ b/test/markdownlint-cli2-test.js @@ -109,7 +109,7 @@ test("validateMarkdownlintConfigSchema", async (t) => { }); test("validateMarkdownlintCli2ConfigSchema", async (t) => { - t.plan(88); + t.plan(89); // Validate schema // @ts-ignore diff --git a/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml b/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml index 7f53dda7..10140244 100644 --- a/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml +++ b/test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml @@ -17,6 +17,9 @@ fix: true # Define a custom front matter pattern frontMatter: "[^]*<\/head>" +# Ignore files referenced by .gitignore (only valid at root) +gitignore: true + # Define glob expressions to use (only valid at root) globs: - "!*bout.md" diff --git a/test/snapshots/markdownlint-cli2-test-exec.js.md b/test/snapshots/markdownlint-cli2-test-exec.js.md index 5a1c6fef..5038d1f6 100644 --- a/test/snapshots/markdownlint-cli2-test-exec.js.md +++ b/test/snapshots/markdownlint-cli2-test-exec.js.md @@ -1579,6 +1579,32 @@ Generated by [AVA](https://avajs.dev). `, } +## gitignore (exec) + +> Snapshot 1 + + { + exitCode: 1, + formatterCodeQuality: '', + formatterJson: '', + formatterJunit: '', + formatterSarif: '', + stderr: `dir/about.md:1:1 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊ + dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊ + dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊ + dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊ + dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊ + dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊ + dir/UPPER.MD:12:1 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊ + dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊ + `, + stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ + Finding: **/*.{md,MD}␊ + Linting: 2 file(s)␊ + Summary: 8 error(s)␊ + `, + } + ## literal-files (exec) > Snapshot 1 diff --git a/test/snapshots/markdownlint-cli2-test-exec.js.snap b/test/snapshots/markdownlint-cli2-test-exec.js.snap index 7b91d06a..0c0a7e0f 100644 Binary files a/test/snapshots/markdownlint-cli2-test-exec.js.snap and b/test/snapshots/markdownlint-cli2-test-exec.js.snap differ diff --git a/test/snapshots/markdownlint-cli2-test-fs.js.md b/test/snapshots/markdownlint-cli2-test-fs.js.md index b031e593..d3762187 100644 --- a/test/snapshots/markdownlint-cli2-test-fs.js.md +++ b/test/snapshots/markdownlint-cli2-test-fs.js.md @@ -1355,6 +1355,42 @@ Generated by [AVA](https://avajs.dev). `, } +## gitignore (fs) + +> Snapshot 1 + + { + exitCode: 1, + formatterCodeQuality: '', + formatterJson: '', + formatterJunit: '', + formatterSarif: '', + stderr: `dir/about.md:1:1 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊ + dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊ + dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊ + dir/subdir/info.md:1 MD022/blanks-around-headings Headings should be surrounded by blank lines [Expected: 1; Actual: 0; Below] [Context: "## Information"]␊ + dir/subdir/info.md:1 MD041/first-line-heading/first-line-h1 First line in a file should be a top-level heading [Context: "## Information"]␊ + dir/subdir/info.md:2:6 MD038/no-space-in-code Spaces inside code span elements [Context: "\` code1\`"]␊ + dir/subdir/info.md:2:20 MD038/no-space-in-code Spaces inside code span elements [Context: "\`code2 \`"]␊ + dir/subdir/info.md:4 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊ + dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊ + dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊ + dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊ + dir/UPPER.MD:12:1 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊ + dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊ + viewme.md:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊ + viewme.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊ + viewme.md:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊ + viewme.md:12:1 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊ + viewme.md:14:14 MD047/single-trailing-newline Files should end with a single newline character␊ + `, + stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ + Finding: **/*.{md,MD}␊ + Linting: 4 file(s)␊ + Summary: 18 error(s)␊ + `, + } + ## literal-files (fs) > Snapshot 1 diff --git a/test/snapshots/markdownlint-cli2-test-fs.js.snap b/test/snapshots/markdownlint-cli2-test-fs.js.snap index c47caf5a..be0ab2a9 100644 Binary files a/test/snapshots/markdownlint-cli2-test-fs.js.snap and b/test/snapshots/markdownlint-cli2-test-fs.js.snap differ diff --git a/test/snapshots/markdownlint-cli2-test-main.js.md b/test/snapshots/markdownlint-cli2-test-main.js.md index 5ac27d61..bd0fce38 100644 --- a/test/snapshots/markdownlint-cli2-test-main.js.md +++ b/test/snapshots/markdownlint-cli2-test-main.js.md @@ -1579,6 +1579,32 @@ Generated by [AVA](https://avajs.dev). `, } +## gitignore (main) + +> Snapshot 1 + + { + exitCode: 1, + formatterCodeQuality: '', + formatterJson: '', + formatterJunit: '', + formatterSarif: '', + stderr: `dir/about.md:1:1 MD021/no-multiple-space-closed-atx Multiple spaces inside hashes on closed atx style heading [Context: "# About #"]␊ + dir/about.md:4 MD032/blanks-around-lists Lists should be surrounded by blank lines [Context: "1. List"]␊ + dir/about.md:5:1 MD029/ol-prefix Ordered list item prefix [Expected: 2; Actual: 3; Style: 1/2/3]␊ + dir/UPPER.MD:3:10 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]␊ + dir/UPPER.MD:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]␊ + dir/UPPER.MD:6 MD025/single-title/single-h1 Multiple top-level headings in the same document [Context: "# Description"]␊ + dir/UPPER.MD:12:1 MD019/no-multiple-space-atx Multiple spaces after hash on atx style heading [Context: "## Summary"]␊ + dir/UPPER.MD:14:14 MD047/single-trailing-newline Files should end with a single newline character␊ + `, + stdout: `markdownlint-cli2 vX.Y.Z (markdownlint vX.Y.Z)␊ + Finding: **/*.{md,MD}␊ + Linting: 2 file(s)␊ + Summary: 8 error(s)␊ + `, + } + ## literal-files (main) > Snapshot 1 diff --git a/test/snapshots/markdownlint-cli2-test-main.js.snap b/test/snapshots/markdownlint-cli2-test-main.js.snap index 99f89f37..de214d96 100644 Binary files a/test/snapshots/markdownlint-cli2-test-main.js.snap and b/test/snapshots/markdownlint-cli2-test-main.js.snap differ