diff --git a/assets/js/src/alpine.js b/assets/js/src/alpine.js index d023fb11d5..f9d2ec796a 100644 --- a/assets/js/src/alpine.js +++ b/assets/js/src/alpine.js @@ -35,6 +35,22 @@ hljs.registerLanguage('py', python) hljs.registerLanguage('go', go) hljs.registerLanguage('golang', go) +// Configure marked to escape HTML in text tokens only (not code blocks) +marked.use({ + walkTokens(token) { + // Escape HTML in text and HTML tokens, preserve code blocks + if (token.type === 'text' || token.type === 'html') { + const text = token.text || token.raw + const escaped = text + .replace(/&/g, '&') + .replace(//g, '>') + if (token.text) token.text = escaped + if (token.raw) token.raw = escaped + } + } +}) + // Add $markdown magic for rendering markdown with syntax highlighting Alpine.magic('markdown', () => { return (content) => {