Files
Yimu c16a754752 新增「大题十年考点」系列:按十年解答题倒推的 15 个考点族
- 19 个 md:README + 01-知识点盘点(§0.4 反查 + §1—§15 地图)
  + 01b-覆盖对照与接口 + 15 个点文件 + 17-速查卡
- 62 题、57 个知识点;覆盖高数 45 + 线代 15 + 概率 15 共 75 道解答题
  (820 分:高数 480 / 线代 170 / 概率 170)
- 题量按「核心 ★★★ 2—3 题、边缘 ★★☆ 1—2 题」落地,
  题面标注真题骨架年份者为结构对齐,未标注者为原创

同批收尾:
- 逐题数值复核,修 7 处,其中两处答案错:
  点 8 题 4(2022·19 斯托克斯)由 -π/32 改为 0;
  点 12 曲面识别表「一正两零」改为两张平行平面
- 01-知识点盘点 曾达 1.58 万字符,§0.1—§0.3 迁入 01b,回落至 1.34 万
- gen_crosslinks.py 兼容新点文件命名与盘点名变体
- check_md_links.py 对 %20 编码路径做 unquote 解码
- 新增 stat_difficulty.py:难度分布 / 多问结构 / 体积自检
2026-11-01 21:43:01 +08:00

98 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
"""校验 Markdown 内部链接:目标文件是否存在、锚点是否命中。
Gitea 规则(实测):
标题 id = "user-content-" + anchor(标题原文)
anchor = 小写,保留字母数字汉字与下划线,空格变 -,其余字符全删
同文件链接写 #anchor —— Gitea 渲染时自动补 user-content-
跨文件链接写 file.md#user-content-anchor —— Gitea 不补,必须手写
用法:
python check_md_links.py <目录或文件> [更多...]
"""
import os
import re
import sys
from urllib.parse import unquote
def anchor(title):
out = []
for ch in title.lower():
if ch.isalnum() or ch == '_':
out.append(ch)
elif ch in ' \t':
out.append('-')
return ''.join(out)
def read(p):
with open(p, encoding='utf-8', newline='') as f:
return f.read().replace('\r\n', '\n')
def headings(txt):
return [m.group(1).strip()
for m in re.finditer(r'^#{1,6}\s+(.*)$', txt, re.M)]
def collect(paths):
files = []
for p in paths:
if os.path.isdir(p):
for fn in sorted(os.listdir(p)):
if fn.endswith('.md'):
files.append(os.path.join(p, fn))
else:
files.append(p)
return files
LINK = re.compile(r'\[([^\]]*)\]\(([^)\s]+)\)')
def main(paths):
files = collect(paths)
ids = {}
for p in files:
ids[os.path.abspath(p)] = {anchor(t) for t in headings(read(p))}
bad = 0
total = 0
for p in files:
base = os.path.dirname(p)
txt = read(p)
# 跳过围栏代码块中的链接
txt = re.sub(r'^```.*?^```', '', txt, flags=re.M | re.S)
for text, target in LINK.findall(txt):
if target.startswith(('http://', 'https://', 'mailto:')):
continue
total += 1
if '#' in target:
tfile, frag = target.split('#', 1)
else:
tfile, frag = target, None
# 链接里的空格常写成 %20(参考仓库目录名带空格),先解码再判文件
tfile = unquote(tfile)
frag = unquote(frag) if frag else frag
tgt = os.path.abspath(os.path.join(base, tfile)) if tfile else os.path.abspath(p)
tag = '%s -> %s' % (os.path.basename(p), target)
if not os.path.exists(tgt):
print(' ✗ 文件不存在 %s' % tag)
bad += 1
continue
if frag is None:
continue
pure = frag[len('user-content-'):] if frag.startswith('user-content-') else frag
if pure not in ids.get(tgt, set()):
print(' ✗ 锚点未命中 %s' % tag)
bad += 1
print('检查 %d 个文件、%d 条内部链接,异常 %d 条' % (len(files), total, bad))
return 0 if bad == 0 else 1
if __name__ == '__main__':
if len(sys.argv) < 2:
raise SystemExit(__doc__)
raise SystemExit(main(sys.argv[1:]))