66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
"""Patch all built HTML files so the sidebar title link goes to /Threadwell/.
|
|
|
|
The `<h2 class="page-title">` link is generated by the
|
|
`@quartz-community/page-title` plugin from a relative href that breaks
|
|
when the URL bar lacks a trailing slash. We rewrite the href to the
|
|
absolute site base path so it never lands on the bare domain.
|
|
|
|
Idempotent. Safe to re-run.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
SCRIPT = Path(__file__).resolve()
|
|
VAULT = SCRIPT.parents[2]
|
|
|
|
# Detect base path from the publish overlay if present, else default to /Threadwell/
|
|
overlay = VAULT / "publish" / "quartz.config.yaml"
|
|
site_base = "/Threadwell/"
|
|
if overlay.is_file():
|
|
for line in overlay.read_text(encoding="utf-8").splitlines():
|
|
if line.startswith("baseUrl:"):
|
|
host = line.split(":", 1)[1].strip()
|
|
if "/" in host:
|
|
site_base = "/" + host.split("/", 1)[1].rstrip("/") + "/"
|
|
else:
|
|
site_base = "/"
|
|
break
|
|
|
|
PUBLIC = VAULT / "site" / "public"
|
|
|
|
PATTERN = re.compile(
|
|
r'(<h2\s+class="page-title"><a\s+href=")[^"]*(">)'
|
|
)
|
|
REPLACEMENT = rf"\1{site_base}\2"
|
|
|
|
|
|
def patch_html(path: Path) -> bool:
|
|
text = path.read_text(encoding="utf-8", errors="ignore")
|
|
new = PATTERN.sub(REPLACEMENT, text)
|
|
if new != text:
|
|
path.write_text(new, encoding="utf-8")
|
|
return True
|
|
return False
|
|
|
|
|
|
def main() -> int:
|
|
if not PUBLIC.is_dir():
|
|
print(f"missing {PUBLIC}")
|
|
return 1
|
|
changed = 0
|
|
total = 0
|
|
for path in PUBLIC.rglob("*.html"):
|
|
total += 1
|
|
if patch_html(path):
|
|
changed += 1
|
|
print(f"site base: {site_base}")
|
|
print(f"total {total}, changed {changed}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|