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

@ -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);
}
});
}