markdownlint/lib/rules/noEmptyLinks.js
Josh Goldberg b63647f6b1 Moved rules into their own files
Everything under `lib/rules/*` is a rules file (with the name of the rule in camelCase), re-exported into an array in `lib/rules.js`.
Moved the regular expressions from `lib/rules.js` to `lib/expressions.js`, and the rest of the variables into `lib/shared.js`.
2017-10-29 19:55:01 -07:00

35 lines
1 KiB
JavaScript

var shared = require("../shared");
var expressions = require("../expressions");
module.exports = {
"name": "MD042",
"desc": "No empty links",
"tags": [ "links" ],
"aliases": [ "no-empty-links" ],
"regexp": expressions.emptyLinkRe,
"func": function MD042(params, errors) {
shared.filterTokens(params, "inline", function forToken(token) {
var inLink = false;
var linkText = "";
var emptyLink = false;
token.children.forEach(function forChild(child) {
if (child.type === "link_open") {
inLink = true;
linkText = "";
child.attrs.forEach(function forAttr(attr) {
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
emptyLink = true;
}
});
} else if (child.type === "link_close") {
inLink = false;
if (emptyLink) {
errors.addContext(child.lineNumber, "[" + linkText + "]");
}
} else if (inLink) {
linkText += child.content;
}
});
});
}
};