From 60d46c2f961976a9af628fdc32140e042583acb9 Mon Sep 17 00:00:00 2001 From: Litruv Date: Sun, 9 Feb 2025 04:02:21 +1100 Subject: [PATCH] added history --- index.js | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 3852ec3..52e5e4a 100644 --- a/index.js +++ b/index.js @@ -162,6 +162,7 @@ async function loadIndex() { try { const response = await fetch('index.json'); const data = await response.json(); + window._indexData = data; // Cache the index data const fileIndex = document.getElementById('file-index'); fileIndex.innerHTML = ''; @@ -205,14 +206,31 @@ async function loadIndex() { } } -window.addEventListener('popstate', async () => { +window.addEventListener('popstate', async (event) => { const urlParams = new URLSearchParams(window.location.search); const slug = urlParams.toString().replace(/^=/, '').replace(/^\?/, ''); - if (slug) { - const response = await fetch('index.json'); - const data = await response.json(); - const matchingDoc = findDocumentBySlug(data.documents, slug); - if (matchingDoc) { + + if (!slug) { + const defaultDoc = findDocumentBySlug(window._indexData.documents, 'welcome'); + if (defaultDoc) { + await loadDocument(defaultDoc.path); + } + return; + } + + const response = await fetch('index.json'); + const data = await response.json(); + window._indexData = data; // Cache the index data + + const matchingDoc = findDocumentBySlug(data.documents, slug); + if (matchingDoc) { + if (matchingDoc.type === 'folder') { + if (matchingDoc.path) { + await loadDocument(matchingDoc.path); + } else if (matchingDoc.items && matchingDoc.items.length > 0) { + await loadDocument(matchingDoc.items[0].path); + } + } else { await loadDocument(matchingDoc.path); } } @@ -342,6 +360,9 @@ async function loadDocument(path) { return `\n![${filename}](${mediaPath})\n\n`; }); + // Track current page for navigation + window._currentPath = path; + // Add Discord-style underline support: __text__ -> text finalContent = finalContent.replace(/__(.*?)__/g, '$1'); @@ -493,11 +514,10 @@ document.addEventListener('click', async (e) => { if (target) { e.preventDefault(); const slug = target.href.split('?').pop(); - const indexData = await fetch('index.json').then(res => res.json()); - const matchingDoc = findDocumentBySlug(indexData.documents, slug); + const matchingDoc = findDocumentBySlug(window._indexData.documents, slug); if (matchingDoc) { await loadDocument(matchingDoc.path); - history.pushState(null, '', target.href); + history.pushState({ slug }, '', target.href); } } });