`;
}
// Fill the active page's heading anchors (skipped when a page has <2 headings,
// e.g. the recipe pages, whose place in the tree is enough).
function renderHeadingToc() {
const toc = sidebarEl.querySelector(".s-toc");
if (!toc) return;
const heads = [...contentEl.querySelectorAll("h2, h3")];
if (heads.length < 2) {
toc.remove();
return;
}
toc.innerHTML = heads
.map((h) => {
if (!h.id) h.id = slugify(h.textContent);
const sub = h.tagName === "H3" ? " s-toc-sub" : "";
return `
`;
})
.join("");
}
// Heading anchors scroll in-page without touching location.hash (the hash is
// the SPA's doc route, so a real #id anchor would break navigation).
sidebarEl.addEventListener("click", (e) => {
const a = e.target?.closest?.("a[data-target]");
if (!a) return;
e.preventDefault();
const target = document.getElementById(a.getAttribute("data-target"));
if (!target) return;
// A heading can sit inside a closed ; scrollIntoView on a hidden
// element is a no-op, so open the ancestors first.
for (let d = target.closest("details"); d; d = d.parentElement?.closest("details")) {
d.open = true;
}
const reduce = matchMedia("(prefers-reduced-motion: reduce)").matches;
target.scrollIntoView({
behavior: reduce ? "auto" : "smooth",
block: "start",
});
});
function titleForPath(pages, path) {
for (const p of pages) {
if (normalizeDocPath(p.path) === path) return p.title || path;
for (const c of p.children || []) {
if (normalizeDocPath(c.path) === path) return c.title || path;
}
}
return path;
}
// Skip link: move focus to the content region without polluting the route hash.
document.querySelector(".skip-link")?.addEventListener("click", (e) => {
e.preventDefault();
contentEl.focus();
});
function installContentLinkHandler() {
contentEl.addEventListener("click", (e) => {
const a = e.target?.closest?.("a");
if (!a) return;
const href = a.getAttribute("href") || "";
if (
href.startsWith("http://") ||
href.startsWith("https://") ||
href.startsWith("mailto:") ||
href.startsWith("#")
) {
return;
}
// Route relative markdown links through the SPA.
if (href.endsWith(".md")) {
e.preventDefault();
setSelectedPath(href);
return;
}
});
}
async function main() {
// SPA fallback: convert pathname to hash route so deep links work
if (location.pathname !== "/" && !location.hash) {
location.replace("#" + docPathFromPathname(location.pathname));
return;
}
if (!globalThis.marked) {
contentEl.innerHTML = `
`;
return;
}
try {
const md = await fetchText(`./docs/${activePath}`);
const html = globalThis.marked.parse(md);
contentEl.innerHTML = html;
globalThis.Prism?.highlightAllUnder(contentEl);
applyProfileClientPreference(contentEl);
renderHeadingToc();
window.scrollTo(0, 0);
// On navigation, announce the new page and move focus into it so screen
// reader and keyboard users aren't stranded on stale/orphaned focus.
const liveEl = document.getElementById("a11y-live");
if (liveEl) liveEl.textContent = titleForPath(pages, activePath);
if (isNav) contentEl.focus();
} catch (e) {
contentEl.innerHTML = `
Failed to load ${escapeHtml(
activePath,
)}.
`;
console.error(e);
}
}
window.addEventListener("hashchange", () => render(true));
// Seed the default route without firing hashchange — the synthetic event
// would run the isNav focus path and yank focus into #content on first load.
if (!getSelectedPath() && defaultPath) {
history.replaceState(null, "", pageHref(defaultPath));
}
await render(false);
}
main();
// atproto profile links honor the "open profiles in" preference (clients.js,
// shared across the zzstoatzz/zat.dev sites via the same localStorage key).
// Authors write plain bsky.app profile URLs in markdown; render tags them
// with data-profile-did so clients.js keeps them fresh on preference change.
function applyProfileClientPreference(root) {
if (!window.LeafletClients) return;
root.querySelectorAll('a[href^="https://bsky.app/profile/"]').forEach((a) => {
const id = a.href.slice("https://bsky.app/profile/".length).replace(/\/+$/, "");
if (!id) return;
a.setAttribute("data-profile-did", id);
a.href = window.LeafletClients.profileUrlFor(id);
});
}
// settings popover: where atproto identities open
(function initClientSettings() {
const btn = document.querySelector(".client-settings-toggle");
if (!btn || !window.LeafletClients) return;
const pop = document.getElementById("client-settings");
function renderOptions() {
const current = window.LeafletClients.getPreferredClient().value;
pop.innerHTML =
'