/** * Dynamic Page Content Loader * Loads page content from the API and renders it with proper formatting */ // Convert Quill Delta to HTML - accurate conversion matching backend format function convertDeltaToHtml(delta) { if (!delta || !delta.ops) return ""; let html = ""; let currentLine = ""; let inListType = null; // 'bullet' or 'ordered' const ops = delta.ops; for (let i = 0; i < ops.length; i++) { const op = ops[i]; const nextOp = ops[i + 1]; if (typeof op.insert === "string") { const text = op.insert; const inlineAttrs = op.attributes || {}; // Check if this is a standalone newline with block attributes if (text === "\n") { const blockAttrs = inlineAttrs; // Handle list transitions if (blockAttrs.list) { const newListType = blockAttrs.list; if (inListType !== newListType) { if (inListType) { html += inListType === "ordered" ? "" : ""; } html += newListType === "ordered" ? "
${currentLine}`; } else if (blockAttrs["code-block"]) { html += `
${currentLine}`;
} else if (currentLine) {
html += `${currentLine}
`; } } currentLine = ""; } else { // Regular text - may contain embedded newlines const parts = text.split("\n"); for (let j = 0; j < parts.length; j++) { const part = parts[j]; // Format the text part if (part.length > 0) { let formatted = escapeHtml(part); // Apply inline formatting if (inlineAttrs.bold) formatted = `${formatted}`; if (inlineAttrs.italic) formatted = `${formatted}`; if (inlineAttrs.underline) formatted = `${formatted}`; if (inlineAttrs.strike) formatted = `${formatted}`;
if (inlineAttrs.link)
formatted = `${formatted}`;
currentLine += formatted;
}
// Handle embedded newlines (not the last part)
if (j < parts.length - 1) {
// Close any open list for embedded newlines
if (inListType) {
html += inListType === "ordered" ? "" : "";
inListType = null;
}
if (currentLine) {
html += `${currentLine}
`; } currentLine = ""; } } } } else if (op.insert && op.insert.image) { // Flush pending content if (currentLine) { if (inListType) { html += `${currentLine}
`; } currentLine = ""; } html += `${currentLine}
`; } return html; } function escapeHtml(text) { return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } // Parse and render page content (handles both Delta JSON and raw HTML) function parsePageContent(content) { if (!content) return "Content coming soon...
"; try { const delta = JSON.parse(content); if (delta.ops) { return convertDeltaToHtml(delta); } return content; } catch (e) { // Not JSON, return as-is (probably HTML) return content; } } // Load page content from API async function loadPageContent(slug, options = {}) { const { titleSelector = "#pageTitle", contentSelector = "#dynamicContent", staticSelector = "#staticContent", showLoading = true, } = options; const dynamicContent = document.querySelector(contentSelector); const staticContent = document.querySelector(staticSelector); const titleElement = document.querySelector(titleSelector); if (!dynamicContent) { console.warn("Dynamic content container not found:", contentSelector); return null; } if (showLoading) { dynamicContent.innerHTML = `Loading content...