diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index 5544a85..ca6ba30 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -7,6 +7,7 @@ import psutil from playwright.async_api import async_playwright import os import asyncio +import re from loguru import logger @@ -18,11 +19,11 @@ async def safe_close_browser(browser, timeout: float = 4.0) -> None: for page in context.pages[:]: try: await asyncio.wait_for(page.close(), timeout=1.5) - except: + except Exception: pass try: await asyncio.wait_for(context.close(), timeout=timeout) - except: + except Exception: pass try: @@ -66,26 +67,40 @@ async def safe_close_browser(browser, timeout: float = 4.0) -> None: logger.warning(f"force kill failed: {e}") +def _split_hero(html_body: str): + title_match = re.search(r'

(.*?)

', html_body, re.S | re.I) + hero_title = title_match.group(1).strip() if title_match else "聊天总结" + remain = re.sub(r'

.*?

', '', html_body, count=1, flags=re.S | re.I).strip() + + meta_match = re.match(r'^

(.*?)

', remain, re.S | re.I) + hero_meta = meta_match.group(1).strip() if meta_match else "群聊总结 / 自动生成" + if meta_match: + remain = re.sub(r'^

.*?

', '', remain, count=1, flags=re.S | re.I).strip() + + return hero_title, hero_meta, remain + + async def md_str_to_html_content(md_content): - """将 Markdown 字符串转换为更有风格的 HTML 内容。""" + """将 Markdown 字符串转换为更美观的 HTML 内容。""" html_body = markdown.markdown(md_content, extensions=['extra', 'codehilite']) + hero_title, hero_meta, remain_html = _split_hero(html_body) css = """