Skip to content

Commit 36ab80d

Browse files
Surgoazu
authored andcommitted
feat(options): Support Glob format for ignored list (#104)
* Install minimatch * Ignore glob matched urls * Update readme
1 parent 4600810 commit 36ab80d

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

ReadMe.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ Example:
103103
```json
104104
"no-dead-link": {
105105
"ignore": [
106-
"http://example.com/not-exist/index.html"
106+
"http://example.com/not-exist/index.html",
107+
"http://example.com/*" # Glob format
107108
]
108109
}
109110
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"fs-extra": "^5.0.0",
2727
"get-url-origin": "^1.0.1",
2828
"isomorphic-fetch": "^2.2.1",
29+
"minimatch": "^3.0.4",
2930
"textlint-rule-helper": "^2.0.0"
3031
},
3132
"lint-staged": {

src/no-dead-link.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { RuleHelper } from 'textlint-rule-helper';
22
import fetch from 'isomorphic-fetch';
33
import URL from 'url';
44
import fs from 'fs-extra';
5+
import minimatch from 'minimatch';
56
import { isAbsolute } from 'path';
67
import { getURLOrigin } from 'get-url-origin';
78

@@ -51,6 +52,10 @@ function isRedirect(code) {
5152
);
5253
}
5354

55+
function isIgnored(uri, ignore = []) {
56+
return ignore.some((pattern) => minimatch(uri, pattern));
57+
}
58+
5459
/**
5560
* Checks if a given URI is alive or not.
5661
* @param {string} uri
@@ -139,7 +144,7 @@ function reporter(context, options = {}) {
139144
* @param {number} index column number the URI is located at.
140145
*/
141146
const lint = async ({ node, uri, index }) => {
142-
if (opts.ignore.indexOf(uri) !== -1) {
147+
if (isIgnored(uri, opts.ignore)) {
143148
return;
144149
}
145150

test/no-dead-link.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ tester.run('no-dead-link', rule, {
3636
ignore: ['https://example.com/404.html'],
3737
},
3838
},
39+
{
40+
text:
41+
'should ignore URLs in the "ignore" option that glob formatted: https://example.com/404.html shouldn\'t be checked.',
42+
options: {
43+
ignore: ['https://example.com/*'],
44+
},
45+
},
3946
{
4047
text:
4148
'should ignore relative URIs when `checkRelative` is false: [test](./a.md).',

0 commit comments

Comments
 (0)