From 4db938df0b56a3d1db6a1e1eb80091934e2c03b5 Mon Sep 17 00:00:00 2001 From: liuwei Date: Mon, 23 Jun 2025 17:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=90=8C=E6=AD=A5=E5=86=99?= =?UTF-8?q?=E5=85=A5,=E5=B9=B6=E4=B8=94=E5=8A=A0=E4=B8=8A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=AA=8C=E8=AF=81,=E6=96=87=E4=BB=B6=E5=BB=B6?= =?UTF-8?q?=E6=97=B60.5=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/markdown_to_image.py | 41 ++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index a554570..76fe59e 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -155,14 +155,39 @@ async def md_str_to_html(md_content, output_html): """ - # 异步写入 HTML 文件 - async with aiofiles.open(output_html, 'w', encoding='utf-8') as f: - await f.write('') - await f.write('') # 确保 UTF-8 编码 - await f.write(css) - await f.write('') - await f.write(html_content) - await f.write('') + # 构建完整的 HTML 内容 + full_html = f''' + + + {css} + + + {html_content} + + ''' + + # 使用普通的文件写入,确保文件完全写入 + try: + with open(output_html, 'w', encoding='utf-8') as f: + f.write(full_html) + f.flush() # 强制刷新缓冲区 + os.fsync(f.fileno()) # 确保写入磁盘 + except Exception as e: + logger.error(f"写入HTML文件失败: {e}") + raise + + # 验证文件是否成功写入 + try: + with open(output_html, 'r', encoding='utf-8') as f: + content = f.read() + if not content: + raise ValueError("HTML文件写入后为空") + except Exception as e: + logger.error(f"验证HTML文件失败: {e}") + raise + + # 添加小延时确保文件系统同步 + await asyncio.sleep(0.5) def check_chromium_installed(path):