File tree Expand file tree Collapse file tree 1 file changed +19
-3
lines changed
Expand file tree Collapse file tree 1 file changed +19
-3
lines changed Original file line number Diff line number Diff line change 11import pypandoc
22import os
33import re
4+ import urllib .parse
45
56def fix_paths (content ):
67 # 处理 Markdown 图片和链接:  或 [text](../../path)
7- # 使用正则匹配 (!?\[.*?\]\() 并查找紧随其后的一个或多个 ../
8- content = re .sub (r'(!?\[.*?\]\()(\.\./)+' , r'\1./' , content )
8+ def md_callback (match ):
9+ prefix = match .group (1 )
10+ path = match .group (2 )
11+ # 修复路径:将开头的 ../ 替换为 ./
12+ path = re .sub (r'^(\.\./)+' , './' , path )
13+ # 解码中文文件名
14+ return prefix + urllib .parse .unquote (path ) + ')'
15+
16+ content = re .sub (r'(!?\[.*?\]\()([^)]+)\)' , md_callback , content )
917
1018 # 处理 HTML 标签: <img src="../path"> 或 <a href="../../path">
11- content = re .sub (r'(src="|href=")(\.\./)+' , r'\1./' , content )
19+ def html_callback (match ):
20+ prefix = match .group (1 )
21+ path = match .group (2 )
22+ # 修复路径:将开头的 ../ 替换为 ./
23+ path = re .sub (r'^(\.\./)+' , './' , path )
24+ # 解码中文文件名
25+ return prefix + urllib .parse .unquote (path ) + '"'
26+
27+ content = re .sub (r'(src="|href=")([^"]+)"' , html_callback , content )
1228
1329 return content
1430
You can’t perform that action at this time.
0 commit comments