Spaces:
Running
Running
File size: 5,737 Bytes
0b194e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
/*
ELYSIA MARKDOWN STUDIO v1.0 - Preview Module
Real-time Markdown preview with extensions
*/
import Utils from "./utils.js";
const Preview = {
container: null,
renderer: null,
init() {
this.container = document.getElementById("markdown-preview");
this.setupMarked();
this.setupMermaid();
},
setupMarked() {
// Configure marked.js v15+ (async API)
marked.setOptions({
breaks: true,
gfm: true, // GitHub Flavored Markdown
headerIds: true
});
// Custom renderer for task lists (Marked.js v15+ token-based API)
const renderer = {
listitem(token) {
// In v15+, token has 'task' and 'checked' properties
if (token.task) {
return `<li class="task-list-item">
<input type="checkbox" ${token.checked ? "checked" : ""} disabled> ${token.text}
</li>\n`;
}
// Regular list item
return `<li>${token.text}</li>\n`;
},
code(token) {
const lang = token.lang || "";
const code = token.text;
// Highlight with Prism if available
if (lang && window.Prism && Prism.languages[lang]) {
const highlighted = Prism.highlight(code, Prism.languages[lang], lang);
return `<pre><code class="language-${lang}">${highlighted}</code></pre>\n`;
}
return `<pre><code>${code}</code></pre>\n`;
}
};
marked.use({ renderer });
},
setupMermaid() {
if (window.mermaid) {
mermaid.initialize({
startOnLoad: false,
theme: "dark",
securityLevel: "loose"
});
}
},
update() {
const content = window.app?.editor.getContent() || "";
this.render(content);
},
async render(markdown) {
if (!markdown) {
this.container.innerHTML = '<p style="color: var(--text-tertiary); text-align: center; padding: 2rem;">Start writing to see preview...</p>';
return;
}
try {
// Convert markdown to HTML (v15+ can be async)
let html = await marked.parse(markdown);
// Ensure html is a string, not an object
if (typeof html !== "string") {
console.error("Marked.parse returned non-string:", typeof html, html);
html = String(html);
}
// Process KaTeX math
html = this.processKaTeX(html);
// Update container
this.container.innerHTML = html;
// Render mermaid diagrams
this.renderMermaid();
// Highlight code blocks
this.highlightCode();
} catch (err) {
console.error("Preview render failed:", err);
this.container.innerHTML = `<p style="color: var(--error);">Preview error: ${err.message}</p>`;
}
},
processKaTeX(html) {
if (!window.katex) return html;
// Inline math: $...$
html = html.replace(/\$([^\$]+)\$/g, (match, math) => {
try {
return katex.renderToString(math, { throwOnError: false });
} catch {
return match;
}
});
// Block math: $$...$$
html = html.replace(/\$\$([^\$]+)\$\$/g, (match, math) => {
try {
return katex.renderToString(math, {
throwOnError: false,
displayMode: true
});
} catch {
return match;
}
});
return html;
},
renderMermaid() {
if (!window.mermaid) return;
const mermaidBlocks = this.container.querySelectorAll("code.language-mermaid");
mermaidBlocks.forEach((block, index) => {
const code = block.textContent;
const id = `mermaid-${Date.now()}-${index}`;
const div = document.createElement("div");
div.id = id;
div.className = "mermaid";
div.textContent = code;
block.parentElement.replaceWith(div);
try {
mermaid.init(undefined, `#${id}`);
} catch (err) {
console.warn("Mermaid render failed:", err);
}
});
},
highlightCode() {
if (!window.Prism) return;
this.container.querySelectorAll("pre code").forEach(block => {
if (!block.classList.contains("language-mermaid")) {
Prism.highlightElement(block);
}
});
},
// Get current HTML (for export)
getHTML() {
if (!this.container) {
console.warn("Preview container not initialized");
return "";
}
return this.container.innerHTML;
},
// Apply theme
setTheme(theme) {
if (!this.container) {
console.warn("Preview container not initialized yet");
return;
}
if (theme === "github") {
this.container.classList.add("theme-github");
this.container.classList.remove("theme-elysia");
} else {
this.container.classList.add("theme-elysia");
this.container.classList.remove("theme-github");
}
}
};
export default Preview;
|