55def on_page_markdown (markdown , page , config , files ):
66 """
77 将 'issue #数字'、'PR #数字'、'commit 哈希' 替换为 GitHub 链接
8- (忽略代码块和行内代码,只在指定页面生效,支持通配符匹配 )
8+ (忽略代码块和行内代码,只在指定页面生效,支持通配符 )
99 """
1010
1111 repo_url = config .get ("repo_url" , "" ).rstrip ("/" )
1212 if not repo_url :
1313 return markdown
1414
15- # 从 mkdocs.yml 读取页面白名单( 支持通配符)
15+ # 页面白名单, 支持通配符
1616 allowed_pages = config .get ("link_pages" , [])
17- page_src = page .file .src_path # e.g. "docs/releases/v1.0.md"
18-
17+ page_src = page .file .src_path # 相对于 docs/ 的路径
1918 if allowed_pages :
2019 matched = any (fnmatch .fnmatch (page_src , pattern ) for pattern in allowed_pages )
2120 if not matched :
@@ -35,31 +34,28 @@ def store_placeholder(match):
3534 # 提取行内代码(`...`)
3635 markdown = re .sub (r"`.*?`" , store_placeholder , markdown )
3736
38- # issue 替换
37+ # --- issue 替换 ---
38+ # 支持 issue#123 / issue: #123 / issue #123
3939 def issue_replacer (match ):
4040 num = match .group (1 )
4141 return f"issue [#{ num } ]({ repo_url } /issues/{ num } )"
4242
43- markdown = re .sub (
44- r"\bissue\s+#(\d+)" , issue_replacer , markdown , flags = re .IGNORECASE
45- )
43+ markdown = re .sub (r"(?i)issue\s*[:#]?\s*#?(\d+)" , issue_replacer , markdown )
4644
47- # PR 替换
45+ # --- PR 替换 ---
4846 def pr_replacer (match ):
4947 num = match .group (1 )
5048 return f"PR [#{ num } ]({ repo_url } /pull/{ num } )"
5149
52- markdown = re .sub (r"\bPR\s+# (\d+)" , pr_replacer , markdown , flags = re . IGNORECASE )
50+ markdown = re .sub (r"(?i)PR\s*[:#]?\s*#? (\d+)" , pr_replacer , markdown )
5351
54- # commit 替换(显示前 7 位)
52+ # --- commit 替换 ---
5553 def commit_replacer (match ):
5654 sha = match .group (1 )
5755 short_sha = sha [:7 ]
5856 return f"commit [{ short_sha } ]({ repo_url } /commit/{ sha } )"
5957
60- markdown = re .sub (
61- r"\bcommit\s+([0-9a-f]{6,40})" , commit_replacer , markdown , flags = re .IGNORECASE
62- )
58+ markdown = re .sub (r"(?i)commit\s+([0-9a-f]{6,40})" , commit_replacer , markdown )
6359
6460 # 还原代码块和行内代码
6561 for key , value in placeholders .items ():
0 commit comments