Spaces:
Running

vomebook commited on
Commit
4e0435b
·
1 Parent(s): ed3e101

Reject ambiguous duplicate CHM page anchors

Browse files
static/reader/formats/reader-chapter-format.mjs CHANGED
@@ -117,12 +117,16 @@ export function createReaderChapterFormat(readerContext) {
117
  entry.chapterIndex === chapter.index && !entry.fragment && normalizeTocLabel(entry.label));
118
  if (!missing.length) return;
119
  const wanted = new Set(missing.map(entry => normalizeTocLabel(entry.label)));
 
 
 
 
120
  const targets = new Map();
121
  for (const link of article.querySelectorAll('a[href^="#chm-page-"]')) {
122
  const label = normalizeTocLabel(link.textContent);
123
  if (!wanted.has(label)) continue;
124
  const fragment = link.getAttribute("href").slice(1);
125
- if (!/^chm-page-\d+$/.test(fragment) || !chapterTarget(article, fragment)) continue;
126
  if (!targets.has(label)) targets.set(label, new Set());
127
  targets.get(label).add(fragment);
128
  }
 
117
  entry.chapterIndex === chapter.index && !entry.fragment && normalizeTocLabel(entry.label));
118
  if (!missing.length) return;
119
  const wanted = new Set(missing.map(entry => normalizeTocLabel(entry.label)));
120
+ const idCounts = new Map();
121
+ for (const node of article.querySelectorAll("[id], [name]"))
122
+ for (const id of new Set([node.id, node.getAttribute("name")]))
123
+ if (id) idCounts.set(id, (idCounts.get(id) || 0) + 1);
124
  const targets = new Map();
125
  for (const link of article.querySelectorAll('a[href^="#chm-page-"]')) {
126
  const label = normalizeTocLabel(link.textContent);
127
  if (!wanted.has(label)) continue;
128
  const fragment = link.getAttribute("href").slice(1);
129
+ if (!/^chm-page-\d+$/.test(fragment) || idCounts.get(fragment) !== 1 || !chapterTarget(article, fragment)) continue;
130
  if (!targets.has(label)) targets.set(label, new Set());
131
  targets.get(label).add(fragment);
132
  }