diff --git a/frontend/app.js b/frontend/app.js index a8aa2a9..4d25641 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -568,11 +568,28 @@ function noteApp() { // Escape HTML first let html = this.escapeHtml(text); - // Frontmatter (must be at start) - html = html.replace(/^(---\n[\s\S]*?\n---)/m, '$1'); + // Store code blocks and inline code with placeholders to protect from other patterns + const codePlaceholders = []; - // Code blocks (before other patterns to avoid conflicts) - html = html.replace(/(```[\s\S]*?```)/g, '$1'); + // Frontmatter (must be at start) - protect it + html = html.replace(/^(---\n[\s\S]*?\n---)/m, (match) => { + codePlaceholders.push('' + match + ''); + return `\x00CODE${codePlaceholders.length - 1}\x00`; + }); + + // Code blocks - protect them + html = html.replace(/(```[\s\S]*?```)/g, (match) => { + codePlaceholders.push('' + match + ''); + return `\x00CODE${codePlaceholders.length - 1}\x00`; + }); + + // Inline code - protect it + html = html.replace(/`([^`\n]+)`/g, (match) => { + codePlaceholders.push('' + match + ''); + return `\x00CODE${codePlaceholders.length - 1}\x00`; + }); + + // Now apply other patterns (they won't match inside protected code) // Headings html = html.replace(/^(#{1,6})\s(.*)$/gm, '$1 $2'); @@ -585,9 +602,6 @@ function noteApp() { html = html.replace(/(?*$1*'); html = html.replace(/(?_$1_'); - // Inline code - html = html.replace(/`([^`\n]+)`/g, '`$1`'); - // Wikilinks [[...]] html = html.replace(/\[\[([^\]]+)\]\]/g, '[[$1]]'); @@ -604,6 +618,9 @@ function noteApp() { // Horizontal rules html = html.replace(/^([-*_]{3,})$/gm, '$1'); + // Restore protected code blocks + html = html.replace(/\x00CODE(\d+)\x00/g, (match, index) => codePlaceholders[parseInt(index)]); + return html; },