This repository was archived by the owner on Dec 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit af6132b
Showing
11 changed files
with
571 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
charset = utf-8 | ||
indent_style = space | ||
indent_size = 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "airbnb", | ||
"parser": "babel-eslint", | ||
"env": { | ||
"node": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"excludeFiles": [ | ||
"coverage/", | ||
"node_modules/", | ||
"dist/*" | ||
], | ||
"preset": "airbnb", | ||
"maximumLineLength": { | ||
"value": 80, | ||
"allExcept": [ | ||
"regex", | ||
"urlComments" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
language: node_js | ||
node_js: | ||
- '5.0' | ||
sudo: false | ||
|
||
install: | ||
- npm install | ||
|
||
script: | ||
- npm test && npm run lint | ||
|
||
after_success: | ||
- npm run coveralls | ||
|
||
notifications: | ||
email: | ||
on_success: never | ||
on_failure: change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2016 wemake.service company | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# remark-lint-are-links-valid | ||
|
||
[](https://travis-ci.org/wemake-services/remark-lint-are-links-valid) [](https://coveralls.io/github/wemake-services/remark-lint-are-links-valid?branch=master) | ||
|
||
|
||
|
||
## Using the rule | ||
|
||
### Via `.remarkrc` | ||
|
||
```bash | ||
npm install -g remark | ||
npm install -g remark-lint | ||
npm install remark-lint-are-links-valid # local install! | ||
``` | ||
|
||
Then, set up your `.remarkrc`: | ||
|
||
```JSON | ||
{ | ||
"plugins": { | ||
"remark-lint": { | ||
"external": ["remark-lint-are-links-valid"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Now you can use the following command to run the lint: | ||
|
||
```bash | ||
remark --no-stdout xxx.md | ||
``` | ||
|
||
#### Using another ending symbol | ||
|
||
Set up your `.remarkrc`: | ||
|
||
```JSON | ||
{ | ||
"plugins": { | ||
"remark-lint": { | ||
"external": ["remark-lint-are-links-valid"], | ||
"are-links-valid": { | ||
"endings": ["...", ";", "."] | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```bash | ||
npm install -g remark | ||
npm install -g remark-lint | ||
npm install -g remark-lint-are-links-valid # global install! | ||
remark --no-stdout -u remark-lint="external:[\"remark-lint-are-links-valid\"]" xxx.md | ||
``` | ||
|
||
Note that the `lint=<lint_options>` option only works with `remark >= 1.1.1`. | ||
|
||
## License | ||
|
||
MIT, see [LICENSE.md](LICENCE.md) for details. | ||
|
||
This `README.md` is based on [this one](https://github.com/chcokr/mdast-lint-sentence-newline/blob/250b106c9e19b387270099cf16f17a84643f8944/README.md) by [@chcokr](https://github.com/chcokr) (MIT). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
'use strict'; | ||
|
||
var visit = require('unist-util-visit'); | ||
var rp = require('request-promise'); | ||
var url = require('url'); | ||
var Promise = require('bluebird'); | ||
var defaults = require('object.defaults'); | ||
|
||
function handleLinkDuplicateError(file, link) { | ||
file.warn('Link is a duplicate: ' + link.link.href, link.node); | ||
} | ||
|
||
function handleLinkError(file, link) { | ||
file.warn('Link is broken: ' + link.link.href, link.node); | ||
} | ||
|
||
function handleResponse(response, file, link, settings) { | ||
var code = response.statusCode; | ||
|
||
// TODO: what else can be wrong? | ||
if (!response.complete || !(/^2/.test('' + code) && !(code in settings.allowErrors))) { | ||
handleLinkError(file, link); | ||
} | ||
} | ||
|
||
function createRequest(file, link, settings) { | ||
var options = { | ||
method: 'GET', | ||
uri: link.link.href, | ||
resolveWithFullResponse: true, | ||
|
||
// Thinks to be overridden: | ||
timeout: settings.timeout, | ||
followRedirects: settings.allowRedirects | ||
}; | ||
|
||
var promise = rp(options).promise(); | ||
|
||
promise.then(function (r) { | ||
return handleResponse(r, file, link, settings); | ||
}).catch(function () { | ||
return handleLinkError(file, link); | ||
}); | ||
|
||
return promise; | ||
} | ||
|
||
function checkAndRemoveDubplicates(file, links, settings) { | ||
var duplicates = []; | ||
var valid = []; | ||
|
||
var uniq = links.map(function (link) { | ||
var obj = { | ||
count: 1, | ||
uri: '' + link.link.host + link.link.path, | ||
inst: link | ||
}; | ||
return obj; | ||
}).reduce(function (a, b) { | ||
var current = a[b.uri] || {}; | ||
a[b.uri] = { | ||
count: (current.count || 0) + b.count, | ||
inst: b.inst | ||
}; | ||
|
||
return a; | ||
}, {}); | ||
|
||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
|
||
try { | ||
for (var _iterator = Object.keys(uniq)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var i = _step.value; | ||
|
||
var item = uniq[i]; | ||
if (item.count !== 1 && settings.whiteListDomains.indexOf(item.inst.link.host) === -1) { | ||
duplicates.push(item.inst); | ||
} else { | ||
valid.push(item.inst); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
|
||
duplicates.map(function (item) { | ||
return handleLinkDuplicateError(file, item); | ||
}); | ||
return valid; | ||
} | ||
|
||
function areLinksValidCheck(ast, file, preferred, done) { | ||
var defaultSettings = { | ||
// Basic settings: | ||
allowRedirects: true, | ||
allowErrors: [], | ||
timeout: 5000, | ||
|
||
// These settings allow duplicate links validation: | ||
allowDuplicates: true, | ||
whiteListDomains: [] | ||
}; | ||
var settings = preferred || {}; | ||
defaults(settings, defaultSettings); | ||
|
||
var links = []; | ||
var promises = []; | ||
|
||
visit(ast, 'link', function (node) { | ||
var link = url.parse(node.url); | ||
|
||
if (link.host !== null) { | ||
// links without `host` are just `#hashes` | ||
links.push({ node: node, link: link }); | ||
} | ||
}); | ||
|
||
if (!settings.allowDuplicates) { | ||
links = checkAndRemoveDubplicates(file, links, settings); | ||
} | ||
|
||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
|
||
try { | ||
for (var _iterator2 = links[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var link = _step2.value; | ||
|
||
promises.push(createRequest(file, link, settings)); | ||
} | ||
|
||
// We should wait for all requests to finish: | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
|
||
Promise // `.reflect()` will make rejected promises valid: | ||
.all(promises.map(function (promise) { | ||
return promise.reflect(); | ||
})).then(function () { | ||
return done(); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
'are-links-valid': areLinksValidCheck | ||
}; |
Oops, something went wrong.