Update MD051/link-fragments to support id attributes on non-a elements (fixes #538).

The `name` is only an anchor on `a` elements, but `id` is a universal
attribute on all elements.

Also fix match on id/name to be complete, not just a suffix.
This commit is contained in:
Mark Lodato 2022-07-28 00:42:05 -04:00 committed by GitHub
parent cba2ca0dbd
commit 6c8ef48f94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 102 additions and 18 deletions

View file

@ -6,7 +6,8 @@ const { addError, escapeForRegExp, filterTokens, forEachInlineChild,
forEachHeading, htmlElementRe } = require("../helpers");
// Regular expression for identifying HTML anchor names
const identifierRe = /(?:id|name)\s*=\s*['"]?([^'"\s>]+)/iu;
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu;
/**
* Converts a Markdown heading into an HTML fragment according to the rules
@ -55,11 +56,10 @@ module.exports = {
let match = null;
while ((match = htmlElementRe.exec(token.content)) !== null) {
const [ tag, , element ] = match;
if (element.toLowerCase() === "a") {
const idMatch = identifierRe.exec(tag);
if (idMatch) {
fragments.set(`#${idMatch[1]}`, 0);
}
const anchorMatch = idRe.exec(tag) ||
(element.toLowerCase() === "a" && nameRe.exec(tag));
if (anchorMatch) {
fragments.set(`#${anchorMatch[1]}`, 0);
}
}
};