Skip to content

Commit f2d6e13

Browse files
committed
feat: code by Trae
1 parent 3fd197f commit f2d6e13

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

epub_browser/assets/chapter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,9 @@ function initScript() {
10311031
target.closest('.font-controls') ||
10321032
target.closest('.reading-controls') ||
10331033
target.closest('.toc-container') ||
1034-
target.closest('.medium-zoom-container');
1034+
target.closest('.medium-zoom-container') ||
1035+
target.closest('.top-controls') ||
1036+
target.closest('.mobile-controls');
10351037

10361038
if (isInteractiveElement) return;
10371039

epub_browser/processor.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,28 +480,50 @@ def create_index_page(self):
480480

481481
# 如果有详细的toc信息,使用toc生成目录
482482
if self.toc:
483-
# 创建章节路径到索引的映射
483+
# 创建章节路径到索引的映射(支持多种路径格式)
484484
chapter_index_map = {}
485+
chapter_filename_map = {} # 用于文件名匹配
485486
for i, chapter in enumerate(self.chapters):
487+
# 原始路径
486488
chapter_index_map[chapter['path']] = i
489+
# 规范化路径(去除 ./ 前缀)
490+
normalized_path = chapter['path'].lstrip('./').lstrip('/')
491+
chapter_index_map[normalized_path] = i
492+
# 文件名匹配
493+
chapter_filename = os.path.basename(chapter['path'])
494+
chapter_filename_map[chapter_filename] = i
487495

488496
# print(f"Chapter index mapping: {chapter_index_map}")
497+
# print(f"Chapter filename mapping: {chapter_filename_map}")
489498

490499
# 根据toc生成目录
491500
for toc_item in self.toc:
492501
level_class = f"toc-level-{min(toc_item.get('level', 0), 3)}"
493502
chapter_anchor = toc_item.get('anchor', None)
494-
chapter_index = chapter_index_map.get(toc_item['src'])
503+
toc_src = toc_item['src']
504+
505+
# 尝试多种方式匹配章节索引
506+
chapter_index = None
507+
508+
# 1. 直接匹配
509+
if toc_src in chapter_index_map:
510+
chapter_index = chapter_index_map[toc_src]
511+
# 2. 规范化路径匹配(去除 ./ 前缀)
512+
elif toc_src.lstrip('./').lstrip('/') in chapter_index_map:
513+
chapter_index = chapter_index_map[toc_src.lstrip('./').lstrip('/')]
514+
# 3. 文件名匹配
515+
elif os.path.basename(toc_src) in chapter_filename_map:
516+
chapter_index = chapter_filename_map[os.path.basename(toc_src)]
495517

496518
if chapter_index is not None:
497519
if chapter_anchor is not None:
498520
# 只加一个正向链接的 anchor 定位,反链接中的 id 不加 anchor 防止章节中锚点乱搞而回来时无法锚定
499521
index_html += f' <li class="{level_class}"><a href="/book/{self.book_hash}/chapter_{chapter_index}.html#{chapter_anchor}" id="chapter_{chapter_index}"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
500522
else:
501523
index_html += f' <li class="{level_class}"><a href="/book/{self.book_hash}/chapter_{chapter_index}.html" id="chapter_{chapter_index}"><span class="chapter-title">{toc_item["title"]}</span><span class="chapter-page">chapter_{chapter_index}.html</span></a></li>\n'
502-
toc_item['new_file_name'] = f'chapter_{i}.html'
524+
toc_item['new_file_name'] = f'chapter_{chapter_index}.html'
503525
else:
504-
print(f"Chapter index not found: {toc_item['src']}")
526+
print(f"Chapter index not found for toc item: {toc_item['title']} (src: {toc_src})")
505527
else:
506528
# 回退到简单章节列表
507529
for i, chapter in enumerate(self.chapters):
927 KB
Binary file not shown.

0 commit comments

Comments
 (0)