Skip to content
This repository was archived by the owner on Dec 21, 2019. It is now read-only.

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jun 5, 2016
0 parents commit af6132b
Show file tree
Hide file tree
Showing 11 changed files with 571 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
10 changes: 10 additions & 0 deletions .editorconfig
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

7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"node": true
}
}
87 changes: 87 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#### joe made this: http://goel.io/joe

#####=== OSX ===#####
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

#####=== Windows ===#####
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

#####=== Linux ===#####
*~

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

#####=== Node ===#####

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

Expand Down
15 changes: 15 additions & 0 deletions .jscs.json
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"
]
}
}
18 changes: 18 additions & 0 deletions .travis.yml
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
8 changes: 8 additions & 0 deletions LICENSE.md
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.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# remark-lint-are-links-valid

[![Build Status](https://travis-ci.org/wemake-services/remark-lint-are-links-valid.svg?branch=master)](https://travis-ci.org/wemake-services/remark-lint-are-links-valid) [![Coverage Status](https://coveralls.io/repos/github/wemake-services/remark-lint-are-links-valid/badge.svg?branch=master)](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).
172 changes: 172 additions & 0 deletions dist/are-links-valid.js
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
};
Loading

0 comments on commit af6132b

Please sign in to comment.