Skip to content

Commit

Permalink
Add -p/--ignore-path option to override .markdownlintignore (fixes #46).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Feb 5, 2020
1 parent 2721817 commit 2fb5506
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand All @@ -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

Expand Down
15 changes: 11 additions & 4 deletions markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions test/markdownlintignore/.ignorefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/*.md
subdir/*
54 changes: 54 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
});

0 comments on commit 2fb5506

Please sign in to comment.