2018-01-21 21:44:25 -08:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2022-02-13 17:41:31 -08:00
|
|
|
const { addErrorContext, escapeForRegExp, filterTokens } =
|
2019-04-13 11:18:57 -07:00
|
|
|
require("../helpers");
|
2018-01-21 21:44:25 -08:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2018-01-21 21:44:25 -08:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD042", "no-empty-links" ],
|
|
|
|
"description": "No empty links",
|
|
|
|
"tags": [ "links" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2018-01-21 21:44:25 -08:00
|
|
|
"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;
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const child of token.children) {
|
2018-01-21 21:44:25 -08:00
|
|
|
if (child.type === "link_open") {
|
|
|
|
inLink = true;
|
|
|
|
linkText = "";
|
2022-06-08 22:10:27 -07:00
|
|
|
for (const attr of child.attrs) {
|
2018-01-21 21:44:25 -08:00
|
|
|
if (attr[0] === "href" && (!attr[1] || (attr[1] === "#"))) {
|
|
|
|
emptyLink = true;
|
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
} else if (child.type === "link_close") {
|
|
|
|
inLink = false;
|
|
|
|
if (emptyLink) {
|
2022-02-13 17:41:31 -08:00
|
|
|
let context = `[${linkText}]`;
|
|
|
|
let range = null;
|
|
|
|
const match = child.line.match(
|
|
|
|
new RegExp(`${escapeForRegExp(context)}\\((?:|#|<>)\\)`)
|
|
|
|
);
|
|
|
|
if (match) {
|
|
|
|
context = match[0];
|
2024-02-27 20:42:09 -08:00
|
|
|
// @ts-ignore
|
2022-02-13 17:41:31 -08:00
|
|
|
range = [ match.index + 1, match[0].length ];
|
|
|
|
}
|
|
|
|
addErrorContext(
|
|
|
|
onError, child.lineNumber, context, null, null, range
|
|
|
|
);
|
2020-08-26 07:30:45 +02:00
|
|
|
emptyLink = false;
|
2018-01-21 21:44:25 -08:00
|
|
|
}
|
|
|
|
} else if (inLink) {
|
|
|
|
linkText += child.content;
|
|
|
|
}
|
2022-06-08 22:10:27 -07:00
|
|
|
}
|
2018-01-21 21:44:25 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|