Update MD051/link-fragments to identify and fix scenarios where the link fragment has the wrong case (fixes #605).

This commit is contained in:
David Anson 2022-12-16 13:50:38 -08:00
parent d352d4ece1
commit ac8f495ea2
8 changed files with 146 additions and 14 deletions

View file

@ -8,7 +8,7 @@ module.exports.fixableRuleNames = [
"MD011", "MD012", "MD014", "MD018", "MD019", "MD020",
"MD021", "MD022", "MD023", "MD026", "MD027", "MD030",
"MD031", "MD032", "MD034", "MD037", "MD038", "MD039",
"MD044", "MD047", "MD049", "MD050", "MD053"
"MD044", "MD047", "MD049", "MD050", "MD051", "MD053"
];
module.exports.homepage = "https://github.com/DavidAnson/markdownlint";
module.exports.version = "0.26.2";

View file

@ -2,8 +2,8 @@
"use strict";
const { addError, escapeForRegExp, filterTokens, forEachInlineChild,
forEachHeading, htmlElementRe } = require("../helpers");
const { addError, addErrorDetailIf, escapeForRegExp, filterTokens,
forEachInlineChild, forEachHeading, htmlElementRe } = require("../helpers");
// Regular expression for identifying HTML anchor names
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
@ -73,14 +73,46 @@ module.exports = {
if (id && (id.length > 1) && (id[0] === "#") && !fragments.has(id)) {
let context = id;
let range = null;
let fixInfo = null;
const match = line.match(
new RegExp(`\\[.*?\\]\\(${escapeForRegExp(context)}\\)`)
);
if (match) {
context = match[0];
range = [ match.index + 1, match[0].length ];
[ context ] = match;
const index = match.index;
const length = context.length;
range = [ index + 1, length ];
fixInfo = {
"editColumn": index + (length - id.length),
"deleteCount": id.length,
"insertText": null
};
}
const idLower = id.toLowerCase();
const mixedCaseKey = [ ...fragments.keys() ]
.find((key) => idLower === key.toLowerCase());
if (mixedCaseKey) {
(fixInfo || {}).insertText = mixedCaseKey;
addErrorDetailIf(
onError,
lineNumber,
mixedCaseKey,
id,
undefined,
context,
range,
fixInfo
);
} else {
addError(
onError,
lineNumber,
undefined,
context,
// @ts-ignore
range
);
}
addError(onError, lineNumber, undefined, context, range);
}
});
}