Refactor document loading logic and improve error handling for empty folders and missing documents

This commit is contained in:
2025-02-09 03:39:23 +11:00
parent 84235ef885
commit ff7d611140

View File

@@ -147,11 +147,12 @@ function findParentFolders(documents, path, parentFolders = []) {
function findDocumentBySlug(documents, slug) { function findDocumentBySlug(documents, slug) {
for (const doc of documents) { for (const doc of documents) {
if (doc.slug === slug) {
return doc;
}
if (doc.type === 'folder') { if (doc.type === 'folder') {
const found = findDocumentBySlug(doc.items, slug); const found = findDocumentBySlug(doc.items, slug);
if (found) return found; if (found) return found;
} else if (doc.slug === slug) {
return doc;
} }
} }
return null; return null;
@@ -173,7 +174,24 @@ async function loadIndex() {
const matchingDoc = findDocumentBySlug(data.documents, slug); const matchingDoc = findDocumentBySlug(data.documents, slug);
if (matchingDoc) { if (matchingDoc) {
if (matchingDoc.type === 'folder') {
// If the folder has a path, load that document. Otherwise, load the first item.
if (matchingDoc.path) {
await loadDocument(matchingDoc.path); await loadDocument(matchingDoc.path);
} else if (matchingDoc.items && matchingDoc.items.length > 0) {
await loadDocument(matchingDoc.items[0].path);
} else {
// If the folder has no items, display a default message.
document.getElementById('document-content').innerHTML =
'<div class="error">This folder is empty.</div>';
}
} else {
await loadDocument(matchingDoc.path);
}
} else {
// If no matching doc is found, display an error message.
document.getElementById('document-content').innerHTML =
'<div class="error">Document not found.</div>';
} }
} else { } else {
const defaultDoc = findDocumentBySlug(data.documents, 'welcome'); const defaultDoc = findDocumentBySlug(data.documents, 'welcome');
@@ -238,8 +256,14 @@ function findDocumentByTitle(documents, title) {
if (doc.type === 'folder') { if (doc.type === 'folder') {
const found = findDocumentByTitle(doc.items, title); const found = findDocumentByTitle(doc.items, title);
if (found) return found; if (found) return found;
} else if (doc.title === title || doc.path.endsWith(title + '.md')) { } else {
if (doc.title === title) {
return doc; return doc;
} else if (doc.path.endsWith(title + '.md')) {
return doc;
} else if (doc.slug === title.toLowerCase().replace(/ /g, '-')) {
return doc;
}
} }
} }
return null; return null;
@@ -290,15 +314,17 @@ async function loadDocument(path) {
document.querySelector('.title-text .page-title').textContent = titleContent; document.querySelector('.title-text .page-title').textContent = titleContent;
let processedContent = content.replace(/\[\[(.*?)\]\]/g, (match, linkText) => { let processedContent = content.replace(/\[\[(.*?)\]\]/g, (match, linkText) => {
if (linkText.match(/\.(png|jpg|jpeg|gif|mp4|webm)$/i)) { const [targetTitle, displayText] = linkText.split('|').map(s => s.trim());
if (targetTitle.match(/\.(png|jpg|jpeg|gif|mp4|webm)$/i)) {
return match; return match;
} }
const doc = findDocumentByTitle(indexData.documents, linkText); const doc = findDocumentByTitle(indexData.documents, targetTitle);
if (doc) { if (doc) {
return `[${doc.title}](?${doc.slug})`; return `[${displayText || doc.title}](?${doc.slug})`;
} } else {
return match; return match;
}
}); });
processedContent = `# ${titleContent}\n\n${processedContent}`; processedContent = `# ${titleContent}\n\n${processedContent}`;