"""Single source of truth for the Threadwell site base.
Two stages:
1. Apply the SPA basepath patch to quartz/components/scripts/spa.inline.ts so
relative URLs in the SPA click handler resolve from a directory base.
2. After `npx quartz build`, rewrite every built HTML file:
- add `` so relative URL resolution works even when
the URL bar lacks a trailing slash
- rewrite the sidebar `` href to the absolute site
base, not the directory the page is in
The base URL is read from `publish/quartz.config.yaml`'s `baseUrl`. Change
that line; the next deploy uses the new value everywhere.
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]
QUARTZ_REPO = VAULT / "site" / "quartz"
SPA_FILE = QUARTZ_REPO / "components" / "scripts" / "spa.inline.ts"
PUBLIC = VAULT / "site" / "public"
OVERLAY = VAULT / "publish" / "quartz.config.yaml"
def read_base_url() -> tuple[str, str]:
"""Read `baseUrl` from the overlay (YAML) as the single source of truth.
Falls back to lenient line scan if YAML parsing isn't available.
"""
text = OVERLAY.read_text(encoding="utf-8")
host = ""
for line in text.splitlines():
stripped = line.strip()
if stripped.startswith("#") or not stripped:
continue
if stripped.startswith("baseUrl:"):
host = stripped.split(":", 1)[1].strip()
break
if not host or "/" not in host:
raise SystemExit(f"missing baseUrl in {OVERLAY}")
if host.startswith("http://"):
scheme = "http://"
host = host[len("http://") :]
elif host.startswith("https://"):
scheme = "https://"
host = host[len("https://") :]
else:
scheme = "https://"
domain, _, path = host.partition("/")
abs_base = (
f"{scheme}{domain}/{path.rstrip('/')}/" if path else f"{scheme}{domain}/"
)
rel_base = "/" + path.rstrip("/") + "/" if path else "/"
return abs_base, rel_base
ABS_BASE, REL_BASE = read_base_url()
HEAD_RE = re.compile(r"(]*>)", re.IGNORECASE)
BASE_TAG = f'\n'
TITLE_LINK = re.compile(
r'(