mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
Add MD052/reference-links-images and MD053/link-image-reference-definitions for reporting issues with link and image references (fixes #144, fixes #390, fixes #425, fixes #456).
This commit is contained in:
parent
2c947abf7b
commit
c5ca661b96
21 changed files with 1333 additions and 65 deletions
52
lib/md053.js
Normal file
52
lib/md053.js
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
// @ts-check
|
||||
|
||||
"use strict";
|
||||
|
||||
const { addError, ellipsify, linkReferenceDefinitionRe } =
|
||||
require("../helpers");
|
||||
const { referenceLinkImageData } = require("./cache");
|
||||
|
||||
module.exports = {
|
||||
"names": [ "MD053", "link-image-reference-definitions" ],
|
||||
"description": "Link and image reference definitions should be needed",
|
||||
"tags": [ "images", "links" ],
|
||||
"function": function MD053(params, onError) {
|
||||
const { lines } = params;
|
||||
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()) {
|
||||
const [ label, lineIndex ] = definition;
|
||||
if (!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 : 0
|
||||
);
|
||||
}
|
||||
}
|
||||
// Look for duplicate link references (defined more than once)
|
||||
for (const duplicateDefinition of duplicateDefinitions) {
|
||||
const [ label, lineIndex ] = duplicateDefinition;
|
||||
const line = lines[lineIndex];
|
||||
addError(
|
||||
onError,
|
||||
lineIndex + 1,
|
||||
`Duplicate link or image reference definition: "${label}"`,
|
||||
ellipsify(line),
|
||||
[ 1, line.length ],
|
||||
singleLineDefinition(line) ? deleteFixInfo : 0
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue