This commit is contained in:
Mateusz Gruszczyński committed 2026-11-14 17:24:12 +01:00
1 parent 78dbb31487
commit f48843e934
83 files changed
+7304 -1203

No files matched your search

+27 -9
View File
@@ -99,12 +99,16 @@
const parsed = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(token); const anchor = document.createElement("a");
anchor.textContent = parsed[1]; const target = parsed[2].trim();
if (/^https?:\/\//i.test(target)) { anchor.href = target; anchor.target = "_blank"; anchor.rel = "noreferrer"; }
else if (target.startsWith("#")) { anchor.href = target; }
else if (target.startsWith("#")) {
const section = target.slice(1);
anchor.href = `#doc=${encodeURIComponent(basePath)}&section=${encodeURIComponent(section)}`;
anchor.addEventListener("click", event => { event.preventDefault(); selectDocument(basePath, true, section); });
}
else {
const resolved = normalizeDocPath(basePath, target);
const resolved = normalizeDocPath(basePath, target), section = target.includes("#") ? target.slice(target.indexOf("#") + 1) : "";
if (docsByPath.has(resolved)) {
anchor.href = `#doc=${encodeURIComponent(resolved)}`;
anchor.addEventListener("click", event => { event.preventDefault(); selectDocument(resolved, true); });
anchor.href = `#doc=${encodeURIComponent(resolved)}${section ? `&section=${encodeURIComponent(section)}` : ""}`;
anchor.addEventListener("click", event => { event.preventDefault(); selectDocument(resolved, true, section); });
} else { anchor.href = target; }
}
parent.append(anchor);
@@ -114,6 +118,14 @@
if (offset < text.length) parent.append(document.createTextNode(text.slice(offset)));
}
function headingSlug(text) {
return String(text || "").trim().toLowerCase().replace(/[^\p{L}\p{N}\s_-]/gu, "").replace(/\s/g, "-");
}
function scrollToSection(section) {
if (!section) { element("docs-reader-content").scrollTop = 0; return; }
const node = element("docs-reader-content").querySelector(`[id="${CSS.escape(section)}"]`);
if (node) { node.scrollIntoView({ block: "start" }); node.classList.add("reader-target"); setTimeout(() => node.classList.remove("reader-target"), 1600); }
}
function renderMarkdown(documentInfo) {
const target = element("docs-reader-content"); target.replaceChildren();
const lines = documentInfo.content.replaceAll("\r\n", "\n").split("\n");
@@ -139,7 +151,7 @@
}
table = null;
const heading = /^(#{1,4})\s+(.+)$/.exec(line);
if (heading) { flushParagraph(); closeList(); const h = document.createElement(`h${heading[1].length}`); appendInline(h, heading[2], documentInfo.path); target.append(h); continue; }
if (heading) { flushParagraph(); closeList(); const h = document.createElement(`h${heading[1].length}`); appendInline(h, heading[2], documentInfo.path); h.id = headingSlug(h.textContent); target.append(h); continue; }
const bullet = /^\s*[-*]\s+(.+)$/.exec(line), ordered = /^\s*\d+\.\s+(.+)$/.exec(line);
if (bullet || ordered) {
flushParagraph(); const tag = ordered ? "ol" : "ul";
@@ -170,10 +182,11 @@
}
if (!root.children.length) { const empty = document.createElement("div"); empty.className = "reader-empty"; empty.textContent = t("docsReaderEmpty"); root.append(empty); }
}
function selectDocument(path, updateHash = false) {
function selectDocument(path, updateHash = false, section = "") {
const item = docsByPath.get(path) || docsByPath.get("docs/README.md") || documents[0]; if (!item) return;
currentDocument = item; renderReaderList(); renderMarkdown(item);
if (updateHash) history.replaceState(null, "", `#doc=${encodeURIComponent(item.path)}`);
if (currentDocument !== item) { currentDocument = item; renderReaderList(); renderMarkdown(item); }
if (updateHash) history.replaceState(null, "", `#doc=${encodeURIComponent(item.path)}${section ? `&section=${encodeURIComponent(section)}` : ""}`);
requestAnimationFrame(() => scrollToSection(section));
}
function selectTab(tab) {
@@ -201,9 +214,14 @@
window.document.querySelectorAll("[data-i18n]").forEach(n => n.textContent = t(n.dataset.i18n)); window.document.querySelectorAll("[data-i18n-placeholder]").forEach(n => n.placeholder = t(n.dataset.i18nPlaceholder));
session = info; spec = documentSpec; documents = Array.isArray(docBundle.documents) ? docBundle.documents : []; docsByPath = new Map(documents.map(item => [item.path, item]));
element("docs-session").textContent = `${t("docsAccount")}: ${session.username}`; element("docs-error").textContent = "";
const hash = new URLSearchParams(location.hash.replace(/^#/, "")).get("doc"); selectDocument(hash && docsByPath.has(hash) ? hash : "docs/README.md"); selectTab("reader");
const params = new URLSearchParams(location.hash.replace(/^#/, "")), hash = params.get("doc"); currentDocument = null; selectTab("reader"); selectDocument(hash && docsByPath.has(hash) ? hash : "docs/README.md", false, params.get("section") || "");
} catch (error) { if (mine === epoch) { clear(); element("docs-error").textContent = error.message; } }
}
window.addEventListener("hashchange", () => {
if (!session) return;
const params = new URLSearchParams(location.hash.replace(/^#/, "")), path = params.get("doc");
if (path && docsByPath.has(path)) { selectTab("reader"); selectDocument(path, false, params.get("section") || ""); }
});
document.addEventListener("visibilitychange", () => { if (!document.hidden && session) void checkSession().catch(() => expired()); });
window.addEventListener("pageshow", event => { if (event.persisted) { clear(); void boot(); } });
setInterval(() => { if (!document.hidden && session) void checkSession().catch(() => expired()); }, 30000);