"""实例「系统说明」:加载 Markdown,生成 h2 目录与带锚点正文.""" from __future__ import annotations import re from functools import lru_cache from html import escape from pathlib import Path from typing import Any from lib.common.markdown_html_lib import render_markdown_html from lib.paths import REPO_ROOT def system_guide_md_path() -> Path: return REPO_ROOT / "docs" / "系统说明.md" def _slugify(text: str) -> str: raw = re.sub(r"<[^>]+>", "", text or "") raw = re.sub(r"\s+", "-", raw.strip()) raw = re.sub(r"[^\w\u4e00-\u9fff\-]+", "", raw) return raw[:80] or "section" def _inject_h2_ids(html: str) -> tuple[str, list[dict[str, str]]]: """为 h2 注入 id,并收集目录(仅 h2).""" toc: list[dict[str, str]] = [] used: dict[str, int] = {} def repl(m: re.Match[str]) -> str: inner = m.group(1) base = _slugify(inner) n = used.get(base, 0) + 1 used[base] = n hid = base if n == 1 else f"{base}-{n}" toc.append({"id": hid, "title": re.sub(r"<[^>]+>", "", inner).strip()}) return f'