Skip to content

Commit

Permalink
Convert .markdownlintignore to ignores option in .markdownlint-cli2.j…
Browse files Browse the repository at this point in the history
…sonc (fixes #1).
  • Loading branch information
DavidAnson committed Aug 18, 2020
1 parent c188550 commit e2a5ba9
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 36 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ markdownlint-cli2 "**/*.md" "#node_modules"
- See the [Configuration][markdownlint-configuration] section of the
`markdownlint` documentation for information about the inline comment syntax.

### `.markdownlintignore`

- The format of this file is similar to [`.npmignore`][npmignore] and consists
of one glob pattern per line.
- These glob patterns are negated (by adding a leading `!`) and appended to the
end of the command-line arguments.
- Blank lines and lines that begin with the `#` character are ignored.

### `.markdownlint-cli2.jsonc`

- The format of this file is a [JSONC][jsonc] object similar to the
Expand All @@ -111,6 +103,12 @@ markdownlint-cli2 "**/*.md" "#node_modules"
- The `String` is passed as the `pattern` parameter to the
[`RegExp` constructor][regexp-constructor]
- For example: `(^---\s*$[^]*?^---\s*$)(\r\n|\r|\n|$)`
- `ignores`: `Array` of `Strings` defining glob expressions to ignore when
linting
- Glob expressions are negated (by adding a leading `!`) and appended to the
command-line arguments
- For performance reasons, this setting is valid only in the directory from
which `markdownlint-cli2` is run
- `markdownItPlugins`: `Array` of `Array`s, each of which has a `String`
naming a [markdown-it plugin][markdown-it-plugins] followed by parameters
- Plugins can be used to add support for additional Markdown syntax
Expand All @@ -124,8 +122,8 @@ markdownlint-cli2 "**/*.md" "#node_modules"
- Formatters can be used to customize the tool's output for different
scenarios
- For example: `[ [ "formatter-name", param_0, param_1, ... ], ... ]`
- This setting affects *all* output, so is valid only in the directory
`markdownlint-cli2` is run from
- This setting affects all output, so is valid only in the directory from
which `markdownlint-cli2` is run
- Settings in this file apply to the directory it is in and all subdirectories.
- Settings **merge with** those applied by any versions of this file in a parent
directory.
Expand Down Expand Up @@ -205,7 +203,6 @@ markdownlint-cli2 "**/*.md" "#node_modules"
[nodejs-require]: https://nodejs.org/api/modules.html#modules_require_id
[npm-image]: https://img.shields.io/npm/v/markdownlint-cli2.svg
[npm-url]: https://www.npmjs.com/package/markdownlint-cli2
[npmignore]: https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package
[regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
[regexp-constructor]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp
[vscode]: https://code.visualstudio.com/
Expand Down
31 changes: 10 additions & 21 deletions markdownlint-cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const markdownlintRuleHelpers = require("markdownlint-rule-helpers");
// Variables
const markdownlintPromise = util.promisify(markdownlint);
const markdownlintReadConfigPromise = util.promisify(markdownlint.readConfig);
const crlfRe = /\r\n?|\n/gu;
const utf8 = "utf8";

// Parse JSONC text
Expand Down Expand Up @@ -62,25 +61,7 @@ ${name} "**/*.md" "#node_modules"`
return 1;
}

// Read ignore globs to pass as globby patterns
const markdownlintIgnore = ".markdownlintignore";
await fs.access(markdownlintIgnore).
then(
() => fs.readFile(markdownlintIgnore, utf8).
then(
(content) => {
const blankOrCommentRe = /^#|^\s*$/u;
const ignorePatterns = content.
split(crlfRe).
filter((line) => !blankOrCommentRe.test(line)).
map((glob) => `!${glob.trim()}`);
globPatterns.push(...ignorePatterns);
}
),
() => null
);

// Enumerate glob patterns and build directory info list
// Read base ignore globs to pass as globby patterns (best performance)
const tasks = [];
const dirToDirInfo = {};
const readConfig = (dir, name, otherwise) => {
Expand Down Expand Up @@ -144,6 +125,15 @@ ${name} "**/*.md" "#node_modules"`
}
return dirInfo;
};
getAndProcessDirInfo(".");
await Promise.all(tasks);
tasks.length = 0;
const baseMarkdownlintOptions = dirToDirInfo["."].markdownlintOptions || {};
const ignorePatterns = (baseMarkdownlintOptions.ignores || []).
map((glob) => `!${glob}`);
globPatterns.push(...ignorePatterns);

// Enumerate files from globs and build directory info list
for await (const file of globby.stream(globPatterns)) {
// @ts-ignore
let dir = path.dirname(file);
Expand All @@ -161,7 +151,6 @@ ${name} "**/*.md" "#node_modules"`
}
getAndProcessDirInfo(".");
await Promise.all(tasks);
const baseMarkdownlintOptions = dirToDirInfo["."].markdownlintOptions || {};
tasks.length = 0;

// Merge file lists with identical configuration
Expand Down
File renamed without changes.
6 changes: 6 additions & 0 deletions test/ignores/.markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ignores": [
"*.md",
"dir/*/*.md"
]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 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,11 @@
// Define a custom front matter pattern
"frontMatter": "<head>[^]*<\/head>",

// Define glob expressions to ignore
"ignores": [
"ignore*.md"
],

// Use a plugin to recognize math
"markdownItPlugins": [
[ "@iktakahiro/markdown-it-katex" ]
Expand Down
1 change: 1 addition & 0 deletions test/markdownlint-cli2-jsonc-example/ignoreme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Heading
2 changes: 1 addition & 1 deletion test/markdownlint-cli2-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ testCase({
});

testCase({
"name": "markdownlintignore",
"name": "ignores",
"args": [ "**/*.md" ],
"exitCode": 1
});
Expand Down
3 changes: 0 additions & 3 deletions test/markdownlintignore/.markdownlintignore

This file was deleted.

0 comments on commit e2a5ba9

Please sign in to comment.