vomebook commited on
Commit
768231f
·
1 Parent(s): 2ba0b38

Recover missing merged CHM sidebar anchors

Browse files
static/reader/formats/reader-chapter-format.mjs CHANGED
@@ -108,9 +108,33 @@ export function createReaderChapterFormat(readerContext) {
108
  const chapterTarget = (article, fragment) =>
109
  !fragment || article.id === fragment ? article :
110
  article.querySelector(`[id="${CSS.escape(fragment)}"], [name="${CSS.escape(fragment)}"]`);
111
- const setChapterToc = () =>
112
- readerContext.setToc(
113
- (manifest.toc ?? manifest.chapters.map((item) => ({
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  title: item.title || `章节 ${item.index}`, chapter: item.index, depth: 0, fragment: ""
115
  }))).map((item) => ({
116
  label: item.title,
@@ -118,8 +142,11 @@ export function createReaderChapterFormat(readerContext) {
118
  fragment: item.fragment,
119
  depth: item.depth,
120
  activate: (generation) => activateChapter(manifest.chapters[item.chapter - 1], generation, item.fragment)
121
- }))
122
- );
 
 
 
123
  const chapterScheduler = VoiceOfMLReaderChapters.createChapterScheduler({
124
  create: async (index, signal, priority) => {
125
  const chapter = chapterAt(index);
@@ -166,6 +193,7 @@ export function createReaderChapterFormat(readerContext) {
166
  loaded.add(chapter.index);
167
  retainedBytes.set(chapter.index, bytes.byteLength);
168
  inserted = true;
 
169
  for (const entry of readerContext.navigationState.tocEntries)
170
  if (entry.chapterIndex === chapter.index)
171
  entry.target = chapterTarget(article, entry.fragment);
@@ -194,14 +222,14 @@ export function createReaderChapterFormat(readerContext) {
194
  throw error;
195
  });
196
  };
197
- const activateChapter = async (chapter, generation, fragment = "") => {
198
  chapterFocusIndex = chapter.index;
199
  chapterNavigating++;
200
  try { await fetchChapter(chapter, true, chapterWindowReady); }
201
  finally { chapterNavigating--; }
202
  if (!readerContext.isReaderGenerationCurrent("navigation", generation)) return false;
203
  const node = frame.querySelector(`.reader-epub-chapter[data-chapter="${chapter.index}"]`);
204
- let id = fragment.replace(/^#/, "");
205
  try {
206
  id = decodeURIComponent(id);
207
  } catch (_) {}
 
108
  const chapterTarget = (article, fragment) =>
109
  !fragment || article.id === fragment ? article :
110
  article.querySelector(`[id="${CSS.escape(fragment)}"], [name="${CSS.escape(fragment)}"]`);
111
+ const normalizeTocLabel = value => String(value || "").replace(/\s+/gu, "").toLowerCase();
112
+ // Old merged CHM bundles can lose the TOC fragment while retaining the
113
+ // original, functioning book-internal link. Accept only a unique exact
114
+ // label that points to an existing generated page in the same chapter.
115
+ const repairMergedChmToc = (article, chapter) => {
116
+ const missing = readerContext.navigationState.tocEntries.filter(entry =>
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
+ }
129
+ for (const entry of missing) {
130
+ const candidates = targets.get(normalizeTocLabel(entry.label));
131
+ if (candidates?.size === 1) entry.fragment = candidates.values().next().value;
132
+ }
133
+ };
134
+ const mergedChmToc = manifest.chapters.some(chapter =>
135
+ chapter.source_path === "chm-fallback.html");
136
+ const setChapterToc = () => {
137
+ const entries = (manifest.toc ?? manifest.chapters.map((item) => ({
138
  title: item.title || `章节 ${item.index}`, chapter: item.index, depth: 0, fragment: ""
139
  }))).map((item) => ({
140
  label: item.title,
 
142
  fragment: item.fragment,
143
  depth: item.depth,
144
  activate: (generation) => activateChapter(manifest.chapters[item.chapter - 1], generation, item.fragment)
145
+ }));
146
+ readerContext.setToc(entries);
147
+ for (const entry of readerContext.navigationState.tocEntries)
148
+ entry.activate = generation => activateChapter(chapterAt(entry.chapterIndex), generation, entry);
149
+ };
150
  const chapterScheduler = VoiceOfMLReaderChapters.createChapterScheduler({
151
  create: async (index, signal, priority) => {
152
  const chapter = chapterAt(index);
 
193
  loaded.add(chapter.index);
194
  retainedBytes.set(chapter.index, bytes.byteLength);
195
  inserted = true;
196
+ if (mergedChmToc) repairMergedChmToc(article, chapter);
197
  for (const entry of readerContext.navigationState.tocEntries)
198
  if (entry.chapterIndex === chapter.index)
199
  entry.target = chapterTarget(article, entry.fragment);
 
222
  throw error;
223
  });
224
  };
225
+ const activateChapter = async (chapter, generation, destination = "") => {
226
  chapterFocusIndex = chapter.index;
227
  chapterNavigating++;
228
  try { await fetchChapter(chapter, true, chapterWindowReady); }
229
  finally { chapterNavigating--; }
230
  if (!readerContext.isReaderGenerationCurrent("navigation", generation)) return false;
231
  const node = frame.querySelector(`.reader-epub-chapter[data-chapter="${chapter.index}"]`);
232
+ let id = (typeof destination === "string" ? destination : destination.fragment || "").replace(/^#/, "");
233
  try {
234
  id = decodeURIComponent(id);
235
  } catch (_) {}