fetch('index.json') .then(response => response.json()) .then(data => { document.title = data.metadata.title || 'Documentation'; }) .catch(error => { console.error('Error loading title:', error); document.title = 'Documentation'; }); class EventBus { constructor() { this.events = {}; } on(event, callback) { if (!this.events[event]) { this.events[event] = []; } this.events[event].push(callback); return () => this.off(event, callback); } off(event, callback) { if (!this.events[event]) return; this.events[event] = this.events[event].filter(cb => cb !== callback); } emit(event, data) { if (!this.events[event]) return; this.events[event].forEach(callback => callback(data)); } } class IndexService { findDocumentBySlug(documents, slug) { for (const doc of documents) { if (doc.slug === slug) return doc; if (doc.type === 'folder') { const found = this.findDocumentBySlug(doc.items, slug); if (found) return found; } } return null; } findDocumentByTitle(documents, title) { for (const doc of documents) { if (doc.type === 'folder') { const found = this.findDocumentByTitle(doc.items, title); if (found) return found; } else if (doc.title === title || doc.path.endsWith(title + '.md') || doc.slug === title.toLowerCase().replace(/ /g, '-')) { return doc; } } return null; } findParentFolders(documents, path, parentFolders = []) { for (const doc of documents) { if (doc.type === 'folder') { const found = doc.items.find(item => { if (item.path === path) return true; if (item.type === 'folder') { return this.findParentFolders([item], path).length > 0; } return false; }); if (found) { parentFolders.push(doc); doc.items.forEach(item => { if (item.type === 'folder') { this.findParentFolders([item], path, parentFolders); } }); } } } return parentFolders; } } class DOMService { constructor(eventBus) { this.eventBus = eventBus; this.elements = { content: document.getElementById('document-content'), outline: document.getElementById('document-outline'), fileIndex: document.getElementById('file-index'), titleText: document.querySelector('.title-text .page-title'), leftSidebar: document.querySelector('.left-sidebar'), menuButton: document.querySelector('.menu-button'), header: document.querySelector('title-bar') // Add header element }; this.headerOffset = 60; // Fixed header heightfsetHeight || 0; // Get header height } setupMobileMenu() { this.elements.menuButton.addEventListener('click', () => this.elements.leftSidebar.classList.toggle('show')); this.elements.content.addEventListener('click', () => this.elements.leftSidebar.classList.remove('show')); } setContent(html) { this.elements.content.innerHTML = html; this.elements.content.className = 'markdown-content'; hljs.highlightAll(); } setTitle(title) { document.title = `${window.originalDocTitle} / ${title}`; this.elements.titleText.textContent = title; } setError(message) { this.elements.content.innerHTML = `
${highlighted}`;
}
extractMetadata(content) {
const lines = content.trim().split('\n');
let metadata = {};
let contentStart = 0;
if (lines[0].trim() === '---') {
let endMetadata = lines.findIndex((line, index) => index > 0 && line.trim() === '---');
if (endMetadata !== -1) {
metadata = Object.fromEntries(
lines.slice(1, endMetadata)
.map(line => line.match(/^([\w-]+):\s*(.*)$/))
.filter(Boolean)
.map(([, key, value]) => [key, value.trim()])
);
contentStart = endMetadata + 1;
}
}
return {
metadata,
content: lines.slice(contentStart).join('\n').trim()
};
}
async loadDocument(path) {
try {
const [response, marked] = await Promise.all([
fetch(path),
this.markedPromise
]);
let rawContent = await response.text();
const { metadata, content } = this.extractMetadata(rawContent);
const basePath = path.substring(0, path.lastIndexOf('/'));
const indexDoc = this.findDocInIndex(path);
let processedContent = this.processWikiLinks(content);
processedContent = this.processImages(processedContent, basePath);
const titleContent = metadata.title || indexDoc?.title || path.split('/').pop().replace('.md', '');
processedContent = this.ensureTitle(processedContent, titleContent);
return {
content: processedContent,
metadata,
marked,
title: titleContent
};
} catch (error) {
throw new Error('Failed to load document');
}
}
findDocInIndex(path) {
let doc = window._indexData.documents.find(d => d.path === path);
if (!doc) {
for (const d of window._indexData.documents) {
if (d.type === 'folder' && d.items) {
doc = d.items.find(item => item.path === path);
if (doc) break;
}
}
}
return doc;
}
ensureTitle(content, title) {
content = content.replace(/^#\s+.*$/m, '').trim();
return `# ${title}\n\n${content}`;
}
processWikiLinks(content) {
return content.replace(/\[\[(.*?)\]\]/g, (match, linkText) => {
const [targetTitle, displayText] = linkText.split('|').map(s => s.trim());
if (targetTitle.match(/\.(png|jpg|jpeg|gif|mp4|webm)$/i)) {
return match;
}
const doc = this.indexService.findDocumentByTitle(
window._indexData.documents,
targetTitle
);
return doc ? `[${displayText || doc.title}](?${doc.slug})` : match;
});
}
processImages(content, basePath) {
return content.replace(/!\[\[(.*?)\]\]/g, (match, filename) => {
const mediaPath = `${basePath}/images/${filename}`;
if (filename.toLowerCase().endsWith('.mp4')) {
return `\n\n\n`;
}
return `\n\n\n`;
});
}
}
class NavigationService {
constructor(eventBus, documentService) {
this.eventBus = eventBus;
this.documentService = documentService;
this.setupEventListeners();
}
setupEventListeners() {
window.addEventListener('popstate', async () => {
const slug = new URLSearchParams(window.location.search).toString()
.replace(/^=/, '').replace(/^\?/, '');
const hash = window.location.hash;
this.eventBus.emit('navigation:requested', { slug, hash });
});
document.addEventListener('click', async (e) => {
const target = e.target.closest('a[data-internal="true"]');
if (target) {
e.preventDefault();
const slug = target.href.split('?').pop();
history.pushState({ slug }, '', target.href);
this.eventBus.emit('navigation:requested', { slug });
}
});
}
}
class Documentation {
constructor() {
this.eventBus = new EventBus();
this.indexService = new IndexService();
this.domService = new DOMService(this.eventBus);
this.documentService = new DocumentService(this.eventBus, this.indexService);
this.navigationService = new NavigationService(this.eventBus, this.documentService);
this.setupEventListeners();
}
setupEventListeners() {
this.eventBus.on('navigation:requested', async ({ slug }) => {
slug = slug.replace(/^[?=]/, '');
await this.loadDocumentBySlug(slug);
});
this.eventBus.on('document:load', async ({ path }) => {
await this.loadDocument(path);
});
window.addEventListener('load', () => {
this.domService.setupMobileMenu();
this.initialize();
});
document.addEventListener('click', async (e) => {
const target = e.target.closest('a[data-internal="true"]');
if (target) {
e.preventDefault();
const slug = target.href.split('?').pop();
history.pushState(null, '', target.href);
await this.loadDocumentBySlug(slug);
}
});
}
async initialize() {
try {
const response = await fetch('index.json');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
this.indexData = data;
window._indexData = data;
// Add this line to populate author info
this.populateAuthorInfo(data.author);
window.originalDocTitle = data.title || 'Documentation';
document.title = window.originalDocTitle;
this.domService.elements.fileIndex.innerHTML = '';
this.indexData.documents.forEach(doc =>
this.domService.createFileIndexItem(doc, this.domService.elements.fileIndex));
const params = new URLSearchParams(window.location.search);
let slug = '';
if (params.has('')) {
slug = params.get('');
} else {
for (const [key] of params) {
slug = key;
break;
}
}
slug = slug || this.indexData.defaultPage;
await this.loadDocumentBySlug(slug);
if (window.location.hash) {
setTimeout(() => {
const element = document.getElementById(window.location.hash.slice(1));
if (element) {
this.domService.scrollToElement(element);
}
}, 100);
}
} catch (error) {
this.domService.setError(`Failed to load documentation index: ${error.message}`);
}
}
populateAuthorInfo(author) {
const subtitleName = document.querySelector('.name');
const subtitleRole = document.querySelector('.role');
if (!subtitleName || !subtitleRole) return;
subtitleName.textContent = author.name || '';
subtitleRole.textContent = author.role || '';
const socials = document.querySelector('.social-links');
if (socials) {
socials.innerHTML = '';
if (author.socials) {
author.socials.forEach(s => {
const link = document.createElement('a');
link.href = s.url;
link.target = '_blank';
link.title = s.title;
link.innerHTML = ``;
socials.appendChild(link);
});
}
}
}
async loadDocumentBySlug(slug) {
const doc = this.indexService.findDocumentBySlug(this.indexData.documents, slug);
if (!doc) {
this.domService.setError(`Document not found: ${slug}`);
return;
}
if (doc.type === 'folder') {
if (doc.path) {
await this.loadDocument(doc.path);
} else if (doc.items?.length > 0) {
await this.loadDocument(doc.items[0].path);
} else {
this.domService.setError('This folder is empty.');
}
return;
}
await this.loadDocument(doc.path);
}
async loadDocument(path) {
try {
const { content, metadata, marked } = await this.documentService.loadDocument(path);
this.domService.setTitle(metadata.site_name);
this.domService.setContent(marked.parse(content));
this.domService.updateActiveDocument(path);
const headings = document.querySelectorAll('h2, h3, h4, h5, h6');
const headingLinks = this.domService.createOutline(headings);
this.setupScrollObserver(headings, headingLinks);
window._currentPath = path;
if (window.location.hash) {
const element = document.getElementById(window.location.hash.slice(1));
if (element) {
setTimeout(() => {
this.domService.scrollToElement(element);
}, 100);
}
} else {
window.scrollTo({ top: 0, behavior: 'smooth' });
}
} catch (error) {
this.domService.setError('Error loading document. Please try again.');
}
}
setupScrollObserver(headings, headingLinks) {
const observer = new IntersectionObserver(
(entries) => {
const visibleHeadings = entries
.filter(entry => entry.isIntersecting)
.sort((a, b) => a.target.offsetTop - b.target.offsetTop);
if (visibleHeadings.length) {
this.domService.elements.outline
.querySelectorAll('a')
.forEach(a => a.classList.remove('active'));
const link = headingLinks.get(visibleHeadings[0].target);
if (link) link.classList.add('active');
}
},
{
rootMargin: '-48px 0px -60% 0px',
threshold: [0, 0.25, 0.5, 0.75, 1]
}
);
headings.forEach(heading => observer.observe(heading));
return observer;
}
}
window.originalDocTitle = document.title;
const docs = new Documentation();