2022-06-01 20:23:08 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { addError, ellipsify, linkReferenceDefinitionRe } =
|
|
|
|
require("../helpers");
|
|
|
|
const { referenceLinkImageData } = require("./cache");
|
|
|
|
|
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": [ "MD053", "link-image-reference-definitions" ],
|
|
|
|
"description": "Link and image reference definitions should be needed",
|
|
|
|
"tags": [ "images", "links" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "none",
|
2022-06-01 20:23:08 -07:00
|
|
|
"function": function MD053(params, onError) {
|
2022-08-02 20:36:47 -07:00
|
|
|
const ignored = new Set(params.config.ignored_definitions || [ "//" ]);
|
|
|
|
const lines = params.lines;
|
2022-06-01 20:23:08 -07:00
|
|
|
const { references, shortcuts, definitions, duplicateDefinitions } =
|
|
|
|
referenceLinkImageData();
|
|
|
|
const singleLineDefinition = (line) => (
|
|
|
|
line.replace(linkReferenceDefinitionRe, "").trim().length > 0
|
|
|
|
);
|
|
|
|
const deleteFixInfo = {
|
|
|
|
"deleteCount": -1
|
|
|
|
};
|
|
|
|
// Look for unused link references (unreferenced by any link/image)
|
|
|
|
for (const definition of definitions.entries()) {
|
2023-10-25 20:05:19 -07:00
|
|
|
const [ label, [ lineIndex ] ] = definition;
|
2022-08-02 20:36:47 -07:00
|
|
|
if (
|
|
|
|
!ignored.has(label) &&
|
|
|
|
!references.has(label) &&
|
|
|
|
!shortcuts.has(label)
|
|
|
|
) {
|
2022-06-01 20:23:08 -07:00
|
|
|
const line = lines[lineIndex];
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineIndex + 1,
|
|
|
|
`Unused link or image reference definition: "${label}"`,
|
|
|
|
ellipsify(line),
|
|
|
|
[ 1, line.length ],
|
|
|
|
singleLineDefinition(line) ? deleteFixInfo : 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Look for duplicate link references (defined more than once)
|
|
|
|
for (const duplicateDefinition of duplicateDefinitions) {
|
|
|
|
const [ label, lineIndex ] = duplicateDefinition;
|
2022-08-02 20:36:47 -07:00
|
|
|
if (!ignored.has(label)) {
|
|
|
|
const line = lines[lineIndex];
|
|
|
|
addError(
|
|
|
|
onError,
|
|
|
|
lineIndex + 1,
|
|
|
|
`Duplicate link or image reference definition: "${label}"`,
|
|
|
|
ellipsify(line),
|
|
|
|
[ 1, line.length ],
|
|
|
|
singleLineDefinition(line) ? deleteFixInfo : 0
|
|
|
|
);
|
|
|
|
}
|
2022-06-01 20:23:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|