Skip to content

Commit

Permalink
Add "gitignore" configuration option to ignore files specified in .gi…
Browse files Browse the repository at this point in the history
…tignore when linting (fixes #292).
  • Loading branch information
DavidAnson committed Apr 1, 2024
1 parent 4bad4cc commit a083dec
Show file tree
Hide file tree
Showing 20 changed files with 158 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -608,6 +611,7 @@ const createDirInfos = async (
globPatterns,
dirToDirInfo,
optionsOverride,
gitignore,
noRequire
) => {
await enumerateFiles(
Expand All @@ -616,6 +620,7 @@ const createDirInfos = async (
baseDir,
globPatterns,
dirToDirInfo,
gitignore,
noRequire
);
await enumerateParents(
Expand Down Expand Up @@ -1000,6 +1005,7 @@ const main = async (params) => {
globPatterns,
dirToDirInfo,
optionsOverride,
Boolean(baseMarkdownlintOptions.gitignore),
noRequire
);
// Output linting status
Expand Down
5 changes: 5 additions & 0 deletions schema/markdownlint-cli2-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions test/gitignore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewme.md
3 changes: 3 additions & 0 deletions test/gitignore/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"gitignore": true
}
1 change: 1 addition & 0 deletions test/gitignore/dir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir
14 changes: 14 additions & 0 deletions test/gitignore/dir/UPPER.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
6 changes: 6 additions & 0 deletions test/gitignore/dir/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# About #

Text text text
1. List
3. List
3. List
3 changes: 3 additions & 0 deletions test/gitignore/dir/subdir/info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Information
Text ` code1` text `code2 ` text

14 changes: 14 additions & 0 deletions test/gitignore/viewme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Title

> Tagline

# Description

Text text text
Text text text
Text text text

## Summary

Text text text
3 changes: 3 additions & 0 deletions test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
// Define a custom front matter pattern
"frontMatter": "<head>[^]*<\/head>",

// Ignore files referenced by .gitignore (only valid at root)
"gitignore": true,

// Define glob expressions to use (only valid at root)
"globs": [
"!*bout.md"
Expand Down
6 changes: 6 additions & 0 deletions test/markdownlint-cli2-test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ const testCases = ({
"exitCode": 0
});

testCase({
"name": "gitignore",
"args": [ "**/*.{md,MD}" ],
"exitCode": 1
});

testCase({
"name": "literal-files",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test("validateMarkdownlintConfigSchema", async (t) => {
});

test("validateMarkdownlintCli2ConfigSchema", async (t) => {
t.plan(88);
t.plan(89);

// Validate schema
// @ts-ignore
Expand Down
3 changes: 3 additions & 0 deletions test/markdownlint-cli2-yaml-example/.markdownlint-cli2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ fix: true
# Define a custom front matter pattern
frontMatter: "<head>[^]*<\/head>"

# Ignore files referenced by .gitignore (only valid at root)
gitignore: true

# Define glob expressions to use (only valid at root)
globs:
- "!*bout.md"
Expand Down
26 changes: 26 additions & 0 deletions test/snapshots/markdownlint-cli2-test-exec.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-exec.js.snap
Binary file not shown.
36 changes: 36 additions & 0 deletions test/snapshots/markdownlint-cli2-test-fs.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-fs.js.snap
Binary file not shown.
26 changes: 26 additions & 0 deletions test/snapshots/markdownlint-cli2-test-main.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified test/snapshots/markdownlint-cli2-test-main.js.snap
Binary file not shown.

0 comments on commit a083dec

Please sign in to comment.