152 lines
3.9 KiB
JavaScript
152 lines
3.9 KiB
JavaScript
// Generated by CoffeeScript 1.9.3
|
|
(function() {
|
|
var Link, URI, cheerio, fileExists, isHTML, path;
|
|
|
|
URI = require('urijs');
|
|
|
|
cheerio = require('cheerio');
|
|
|
|
path = require('path');
|
|
|
|
isHTML = function(filename) {
|
|
return /\.html$/.exec(filename);
|
|
};
|
|
|
|
fileExists = function(files, filename) {
|
|
var winPath;
|
|
if (filename.charAt(0) === '/') {
|
|
filename = filename.slice(1);
|
|
}
|
|
if (filename in files) {
|
|
return true;
|
|
} else {
|
|
winPath = filename.split('/').join(path.sep);
|
|
return winPath in files;
|
|
}
|
|
};
|
|
|
|
Link = (function() {
|
|
function Link($link) {
|
|
var ref;
|
|
if ($link.is('a')) {
|
|
this.text = $link.text();
|
|
this.href = $link.attr('href');
|
|
this.isAnchor = ((ref = $link.attr('name')) != null ? ref.length : void 0) > 0 && (this.href == null);
|
|
} else if ($link.is('img')) {
|
|
this.text = $link.attr('alt');
|
|
this.href = $link.attr('src');
|
|
}
|
|
}
|
|
|
|
Link.prototype.isBroken = function(filename, files, options) {
|
|
var linkPath, ref, unixFilename, uri;
|
|
if (options.allowAnchors && this.isAnchor) {
|
|
return false;
|
|
}
|
|
if (this.href == null) {
|
|
return true;
|
|
}
|
|
uri = URI(this.href);
|
|
if ((options.allowRegex != null) && options.allowRegex.exec(this.href)) {
|
|
return false;
|
|
}
|
|
if (this.href === '') {
|
|
return true;
|
|
}
|
|
if (this.href === '#') {
|
|
return false;
|
|
}
|
|
if (uri.hostname()) {
|
|
return false;
|
|
}
|
|
if (uri.protocol() && ((ref = uri.protocol) !== 'http' && ref !== 'https')) {
|
|
return false;
|
|
}
|
|
if (uri.fragment() && !uri.path()) {
|
|
return false;
|
|
}
|
|
unixFilename = filename.replace(/\\/g, '/');
|
|
linkPath = uri.absoluteTo(unixFilename).path();
|
|
if (linkPath === '/') {
|
|
return !fileExists(files, 'index.html');
|
|
}
|
|
if (linkPath.slice(-1) === '/') {
|
|
linkPath += 'index.html';
|
|
}
|
|
if (options.allowRedirects && fileExists(files, linkPath + '/index.html')) {
|
|
return false;
|
|
}
|
|
return !fileExists(files, linkPath);
|
|
};
|
|
|
|
Link.prototype.toString = function() {
|
|
return "href: \"" + this.href + "\", text: \"" + this.text + "\"";
|
|
};
|
|
|
|
return Link;
|
|
|
|
})();
|
|
|
|
module.exports = function(options) {
|
|
var selector;
|
|
if (options == null) {
|
|
options = {};
|
|
}
|
|
if (options === true) {
|
|
options = {};
|
|
}
|
|
if (options.warn == null) {
|
|
options.warn = false;
|
|
}
|
|
if (options.checkLinks == null) {
|
|
options.checkLinks = true;
|
|
}
|
|
if (options.checkImages == null) {
|
|
options.checkImages = true;
|
|
}
|
|
if (options.allowRegex == null) {
|
|
options.allowRegex = null;
|
|
}
|
|
if (options.allowAnchors == null) {
|
|
options.allowAnchors = true;
|
|
}
|
|
if (options.allowAnchors == null) {
|
|
options.allowAnchors = false;
|
|
}
|
|
if (options.checkLinks && options.checkImages) {
|
|
selector = 'a, img';
|
|
} else if (options.checkLinks) {
|
|
selector = 'a';
|
|
} else if (options.checkImages) {
|
|
selector = 'img';
|
|
} else {
|
|
return function() {};
|
|
}
|
|
return function(files, metalsmith) {
|
|
var $, contents, file, filename, results;
|
|
results = [];
|
|
for (filename in files) {
|
|
file = files[filename];
|
|
if (!isHTML(filename)) {
|
|
continue;
|
|
}
|
|
contents = file.contents.toString();
|
|
$ = cheerio.load(contents);
|
|
results.push($(selector).each(function() {
|
|
var link;
|
|
link = new Link($(this));
|
|
if (link.isBroken(filename, files, options)) {
|
|
if (options.warn) {
|
|
return console.log("Warning: Link is broken: " + (link.toString()) + ", in file: " + filename);
|
|
} else {
|
|
throw new Error("Link is broken: " + (link.toString()) + ", in file: " + filename);
|
|
}
|
|
}
|
|
}));
|
|
}
|
|
return results;
|
|
};
|
|
};
|
|
|
|
}).call(this);
|