diff --git a/lib/ai/renderer.js b/lib/ai/renderer.js index 5e121a2..a5b447d 100644 --- a/lib/ai/renderer.js +++ b/lib/ai/renderer.js @@ -58,23 +58,27 @@ class Renderer { async renderMarkdown(markdown) { if (!this.isInitialized) await this.init(); - try { const page = await this.browser.newPage(); const html = this.getMarkdownTemplate(markdown, this.config?.markdownRenderer || {}); - await page.setContent(html, { waitUntil: 'networkidle0' }); await page.waitForSelector('#render-complete', { timeout: 5000 }); - + //计算页面尺寸 const rect = await page.evaluate(() => { const body = document.body; - return { width: body.scrollWidth, height: body.scrollHeight }; - }); - await page.setViewport({ - width: Math.ceil(rect.width), - height: Math.ceil(rect.height), + const main = document.querySelector('.markdown-body') || body; + const rect = main.getBoundingClientRect(); + return { + width: Math.min(Math.ceil(rect.width + 40), 1200), + height: Math.ceil(rect.height + 40), + }; }); + await page.setViewport({ + width: rect.width, + height: Math.min(rect.height, 3000), + deviceScaleFactor: 2, + }); const tempDir = path.join(process.cwd(), 'temp', 'html'); if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, { recursive: true }); const filepath = path.join(tempDir, `markdown_${Date.now()}.png`); @@ -222,27 +226,98 @@ class Renderer { }); return ` - - -
- - - - - + + + + + + + + + +