diff --git a/README.md b/README.md index e33a80ec5..f277465a4 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ markdownlint --help -s, --stdin read from STDIN (does not work with files) -o, --output [outputFile] write issues to file (no console) -c, --config [configFile] configuration file (JSON, JSONC, or YAML) - -i, --ignore [file|directory|glob] files to ignore/exclude + -i, --ignore [file|directory|glob] file(s) to ignore/exclude + -p, --ignore-path [file] path to file with ignore pattern(s) -r, --rules [file|directory|glob|package] custom rule files ``` @@ -46,13 +47,14 @@ Linux Bash: `markdownlint '**/*.md' --ignore node_modules` ### Ignoring files -If present in the current folder, a `.markdownlintignore` file will be used to ignore files and /or directories according to the rules for [gitignore][gitignore]. +If present in the current folder, a `.markdownlintignore` file will be used to ignore files and/or directories according to the rules for [gitignore][gitignore]. +If the `-p`/`--ignore-path` option is present, the specified file will be used instead of `.markdownlintignore`. The order of operations is: -- Enumerate files/directories/globs on the command line -- Apply exclusions from `.markdownlintignore` -- Apply exclusions from `-i`/`--ignore` option(s) +- Enumerate files/directories/globs passed on the command line +- Apply exclusions from `-p`/`--ignore-path` (if specified) or `.markdownlintignore` (if present) +- Apply exclusions from any `-i`/`--ignore` option(s) that are specified ### Fixing errors diff --git a/markdownlint.js b/markdownlint.js index 59b853113..29ccb59db 100755 --- a/markdownlint.js +++ b/markdownlint.js @@ -170,7 +170,8 @@ program .option('-s, --stdin', 'read from STDIN (does not work with files)') .option('-o, --output [outputFile]', 'write issues to file (no console)') .option('-c, --config [configFile]', 'configuration file (JSON, JSONC, or YAML)') - .option('-i, --ignore [file|directory|glob]', 'files to ignore/exclude', concatArray, []) + .option('-i, --ignore [file|directory|glob]', 'file(s) to ignore/exclude', concatArray, []) + .option('-p, --ignore-path [file]', 'path to file with ignore pattern(s)') .option('-r, --rules [file|directory|glob|package]', 'custom rule files', concatArray, []); program.parse(process.argv); @@ -216,10 +217,16 @@ function loadCustomRules(rules) { })); } -const markdownlintIgnore = '.markdownlintignore'; +let ignorePath = '.markdownlintignore'; +let {existsSync} = fs; +if (program.ignorePath) { + ignorePath = program.ignorePath; + existsSync = () => true; +} + let ignoreFilter = () => true; -if (fs.existsSync(markdownlintIgnore)) { - const ignoreText = fs.readFileSync(markdownlintIgnore, fsOptions); +if (existsSync(ignorePath)) { + const ignoreText = fs.readFileSync(ignorePath, fsOptions); const ignoreInstance = ignore().add(ignoreText); ignoreFilter = fileInfo => !ignoreInstance.ignores(fileInfo.original); } diff --git a/test/markdownlintignore/.ignorefile b/test/markdownlintignore/.ignorefile new file mode 100644 index 000000000..2fc73783b --- /dev/null +++ b/test/markdownlintignore/.ignorefile @@ -0,0 +1,2 @@ +**/*.md +subdir/* diff --git a/test/test.js b/test/test.js index 950a4c383..9acfc6005 100644 --- a/test/test.js +++ b/test/test.js @@ -645,3 +645,57 @@ test('.markdownlintignore is applied correctly', async t => { t.deepEqual(error.stderr, expected); } }); + +test('--ignore-path works with .markdownlintignore', async t => { + try { + await execa( + path.resolve('..', 'markdownlint.js'), ['--ignore-path', '.markdownlintignore', '.'], { + cwd: path.join(__dirname, 'markdownlintignore'), + stripFinalNewline: false + }); + t.fail(); + } catch (error) { + const expected = [ + 'incorrect.md:1 MD047/single-trailing-newline Files should end with a single newline character', + 'subdir/incorrect.markdown:1 MD047/single-trailing-newline Files should end with a single newline character', + '' + ].join('\n'); + t.deepEqual(error.stdout, ''); + t.deepEqual(error.stderr, expected); + } +}); + +test('--ignore-path works with .ignorefile', async t => { + try { + await execa( + path.resolve('..', 'markdownlint.js'), ['--ignore-path', '.ignorefile', '.'], { + cwd: path.join(__dirname, 'markdownlintignore'), + stripFinalNewline: false + }); + t.fail(); + } catch (error) { + const expected = [ + 'incorrect.markdown:1 MD047/single-trailing-newline Files should end with a single newline character', + '' + ].join('\n'); + t.deepEqual(error.stdout, ''); + t.deepEqual(error.stderr, expected); + } +}); + +test('--ignore-path fails for missing file', async t => { + const missingFile = 'missing-file'; + try { + await execa( + path.resolve('..', 'markdownlint.js'), + ['--ignore-path', missingFile, '.'], { + cwd: path.join(__dirname, 'markdownlintignore'), + stripFinalNewline: false + } + ); + t.fail(); + } catch (error) { + t.deepEqual(error.stdout, ''); + t.true(/ENOENT.*no such file or directory/i.test(error.stderr)); + } +});