Skip to content
This repository was archived by the owner on Nov 23, 2026. It is now read-only.

Commit 3384bf3

Browse files
committed
Add a post-build script that applies the anchor aliases
1 parent cd67c41 commit 3384bf3

4 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ composer.lock
99
.vscode/
1010
site/
1111
venv/
12+
__pycache__/
1213
.envrc
1314
.autoenv
1415
.env

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ verify:
3434
build: $(MKDOCS)
3535
$(MKDOCS) build --strict
3636
$(VENV)/bin/python admin/llms.py
37+
$(VENV)/bin/python admin/anchors.py
3738

3839
serve: $(MKDOCS)
3940
$(MKDOCS) serve

‎admin/anchors.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Adds the Sphinx ids listed in the docs' .anchors.json to the headings of the built site."""
2+
3+
from __future__ import annotations
4+
5+
import html
6+
import json
7+
import re
8+
import sys
9+
from pathlib import Path
10+
11+
from llms import load
12+
13+
14+
def main(config_path: Path) -> None:
15+
config = load(config_path)
16+
base = config_path.parent
17+
docs = base / config.get("docs_dir", "docs")
18+
site = base / config.get("site_dir", "site")
19+
aliases = json.loads((docs / ".anchors.json").read_text(encoding="utf-8"))
20+
21+
for page, headings in aliases.items():
22+
target = site / (page.removesuffix(".md") + ".html")
23+
24+
if not target.is_file():
25+
print(f"WARNING - {page} has aliases but was not built", file=sys.stderr)
26+
continue
27+
28+
content = target.read_text(encoding="utf-8")
29+
30+
for heading, ids in headings.items():
31+
opening = re.compile(rf'(<h[1-6]\b[^>]*\bid="{re.escape(html.escape(heading))}"[^>]*>)')
32+
anchors = "".join(f'<a id="{html.escape(id_)}"></a>' for id_ in ids)
33+
content, count = opening.subn(lambda match: match.group(1) + anchors, content, count=1)
34+
35+
if count == 0:
36+
print(f"WARNING - {page} has no heading #{heading} for its aliases", file=sys.stderr)
37+
38+
target.write_text(content, encoding="utf-8")
39+
40+
41+
if __name__ == "__main__":
42+
main(Path(sys.argv[1] if len(sys.argv) > 1 else "mkdocs.yml"))

‎admin/how_to_build_docs.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This runs `mkdocs build --strict`, which fails on a broken link or a nav entry w
3030
It then runs `admin/llms.py`, which writes `llms.txt`, `llms-full.txt` and a Markdown copy of
3131
each page beside its HTML in `site/`.
3232
The script reads only the nav and the Markdown, so it does not depend on MkDocs.
33+
Finally it runs `admin/anchors.py`, which adds the ids listed in `docs/.anchors.json` to the headings in `site/`, so that links to the anchors of the old Sphinx guide still land.
34+
It warns when a listed heading no longer exists.
3335

3436
## See the Docs
3537

@@ -44,6 +46,7 @@ python3 -m venv venv
4446
venv/bin/pip install -r requirements.txt
4547
venv/bin/mkdocs build --strict
4648
venv/bin/python admin/llms.py
49+
venv/bin/python admin/anchors.py
4750
venv/bin/mkdocs serve
4851
```
4952

0 commit comments

Comments
 (0)