Simplify URL parameter handling in NavigationService and Documentation class

This commit is contained in:
2025-02-10 14:52:19 +11:00
parent 348da29b09
commit fd0bfff4f7

View File

@@ -468,10 +468,16 @@ class NavigationService {
setupEventListeners() { setupEventListeners() {
window.addEventListener('popstate', async () => { window.addEventListener('popstate', async () => {
// Get slug without equal sign and question mark const search = window.location.search;
const slug = window.location.search.replace(/^\?=?/, '').replace(/^=/, ''); // If no search params or just '?', use default page
const hash = window.location.hash; const slug = (search === '' || search === '?') ?
this.eventBus.emit('navigation:requested', { slug, hash }); window._indexData.defaultPage :
search.replace(/^\?/, '');
this.eventBus.emit('navigation:requested', {
slug,
hash: window.location.hash
});
}); });
document.addEventListener('click', async (e) => { document.addEventListener('click', async (e) => {
@@ -527,16 +533,13 @@ class Documentation {
async initialize() { async initialize() {
try { try {
const response = await fetch('index.json'); const response = await fetch('index.json');
if (!response.ok) { if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json(); const data = await response.json();
this.indexData = data; this.indexData = data;
window._indexData = data; window._indexData = data;
// Add this line to populate author info
this.populateAuthorInfo(data.author); this.populateAuthorInfo(data.author);
window.originalDocTitle = data.metadata.site_name || 'Documentation'; window.originalDocTitle = data.metadata.site_name || 'Documentation';
document.title = window.originalDocTitle; document.title = window.originalDocTitle;
@@ -544,26 +547,18 @@ class Documentation {
this.indexData.documents.forEach(doc => this.indexData.documents.forEach(doc =>
this.domService.createFileIndexItem(doc, this.domService.elements.fileIndex)); this.domService.createFileIndexItem(doc, this.domService.elements.fileIndex));
const params = new URLSearchParams(window.location.search); // Simplified URL parameter handling
let slug = ''; const search = window.location.search;
if (params.has('')) { const slug = search === '' || search === '?'
slug = params.get(''); ? this.indexData.defaultPage
} else { : search.replace(/^\?/, '');
for (const [key] of params) {
slug = key;
break;
}
}
slug = slug || this.indexData.defaultPage;
await this.loadDocumentBySlug(slug); await this.loadDocumentBySlug(slug);
if (window.location.hash) { if (window.location.hash) {
setTimeout(() => { setTimeout(() => {
const element = document.getElementById(window.location.hash.slice(1)); const element = document.getElementById(window.location.hash.slice(1));
if (element) { if (element) this.domService.scrollToElement(element);
this.domService.scrollToElement(element);
}
}, 100); }, 100);
} }
} catch (error) { } catch (error) {