app.js
docs/assets/app.js
1const SITE_PAGES = [2 { id: "home", label: "Overview", href: "./index.html" },3 { id: "start", label: "Get started", href: "./getting-started.html" },4 { id: "architecture", label: "Architecture", href: "./architecture.html" },5 { id: "patterns", label: "Patterns", href: "./patterns.html" },6];78const ICONS = {9 sun: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="3.5"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/></svg>',10 moon: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M20 15.3A8.4 8.4 0 0 1 8.7 4a8.4 8.4 0 1 0 11.3 11.3Z"/></svg>',11 menu: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M4 7h16M4 12h16M4 17h16"/></svg>',12 repository: '<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M4 4.8A2.8 2.8 0 0 1 6.8 2H20v16H6.8A2.8 2.8 0 0 0 4 20.8V4.8Z"/><path d="M4 20.8A2.8 2.8 0 0 1 6.8 18H20M8 6h8"/></svg>',13};1415function currentTheme() {16 return document.documentElement.dataset.theme || "light";17}1819function updateThemeButton(button) {20 const dark = currentTheme() === "dark";21 button.innerHTML = dark ? ICONS.sun : ICONS.moon;22 button.setAttribute("aria-label", dark ? "Use light theme" : "Use dark theme");23 button.title = dark ? "Use light theme" : "Use dark theme";24}2526function renderHeader() {27 const target = document.querySelector("[data-site-header]");28 if (!target) return;29 const current = document.body.dataset.page || "home";30 target.className = "site-header";31 target.innerHTML = `32 <div class="header-inner">33 <a class="brand" href="./index.html" aria-label="Yokan KMP Field Guide home">34 <img src="./assets/mark.svg" alt="" width="34" height="34">35 <span>Yokan KMP <small>field guide</small></span>36 </a>37 <nav class="site-nav" id="site-navigation" aria-label="Primary navigation">38 ${SITE_PAGES.map((page) => `<a href="${page.href}"${page.id === current ? ' aria-current="page"' : ""}>${page.label}</a>`).join("")}39 </nav>40 <div class="header-actions">41 <a class="repo-link" href="https://codeberg.org/FriedCutlet/yokan" aria-label="Open Yokan source repository">42 ${ICONS.repository}<span class="repo-label">Source</span>43 </a>44 <button class="icon-button" type="button" data-theme-toggle></button>45 <button class="menu-toggle" type="button" aria-controls="site-navigation" aria-expanded="false" aria-label="Open navigation">${ICONS.menu}</button>46 </div>47 </div>`;4849 const themeButton = target.querySelector("[data-theme-toggle]");50 updateThemeButton(themeButton);51 themeButton.addEventListener("click", () => {52 const next = currentTheme() === "dark" ? "light" : "dark";53 document.documentElement.dataset.theme = next;54 localStorage.setItem("yokan-docs-theme", next);55 updateThemeButton(themeButton);56 });5758 const menuButton = target.querySelector(".menu-toggle");59 const navigation = target.querySelector(".site-nav");60 menuButton.addEventListener("click", () => {61 const open = navigation.classList.toggle("open");62 menuButton.setAttribute("aria-expanded", String(open));63 menuButton.setAttribute("aria-label", open ? "Close navigation" : "Open navigation");64 });65 navigation.addEventListener("click", () => {66 navigation.classList.remove("open");67 menuButton.setAttribute("aria-expanded", "false");68 });69}7071function renderFooter() {72 const target = document.querySelector("[data-site-footer]");73 if (!target) return;74 target.className = "site-footer";75 target.innerHTML = `76 <div class="footer-inner">77 <div>78 <a class="brand" href="./index.html"><img src="./assets/mark.svg" alt="" width="34" height="34"><span>Yokan KMP Field Guide</span></a>79 <p style="margin-top: 14px">A project-led introduction to Kotlin Multiplatform, shared Compose UI, Room, Ktor, and platform boundaries.</p>80 </div>81 <div class="footer-links">82 <strong>Learn</strong>83 ${SITE_PAGES.slice(1).map((page) => `<a href="${page.href}">${page.label}</a>`).join("")}84 </div>85 <div class="footer-links">86 <strong>Reference</strong>87 <a href="https://kotlinlang.org/docs/multiplatform.html">Kotlin Multiplatform</a>88 <a href="https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-multiplatform.html">Compose Multiplatform</a>89 <a href="./MPV_INTEGRATION.md">Yokan MPV integration</a>90 <a href="https://codeberg.org/FriedCutlet/yokan">Source repository</a>91 </div>92 </div>`;93}9495function buildTableOfContents() {96 const target = document.querySelector("[data-toc]");97 if (!target) return;98 const headings = [...document.querySelectorAll(".doc-section h2[id], .doc-section h3[id]")];99 if (!headings.length) {100 target.hidden = true;101 return;102 }103 target.innerHTML = `<span class="nav-label">On this page</span><ul>${headings.map((heading) => `104 <li><a href="#${heading.id}"${heading.tagName === "H3" ? ' style="padding-left: 18px"' : ""}>${heading.textContent}</a></li>`).join("")}</ul>`;105106 const links = new Map([...target.querySelectorAll("a")].map((link) => [link.hash.slice(1), link]));107 if (!("IntersectionObserver" in window)) return;108 const observer = new IntersectionObserver((entries) => {109 const visible = entries.filter((entry) => entry.isIntersecting).sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top)[0];110 if (!visible) return;111 links.forEach((link) => link.classList.remove("active"));112 links.get(visible.target.id)?.classList.add("active");113 }, { rootMargin: "-18% 0px -70%", threshold: 0 });114 headings.forEach((heading) => observer.observe(heading));115}116117function enhanceCodeBlocks() {118 document.querySelectorAll(".code-block").forEach((block) => {119 const code = block.querySelector("pre code");120 const label = block.querySelector(".code-label");121 if (!code || !label || label.querySelector("button")) return;122 const button = document.createElement("button");123 button.className = "copy-button";124 button.type = "button";125 button.textContent = "Copy";126 button.setAttribute("aria-label", "Copy code");127 button.addEventListener("click", async () => {128 try {129 await navigator.clipboard.writeText(code.textContent);130 button.textContent = "Copied";131 } catch {132 button.textContent = "Select code";133 const selection = window.getSelection();134 const range = document.createRange();135 range.selectNodeContents(code);136 selection.removeAllRanges();137 selection.addRange(range);138 }139 window.setTimeout(() => { button.textContent = "Copy"; }, 1600);140 });141 label.append(button);142 });143}144145function installReadingProgress() {146 const host = document.createElement("div");147 host.className = "reading-progress";148 host.setAttribute("aria-hidden", "true");149 host.innerHTML = "<span></span>";150 document.body.append(host);151 const bar = host.firstElementChild;152 const update = () => {153 const distance = document.documentElement.scrollHeight - window.innerHeight;154 bar.style.width = `${distance > 0 ? Math.min(100, (window.scrollY / distance) * 100) : 0}%`;155 };156 document.addEventListener("scroll", update, { passive: true });157 update();158}159160renderHeader();161renderFooter();162buildTableOfContents();163enhanceCodeBlocks();164installReadingProgress();165