mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
// @ts-check
|
|
|
|
"use strict";
|
|
|
|
const shared = require("./shared");
|
|
|
|
const emptyLinkRe = /\[[^\]]*](?:\((?:#?|(?:<>))\))/;
|
|
|
|
module.exports = {
|
|
"names": [ "MD042", "no-empty-links" ],
|
|
"description": "No empty links",
|
|
"tags": [ "links" ],
|
|
"function": function MD042(params, onError) {
|
|
shared.filterTokens(params, "inline", function forToken(token) {
|
|
let inLink = false;
|
|
let linkText = "";
|
|
let 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) {
|
|
shared.addErrorContext(onError, child.lineNumber,
|
|
"[" + linkText + "]()", null, null,
|
|
shared.rangeFromRegExp(child.line, emptyLinkRe));
|
|
}
|
|
} else if (inLink) {
|
|
linkText += child.content;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
};
|