Update MD051/link-fragments to also look for a fragment matching the URI-encoded link definition (implicit behavior in previous version via markdown-it) (fixes #954, fixes #955).

This commit is contained in:
David Anson 2023-09-06 20:50:56 -07:00
parent b7ef571401
commit 1ddee6b195
5 changed files with 62 additions and 37 deletions

View file

@ -99,10 +99,12 @@ module.exports = {
for (const link of links) {
const definitions = filterByTypes(link.children, [ definitionType ]);
for (const definition of definitions) {
const { endColumn, startColumn, text } = definition;
if (
(definition.text.length > 1) &&
definition.text.startsWith("#") &&
!fragments.has(definition.text)
(text.length > 1) &&
text.startsWith("#") &&
!fragments.has(text) &&
!fragments.has(`#${encodeURIComponent(text.slice(1))}`)
) {
// eslint-disable-next-line no-undef-init
let context = undefined;
@ -114,13 +116,13 @@ module.exports = {
context = link.text;
range = [ link.startColumn, link.endColumn - link.startColumn ];
fixInfo = {
"editColumn": definition.startColumn,
"deleteCount": definition.endColumn - definition.startColumn
"editColumn": startColumn,
"deleteCount": endColumn - startColumn
};
}
const definitionTextLower = definition.text.toLowerCase();
const textLower = text.toLowerCase();
const mixedCaseKey = [ ...fragments.keys() ]
.find((key) => definitionTextLower === key.toLowerCase());
.find((key) => textLower === key.toLowerCase());
if (mixedCaseKey) {
// @ts-ignore
(fixInfo || {}).insertText = mixedCaseKey;
@ -128,7 +130,7 @@ module.exports = {
onError,
link.startLine,
mixedCaseKey,
definition.text,
text,
undefined,
context,
range,