Skip to content

Commit fd0bfff

Browse files
committed
Simplify URL parameter handling in NavigationService and Documentation class
1 parent 348da29 commit fd0bfff

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

index.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,16 @@ class NavigationService {
468468

469469
setupEventListeners() {
470470
window.addEventListener('popstate', async () => {
471-
// Get slug without equal sign and question mark
472-
const slug = window.location.search.replace(/^\?=?/, '').replace(/^=/, '');
473-
const hash = window.location.hash;
474-
this.eventBus.emit('navigation:requested', { slug, hash });
471+
const search = window.location.search;
472+
// If no search params or just '?', use default page
473+
const slug = (search === '' || search === '?') ?
474+
window._indexData.defaultPage :
475+
search.replace(/^\?/, '');
476+
477+
this.eventBus.emit('navigation:requested', {
478+
slug,
479+
hash: window.location.hash
480+
});
475481
});
476482

477483
document.addEventListener('click', async (e) => {
@@ -527,43 +533,32 @@ class Documentation {
527533
async initialize() {
528534
try {
529535
const response = await fetch('index.json');
530-
if (!response.ok) {
531-
throw new Error(`HTTP error! status: ${response.status}`);
532-
}
536+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
537+
533538
const data = await response.json();
534539
this.indexData = data;
535540
window._indexData = data;
536541

537-
// Add this line to populate author info
538542
this.populateAuthorInfo(data.author);
539-
540543
window.originalDocTitle = data.metadata.site_name || 'Documentation';
541544
document.title = window.originalDocTitle;
542545

543546
this.domService.elements.fileIndex.innerHTML = '';
544547
this.indexData.documents.forEach(doc =>
545548
this.domService.createFileIndexItem(doc, this.domService.elements.fileIndex));
546549

547-
const params = new URLSearchParams(window.location.search);
548-
let slug = '';
549-
if (params.has('')) {
550-
slug = params.get('');
551-
} else {
552-
for (const [key] of params) {
553-
slug = key;
554-
break;
555-
}
556-
}
557-
slug = slug || this.indexData.defaultPage;
550+
// Simplified URL parameter handling
551+
const search = window.location.search;
552+
const slug = search === '' || search === '?'
553+
? this.indexData.defaultPage
554+
: search.replace(/^\?/, '');
558555

559556
await this.loadDocumentBySlug(slug);
560-
557+
561558
if (window.location.hash) {
562559
setTimeout(() => {
563560
const element = document.getElementById(window.location.hash.slice(1));
564-
if (element) {
565-
this.domService.scrollToElement(element);
566-
}
561+
if (element) this.domService.scrollToElement(element);
567562
}, 100);
568563
}
569564
} catch (error) {

0 commit comments

Comments
 (0)