added history

This commit is contained in:
2025-02-09 04:02:21 +11:00
parent 76ede99d0f
commit 60d46c2f96

View File

@@ -162,6 +162,7 @@ async function loadIndex() {
try { try {
const response = await fetch('index.json'); const response = await fetch('index.json');
const data = await response.json(); const data = await response.json();
window._indexData = data; // Cache the index data
const fileIndex = document.getElementById('file-index'); const fileIndex = document.getElementById('file-index');
fileIndex.innerHTML = ''; 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 urlParams = new URLSearchParams(window.location.search);
const slug = urlParams.toString().replace(/^=/, '').replace(/^\?/, ''); const slug = urlParams.toString().replace(/^=/, '').replace(/^\?/, '');
if (slug) {
const response = await fetch('index.json'); if (!slug) {
const data = await response.json(); const defaultDoc = findDocumentBySlug(window._indexData.documents, 'welcome');
const matchingDoc = findDocumentBySlug(data.documents, slug); if (defaultDoc) {
if (matchingDoc) { 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); await loadDocument(matchingDoc.path);
} }
} }
@@ -342,6 +360,9 @@ async function loadDocument(path) {
return `\n![${filename}](${mediaPath})\n\n`; return `\n![${filename}](${mediaPath})\n\n`;
}); });
// Track current page for navigation
window._currentPath = path;
// Add Discord-style underline support: __text__ -> <u>text</u> // Add Discord-style underline support: __text__ -> <u>text</u>
finalContent = finalContent.replace(/__(.*?)__/g, '<u>$1</u>'); finalContent = finalContent.replace(/__(.*?)__/g, '<u>$1</u>');
@@ -493,11 +514,10 @@ document.addEventListener('click', async (e) => {
if (target) { if (target) {
e.preventDefault(); e.preventDefault();
const slug = target.href.split('?').pop(); const slug = target.href.split('?').pop();
const indexData = await fetch('index.json').then(res => res.json()); const matchingDoc = findDocumentBySlug(window._indexData.documents, slug);
const matchingDoc = findDocumentBySlug(indexData.documents, slug);
if (matchingDoc) { if (matchingDoc) {
await loadDocument(matchingDoc.path); await loadDocument(matchingDoc.path);
history.pushState(null, '', target.href); history.pushState({ slug }, '', target.href);
} }
} }
}); });