From 92cb9ae8a27a6d7cd2d431b17e99c226f3739df9 Mon Sep 17 00:00:00 2001 From: baiyu-yu <135424680+baiyu-yu@users.noreply.github.com> Date: Sat, 29 Nov 2025 13:44:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=9D=E8=AF=95=E4=BF=AE=E5=A4=8Dhtml?= =?UTF-8?q?=E5=92=8Cmd=E5=90=8E=E7=AB=AF=E5=85=AC=E5=BC=8F=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../render.js" | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git "a/\347\233\270\345\205\263\345\220\216\347\253\257\351\241\271\347\233\256/md\345\222\214html\345\233\276\347\211\207\346\270\262\346\237\223/render.js" "b/\347\233\270\345\205\263\345\220\216\347\253\257\351\241\271\347\233\256/md\345\222\214html\345\233\276\347\211\207\346\270\262\346\237\223/render.js" index 9eff290..3c7007b 100644 --- "a/\347\233\270\345\205\263\345\220\216\347\253\257\351\241\271\347\233\256/md\345\222\214html\345\233\276\347\211\207\346\270\262\346\237\223/render.js" +++ "b/\347\233\270\345\205\263\345\220\216\347\253\257\351\241\271\347\233\256/md\345\222\214html\345\233\276\347\211\207\346\270\262\346\237\223/render.js" @@ -42,21 +42,41 @@ app.use('/images', express.static('generated_images')); const IMAGE_DIR = path.join(__dirname, 'generated_images'); -async function ensureImageDir() { - try { - await fs.access(IMAGE_DIR); - } catch { - await fs.mkdir(IMAGE_DIR, { recursive: true }); - } -} - function generateImageId() { return crypto.randomBytes(16).toString('hex'); } // HTML模板 function generateHTML(content, contentType, theme = 'light', style = 'github') { - const bodyContent = contentType === 'markdown' ? marked(content) : content; +let bodyContent = content; + + if (contentType === 'markdown') { + const mathBlocks = []; + + bodyContent = bodyContent.replace(/\$\$([\s\S]+?)\$\$/g, (match) => { + const id = mathBlocks.length; + mathBlocks.push(match); + return `%%%MATH_BLOCK_${id}%%%`; + }); + + bodyContent = bodyContent.replace(/\$([^\$\n]+?)\$/g, (match) => { + const id = mathBlocks.length; + mathBlocks.push(match); + return `%%%MATH_BLOCK_${id}%%%`; + }); + + bodyContent = bodyContent.replace(/\\\[([\s\S]+?)\\\]/g, (match) => { + const id = mathBlocks.length; + mathBlocks.push(match); + return `%%%MATH_BLOCK_${id}%%%`; + }); + + bodyContent = marked(bodyContent); + + bodyContent = bodyContent.replace(/%%%MATH_BLOCK_(\d+)%%%/g, (match, id) => { + return mathBlocks[parseInt(id)]; + }); + } const themes = { light: { @@ -100,7 +120,9 @@ function generateHTML(content, contentType, theme = 'light', style = 'github') { {left: '\\\\[', right: '\\\\]', display: true}, {left: '\\\\(', right: '\\\\)', display: false} ], - throwOnError: false + ignoredTags: ['script', 'noscript', 'style', 'textarea'], + trust: true, + throwOnError: false });">