mirror of
https://github.com/litruv/Docs-Viewer.git
synced 2026-07-24 02:36:07 +10:00
added history
This commit is contained in:
38
index.js
38
index.js
@@ -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\n\n`;
|
return `\n\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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user