2022-06-01 20:23:08 -07:00
|
|
|
// @ts-check
|
|
|
|
|
2024-11-28 20:36:44 -08:00
|
|
|
import { addError } from "../helpers/helpers.cjs";
|
|
|
|
import { getReferenceLinkImageData } from "./cache.mjs";
|
2022-06-01 20:23:08 -07:00
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2024-11-28 20:36:44 -08:00
|
|
|
export default {
|
2022-06-01 20:23:08 -07:00
|
|
|
"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;
|
2025-04-27 22:36:07 -07:00
|
|
|
const ignoredLabels = new Set(config.ignored_labels || [ "x" ]);
|
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;
|
2025-04-27 22:36:07 -07:00
|
|
|
if (!definitions.has(label) && !ignoredLabels.has(label)) {
|
2022-06-01 20:23:08 -07:00
|
|
|
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 ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|