"""Keep in-site links under the path from `baseUrl`.
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:
- set `` to that page's directory under the site path so
Quartz page-relative `../` links do not resolve against the domain root
- rewrite the sidebar `` href to the site path
`baseUrl` in `publish/quartz.config.yaml` is the single source of truth.
Idempotent. Safe to re-run.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
SCRIPT: Path = Path(__file__).resolve()
VAULT: Path = SCRIPT.parents[2]
QUARTZ_REPO: Path = VAULT / "site" / "quartz"
SPA_FILE: Path = QUARTZ_REPO / "components" / "scripts" / "spa.inline.ts"
SEARCH_FILE: Path = (
VAULT
/ "site"
/ "node_modules"
/ "@quartz-community"
/ "search"
/ "dist"
/ "components"
/ "index.js"
)
PUBLIC: Path = VAULT / "site" / "public"
OVERLAY: Path = 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:
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.Pattern[str] = re.compile(r"(]*>)", re.IGNORECASE)
BASE_RE: re.Pattern[str] = re.compile(
r"\s*", re.IGNORECASE
)
TITLE_LINK: re.Pattern[str] = re.compile(
r'(