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 });">