Skip to content

Commit bc7c8e4

Browse files
authored
fix(rule): ignore non-http external link by default (#115)
fix #112
1 parent 174993d commit bc7c8e4

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"npm-run-all": "^4.1.5",
6060
"prettier": "^1.18.2",
6161
"textlint": "^11.2.5",
62-
"textlint-tester": "5.1.6"
62+
"textlint-tester": "^5.1.6"
6363
},
6464
"husky": {
6565
"hooks": {

src/no-dead-link.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ const DEFAULT_OPTIONS = {
1616
// Adopted from http://stackoverflow.com/a/3809435/951517
1717
const URI_REGEXP = /(?:https?:)?\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_+.~#?&//=]*)/g;
1818

19+
/**
20+
* Returns `true` if a given URI is https? url.
21+
* @param {string} uri
22+
* @return {boolean}
23+
*/
24+
function isHttp(uri) {
25+
const { protocol } = URL.parse(uri);
26+
return protocol === "http:" || protocol === "https:"
27+
}
1928
/**
2029
* Returns `true` if a given URI is relative.
2130
* @param {string} uri
@@ -168,6 +177,12 @@ function reporter(context, options = {}) {
168177
uri = URL.resolve(base, uri);
169178
}
170179

180+
// Ignore non http external link
181+
// https://github.com/textlint-rule/textlint-rule-no-dead-link/issues/112
182+
if (!isLocal(uri) && !isHttp(uri)) {
183+
return;
184+
}
185+
171186
const method =
172187
opts.preferGET.filter(
173188
(origin) => getURLOrigin(uri) === getURLOrigin(origin),
@@ -231,7 +246,7 @@ function reporter(context, options = {}) {
231246
if (typeof node.url === 'undefined') {
232247
return;
233248
}
234-
249+
235250
// [text](http://example.com)
236251
// ^
237252
const index = node.raw.indexOf(node.url) || 0;

test/no-dead-link.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const tester = new TextlintTester();
88

99
tester.run('no-dead-link', rule, {
1010
valid: [
11+
'should ignore non-http url [email address](mailto:mail.example.com) by default',
12+
'should ignore non-http url [ftp](ftp://example.com) by default',
13+
'should ignore non-http url [websockets](ws://example.com) by default',
1114
'should be able to check a link in Markdown: [example](https://example.com/)',
1215
'should be able to check a URL in Markdown: https://example.com/',
1316
'should success with retrying on error: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4461,7 +4461,7 @@ textlint-rule-helper@^2.1.1:
44614461
structured-source "^3.0.2"
44624462
unist-util-visit "^1.1.0"
44634463

4464-
4464+
textlint-tester@^5.1.6:
44654465
version "5.1.6"
44664466
resolved "https://registry.yarnpkg.com/textlint-tester/-/textlint-tester-5.1.6.tgz#0ea5d53a417748984d28e9a718d4af60a71b5f6c"
44674467
integrity sha512-vIid0nh6i240pRnvO9Hr2QopV/OVW+Sx+43Ah0Vy20ug5c2f8buio6+YHisM7sp+IwYA8GXvUn8uIV94S1EUMg==

0 commit comments

Comments
 (0)