2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-01-22 22:20:41 -08:00
|
|
|
const { addErrorContext, emptyLinkRe, filterTokens, rangeFromRegExp } =
|
2019-04-13 11:18:57 -07:00
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD042", "no-empty-links" ],
|
|
|
|
"description": "No empty links",
|
|
|
|
"tags": [ "links" ],
|
|
|
|
"function": function MD042(params, onError) {
|
2019-04-13 11:18:57 -07:00
|
|
|
filterTokens(params, "inline", function forToken(token) {
|
2018-04-27 22:05:34 -07:00
|
|
|
let inLink = false;
|
|
|
|
let linkText = "";
|
|
|
|
let emptyLink = false;
|
2018-01-21 21:44:25 -08:00
|
|
|
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) {
|
2019-04-13 11:18:57 -07:00
|
|
|
addErrorContext(onError, child.lineNumber,
|
2018-01-21 21:44:25 -08:00
|
|
|
"[" + linkText + "]()", null, null,
|
2019-04-13 11:18:57 -07:00
|
|
|
rangeFromRegExp(child.line, emptyLinkRe));
|
2020-08-26 07:30:45 +02:00
|
|
|
emptyLink = false;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
} else if (inLink) {
|
|
|
|
linkText += child.content;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|