From e2a5ba9b25b1d8375cc7b3c11682035a6f9f3bbe Mon Sep 17 00:00:00 2001 From: David Anson Date: Mon, 17 Aug 2020 22:27:31 -0700 Subject: [PATCH] Convert .markdownlintignore to ignores option in .markdownlint-cli2.jsonc (fixes #1). --- README.md | 19 +++++------- markdownlint-cli2.js | 31 ++++++------------- ...rkdownlintignore.stderr => ignores.stderr} | 0 test/ignores/.markdownlint-cli2.jsonc | 6 ++++ .../dir/about.md | 0 .../dir/subdir/info.md | 0 .../{markdownlintignore => ignores}/viewme.md | 0 .../.markdownlint-cli2.jsonc | 5 +++ .../ignoreme.md | 1 + test/markdownlint-cli2-test.js | 2 +- test/markdownlintignore/.markdownlintignore | 3 -- 11 files changed, 31 insertions(+), 36 deletions(-) rename test/{markdownlintignore.stderr => ignores.stderr} (100%) create mode 100644 test/ignores/.markdownlint-cli2.jsonc rename test/{markdownlintignore => ignores}/dir/about.md (100%) rename test/{markdownlintignore => ignores}/dir/subdir/info.md (100%) rename test/{markdownlintignore => ignores}/viewme.md (100%) create mode 100644 test/markdownlint-cli2-jsonc-example/ignoreme.md delete mode 100644 test/markdownlintignore/.markdownlintignore diff --git a/README.md b/README.md index 1608cd8d..ca431ce8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. @@ -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/ diff --git a/markdownlint-cli2.js b/markdownlint-cli2.js index 9a5a2411..da31db54 100644 --- a/markdownlint-cli2.js +++ b/markdownlint-cli2.js @@ -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 @@ -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) => { @@ -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); @@ -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 diff --git a/test/markdownlintignore.stderr b/test/ignores.stderr similarity index 100% rename from test/markdownlintignore.stderr rename to test/ignores.stderr diff --git a/test/ignores/.markdownlint-cli2.jsonc b/test/ignores/.markdownlint-cli2.jsonc new file mode 100644 index 00000000..e9d486ae --- /dev/null +++ b/test/ignores/.markdownlint-cli2.jsonc @@ -0,0 +1,6 @@ +{ + "ignores": [ + "*.md", + "dir/*/*.md" + ] +} diff --git a/test/markdownlintignore/dir/about.md b/test/ignores/dir/about.md similarity index 100% rename from test/markdownlintignore/dir/about.md rename to test/ignores/dir/about.md diff --git a/test/markdownlintignore/dir/subdir/info.md b/test/ignores/dir/subdir/info.md similarity index 100% rename from test/markdownlintignore/dir/subdir/info.md rename to test/ignores/dir/subdir/info.md diff --git a/test/markdownlintignore/viewme.md b/test/ignores/viewme.md similarity index 100% rename from test/markdownlintignore/viewme.md rename to test/ignores/viewme.md diff --git a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc index a9b2e678..26517536 100644 --- a/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc +++ b/test/markdownlint-cli2-jsonc-example/.markdownlint-cli2.jsonc @@ -19,6 +19,11 @@ // Define a custom front matter pattern "frontMatter": "[^]*<\/head>", + // Define glob expressions to ignore + "ignores": [ + "ignore*.md" + ], + // Use a plugin to recognize math "markdownItPlugins": [ [ "@iktakahiro/markdown-it-katex" ] diff --git a/test/markdownlint-cli2-jsonc-example/ignoreme.md b/test/markdownlint-cli2-jsonc-example/ignoreme.md new file mode 100644 index 00000000..4263a364 --- /dev/null +++ b/test/markdownlint-cli2-jsonc-example/ignoreme.md @@ -0,0 +1 @@ +Heading diff --git a/test/markdownlint-cli2-test.js b/test/markdownlint-cli2-test.js index c886a706..f3e9b6e7 100644 --- a/test/markdownlint-cli2-test.js +++ b/test/markdownlint-cli2-test.js @@ -245,7 +245,7 @@ testCase({ }); testCase({ - "name": "markdownlintignore", + "name": "ignores", "args": [ "**/*.md" ], "exitCode": 1 }); diff --git a/test/markdownlintignore/.markdownlintignore b/test/markdownlintignore/.markdownlintignore deleted file mode 100644 index 1771bd21..00000000 --- a/test/markdownlintignore/.markdownlintignore +++ /dev/null @@ -1,3 +0,0 @@ -# comment -*.md -dir/*/*.md