Add support for named heading fragments as supported by some platforms (fixes #830).

This commit is contained in:
David Anson 2023-07-08 22:14:00 -07:00
parent 3dcb66cac5
commit 7a794192ca
8 changed files with 295 additions and 18 deletions

View file

@ -8,6 +8,7 @@ const { addError, addErrorDetailIf, escapeForRegExp, filterTokens,
// Regular expression for identifying HTML anchor names
const idRe = /\sid\s*=\s*['"]?([^'"\s>]+)/iu;
const nameRe = /\sname\s*=\s*['"]?([^'"\s>]+)/iu;
const anchorRe = /\{(#[a-z\d]+(?:[-_][a-z\d]+)*)\}/gu;
/**
* Converts a Markdown heading into an HTML fragment according to the rules
@ -50,6 +51,13 @@ module.exports = {
fragments.set(`${fragment}-${count}`, 0);
}
fragments.set(fragment, count + 1);
let match = null;
while ((match = anchorRe.exec(content)) !== null) {
const [ , anchor ] = match;
if (!fragments.has(anchor)) {
fragments.set(anchor, 1);
}
}
});
// Process HTML anchors
const processHtmlToken = (token) => {