Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rule): ignore non-http external link by default #115

Merged
merged 5 commits into from
Jun 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^1.18.2",
"textlint": "^11.2.5",
"textlint-tester": "5.1.6"
"textlint-tester": "^5.1.6"
},
"husky": {
"hooks": {
Expand Down
17 changes: 16 additions & 1 deletion src/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ const DEFAULT_OPTIONS = {
// Adopted from http://stackoverflow.com/a/3809435/951517
const URI_REGEXP = /(?:https?:)?\/\/(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_+.~#?&//=]*)/g;

/**
* Returns `true` if a given URI is https? url.
* @param {string} uri
* @return {boolean}
*/
function isHttp(uri) {
const { protocol } = URL.parse(uri);
return protocol === "http:" || protocol === "https:"
}
/**
* Returns `true` if a given URI is relative.
* @param {string} uri
Expand Down Expand Up @@ -168,6 +177,12 @@ function reporter(context, options = {}) {
uri = URL.resolve(base, uri);
}

// Ignore non http external link
// https://github.com/textlint-rule/textlint-rule-no-dead-link/issues/112
if (!isLocal(uri) && !isHttp(uri)) {
return;
}

const method =
opts.preferGET.filter(
(origin) => getURLOrigin(uri) === getURLOrigin(origin),
Expand Down Expand Up @@ -231,7 +246,7 @@ function reporter(context, options = {}) {
if (typeof node.url === 'undefined') {
return;
}

// [text](http://example.com)
// ^
const index = node.raw.indexOf(node.url) || 0;
Expand Down
3 changes: 3 additions & 0 deletions test/no-dead-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const tester = new TextlintTester();

tester.run('no-dead-link', rule, {
valid: [
'should ignore non-http url [email address](mailto:mail.example.com) by default',
'should ignore non-http url [ftp](ftp://example.com) by default',
'should ignore non-http url [websockets](ws://example.com) by default',
'should be able to check a link in Markdown: [example](https://example.com/)',
'should be able to check a URL in Markdown: https://example.com/',
'should success with retrying on error: [npm results for textlint](https://www.npmjs.com/search?q=textlint)',
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4461,7 +4461,7 @@ textlint-rule-helper@^2.1.1:
structured-source "^3.0.2"
unist-util-visit "^1.1.0"

[email protected]:
textlint-tester@^5.1.6:
version "5.1.6"
resolved "https://registry.yarnpkg.com/textlint-tester/-/textlint-tester-5.1.6.tgz#0ea5d53a417748984d28e9a718d4af60a71b5f6c"
integrity sha512-vIid0nh6i240pRnvO9Hr2QopV/OVW+Sx+43Ah0Vy20ug5c2f8buio6+YHisM7sp+IwYA8GXvUn8uIV94S1EUMg==
Expand Down