From 3000378786543bba09e835f4a0bc603518316ef2 Mon Sep 17 00:00:00 2001 From: Tom Najdek Date: Fri, 16 Feb 2024 16:10:05 +0100 Subject: [PATCH] Fix #undefined named anchors for question permalinks. Resolve #304 Also removed comment from the default config which made it invalid JSON. (The same explanation is already included in README) --- config/default.json | 10 ---------- scripts/build-html.cjs | 4 +++- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/config/default.json b/config/default.json index 9e035214..8124c17a 100644 --- a/config/default.json +++ b/config/default.json @@ -1,14 +1,4 @@ { "indexConfig": { - // this config is used during the build to configure Zotero Bib - // component both during development and in production. Values - // listed below can be overriden in local.json and/or using - // env variables. See https://www.npmjs.com/package/config - // - // "apiAuthorityPart": undefined, - // "storeURL": undefined, - // "stylesURL": undefined, - // "translatePrefix": "", - // "translateURL": undefined } } \ No newline at end of file diff --git a/scripts/build-html.cjs b/scripts/build-html.cjs index 6ba201c1..cc089ee8 100644 --- a/scripts/build-html.cjs +++ b/scripts/build-html.cjs @@ -13,7 +13,9 @@ const addAnchors = html => { const $ = cheerio.load(html); const headers = $('h2, h3'); headers.map((_, element) => { - const id = element.attribs.id; + const id = element.attribs.id ?? $(element).text().replace(/[^\w\s]/g, '').trim().replace(/\s/g, '_').toLocaleLowerCase(); + element.attribs.id = id; + $('') .attr('href', '#' + id) .addClass('anchor-link')