From a16733eb93de5d76558238d1c92adcfcb21837ea Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Thu, 8 Jun 2023 12:39:12 -0400 Subject: [PATCH] don't allow numbers at the start of an id --- docs/_utilities/anchor-headings.cjs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/docs/_utilities/anchor-headings.cjs b/docs/_utilities/anchor-headings.cjs index 7e45031c..64a71668 100644 --- a/docs/_utilities/anchor-headings.cjs +++ b/docs/_utilities/anchor-headings.cjs @@ -21,21 +21,29 @@ module.exports = function (doc, options) { within.querySelectorAll('h1, h2, h3, h4, h5, h6').forEach(heading => { const hasAnchor = heading.querySelector('a'); const anchor = doc.createElement('a'); - const slug = createSlug(heading.textContent ?? '') ?? ''; - let id = slug; + let id = heading.textContent ?? ''; let suffix = 0; - // If we can't generate a slug, skip this heading - if (!slug) return; - // Skip heading levels we don't care about if (!options.levels?.includes(heading.tagName.toLowerCase())) { return; } - // Ensure the id is unique + // Convert dots to underscores + id = id.replace(/\./g, '_'); + + // Turn it into a slug + id = createSlug(id); + + // Make sure it starts with a letter + if (!/^[a-z]/i.test(id)) { + id = `id_${id}`; + } + + // Make sure the id is unique + const originalId = id; while (doc.getElementById(id) !== null) { - id = `${slug}-${++suffix}`; + id = `${originalId}-${++suffix}`; } if (hasAnchor || !id) return;