fix: improve markdown style application order for better compatibility

This commit is contained in:
myhloli
2026-03-06 02:21:29 +08:00
parent 4adee72f5a
commit a0260e12ab
2 changed files with 19 additions and 10 deletions

1
demo/1.md Normal file
View File

@@ -0,0 +1 @@
~~<u>**xyz**</u>~~

View File

@@ -26,7 +26,14 @@ def _apply_markdown_style(content: str, style: list) -> str:
按照字体样式列表对文本内容应用 Markdown 格式。
支持的样式bold, italic, underline, strikethrough
组合顺序:先处理 underline/strikethrough内层再处理 bold/italic外层
组合顺序(由内到外):
1. bold/italic纯 Markdown最内层兼容性最广
2. strikethrough~~,中间层,包裹纯 Markdown 符号广泛支持)
3. underlineHTML <u>,最外层,作为 HTML 容器不干扰内部 Markdown 解析)
这样可避免 `**~~<u>text</u>~~**` 在部分渲染器中因 HTML 标签打断
外层 Markdown 标记解析而导致样式失效的问题,
改为输出 `<u>~~**text**~~</u>`,兼容性更好。
Args:
content: 待格式化的文本内容
@@ -38,15 +45,7 @@ def _apply_markdown_style(content: str, style: list) -> str:
if not style or not content:
return content
# underline: markdown 无原生语法,使用 HTML <u> 标签
if 'underline' in style:
content = f'<u>{content}</u>'
# strikethrough: ~~text~~
if 'strikethrough' in style:
content = f'~~{content}~~'
# bold 和 italic 作为外层,保证输出更接近 **~~text~~** / **<u>text</u>**
# 第一层最内层bold / italic —— 纯 Markdown 符号,放最里面兼容性最好
if 'bold' in style and 'italic' in style:
content = f'***{content}***'
elif 'bold' in style:
@@ -54,6 +53,15 @@ def _apply_markdown_style(content: str, style: list) -> str:
elif 'italic' in style:
content = f'*{content}*'
# 第二层strikethrough —— ~~text~~,包裹纯 Markdown 内容,广泛支持
if 'strikethrough' in style:
content = f'~~{content}~~'
# 第三层最外层underline —— markdown 无原生语法,使用 HTML <u> 标签
# 作为外层 HTML 容器,不会干扰内部 Markdown 标记的解析
if 'underline' in style:
content = f'<u>{content}</u>'
return content