mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-16 22:10:13 +01:00
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:
parent
cba2ca0dbd
commit
6c8ef48f94
5 changed files with 102 additions and 18 deletions
12
lib/md051.js
12
lib/md051.js
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue