Convert markdownlint library to an ECMAScript module, replace markdownlint-micromark with micromark, stop publishing (large) markdownlint-browser.js, see https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c for guidance.

This commit is contained in:
David Anson 2024-11-28 20:36:44 -08:00
parent 191226f070
commit 1e71f6f44e
140 changed files with 1087 additions and 10428 deletions

60
lib/md053.mjs Normal file
View file

@ -0,0 +1,60 @@
// @ts-check
import { addError, ellipsify } from "../helpers/helpers.cjs";
import { getReferenceLinkImageData } from "./cache.mjs";
const linkReferenceDefinitionRe = /^ {0,3}\[([^\]]*[^\\])\]:/;
/** @type {import("./markdownlint.mjs").Rule} */
export default {
"names": [ "MD053", "link-image-reference-definitions" ],
"description": "Link and image reference definitions should be needed",
"tags": [ "images", "links" ],
"parser": "none",
"function": function MD053(params, onError) {
const ignored = new Set(params.config.ignored_definitions || [ "//" ]);
const lines = params.lines;
const { references, shortcuts, definitions, duplicateDefinitions } =
getReferenceLinkImageData();
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()) {
const [ label, [ lineIndex ] ] = definition;
if (
!ignored.has(label) &&
!references.has(label) &&
!shortcuts.has(label)
) {
const line = lines[lineIndex];
addError(
onError,
lineIndex + 1,
`Unused link or image reference definition: "${label}"`,
ellipsify(line),
[ 1, line.length ],
singleLineDefinition(line) ? deleteFixInfo : undefined
);
}
}
// Look for duplicate link references (defined more than once)
for (const duplicateDefinition of duplicateDefinitions) {
const [ label, lineIndex ] = duplicateDefinition;
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 : undefined
);
}
}
}
};