@@ -37,15 +37,17 @@ def extract_precise_toc(self, doc, offset):
3737 # 粗放式匹配:字体大且粗的可能是标题
3838 if s ["size" ] > 12 :
3939 text = s ["text" ].strip ()
40+ # 存入映射,对 key 进行标准化处理(去除空格、处理罕见字符等)
4041 if text :
41- headings_map [text ] = (page_num , s ["bbox" ][1 ])
42+ headings_map [text . lower () ] = (page_num , s ["bbox" ][1 ])
4243
4344 refined_toc = []
4445 for entry in raw_toc :
4546 lvl , title , page , dest = entry
46- # 尝试匹配文本高度
47- if title in headings_map :
48- p_idx , y_coord = headings_map [title ]
47+ # 尝试匹配文本高度,使用小写标准化匹配
48+ match_title = title .strip ().lower ()
49+ if match_title in headings_map :
50+ p_idx , y_coord = headings_map [match_title ]
4951 dest = {"kind" : fitz .LINK_GOTO , "to" : fitz .Point (0 , y_coord )}
5052
5153 refined_toc .append ([lvl , title , page + offset , dest ])
@@ -81,7 +83,11 @@ def process(self):
8183
8284 # 插入内容页
8385 for sub in section ["sections" ]:
84- content_path = Path ("site/build" ) / sub ["path" ]
86+ # 尝试从 JSON 所在目录查找,或者使用绝对 site/build 路径
87+ content_path = self .book_json_path .parent / sub ["path" ]
88+ if not content_path .exists ():
89+ content_path = Path ("site/build" ) / sub ["path" ]
90+
8591 if content_path .exists ():
8692 doc = fitz .open (content_path )
8793 # 提取并偏移章节内的书签
0 commit comments