2022-06-01 20:23:08 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { addError } = require("../helpers");
|
2024-08-24 22:05:16 -07:00
|
|
|
const { getReferenceLinkImageData } = require("./cache");
|
2022-06-01 20:23:08 -07:00
|
|
|
|
2024-02-27 20:42:09 -08:00
|
|
|
// eslint-disable-next-line jsdoc/valid-types
|
|
|
|
/** @type import("./markdownlint").Rule */
|
2022-06-01 20:23:08 -07:00
|
|
|
module.exports = {
|
|
|
|
"names": [ "MD052", "reference-links-images" ],
|
|
|
|
"description":
|
|
|
|
"Reference links and images should use a label that is defined",
|
|
|
|
"tags": [ "images", "links" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "none",
|
2022-06-01 20:23:08 -07:00
|
|
|
"function": function MD052(params, onError) {
|
2023-09-04 16:40:48 -07:00
|
|
|
const { config, lines } = params;
|
|
|
|
const shortcutSyntax = config.shortcut_syntax || false;
|
2024-08-24 22:05:16 -07:00
|
|
|
const { definitions, references, shortcuts } = getReferenceLinkImageData();
|
2023-09-04 16:40:48 -07:00
|
|
|
const entries = shortcutSyntax ?
|
|
|
|
[ ...references.entries(), ...shortcuts.entries() ] :
|
|
|
|
references.entries();
|
2022-06-01 20:23:08 -07:00
|
|
|
// Look for links/images that use an undefined link reference
|
2023-09-04 16:40:48 -07:00
|
|
|
for (const reference of entries) {
|
2022-06-01 20:23:08 -07:00
|
|
|
const [ label, datas ] = reference;
|
|
|
|
if (!definitions.has(label)) {
|
|
|
|
for (const data of datas) {
|
|
|
|
const [ lineIndex, index, length ] = data;
|
|
|
|
// Context will be incomplete if reporting for a multi-line link
|
|
|
|
const context = lines[lineIndex].slice(index, index + length);
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineIndex + 1,
|
|
|
|
`Missing link or image reference definition: "${label}"`,
|
|
|
|
context,
|
|
|
|
[ index + 1, context.length ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|