Add getHtmlAttributeRe helper for creating a RegExp to match an HTML attribute name/value.

This commit is contained in:
David Anson 2023-10-18 23:45:39 -07:00
parent 531e58ed9a
commit 06466905a5
4 changed files with 36 additions and 15 deletions

View file

@ -2,13 +2,14 @@
"use strict";
const { addError, addErrorDetailIf } = require("../helpers");
const { addError, addErrorDetailIf, getHtmlAttributeRe } =
require("../helpers");
const { filterByPredicate, filterByTypes, getHtmlTagInfo } =
require("../helpers/micromark.cjs");
// Regular expression for identifying HTML anchor names
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu;
const idRe = getHtmlAttributeRe("id");
const nameRe = getHtmlAttributeRe("name");
const anchorRe = /\{(#[a-z\d]+(?:[-_][a-z\d]+)*)\}/gu;
// Sets for filtering heading tokens during conversion
@ -101,7 +102,7 @@ module.exports = {
if (htmlTagInfo && !htmlTagInfo.close) {
const anchorMatch = idRe.exec(token.text) ||
(htmlTagInfo.name.toLowerCase() === "a" && nameRe.exec(token.text));
if (anchorMatch) {
if (anchorMatch && anchorMatch.length > 0) {
fragments.set(`#${anchorMatch[1]}`, 0);
}
}