Skip to content

Commit b5efdd7

Browse files
committed
Update convert.py
1 parent 41abe3c commit b5efdd7

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

images/fragment/scripts/convert.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import pypandoc
22
import os
33
import re
4+
import urllib.parse
45

56
def fix_paths(content):
67
# 处理 Markdown 图片和链接: ![alt](../path) 或 [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

0 commit comments

Comments
 (0)