Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: improve markdown style application order for better compatibility

myhlolia0260e12ab44
diff --git a/demo/1.md b/demo/1.md
new file mode 100644
index 00000000..fbc1284f
--- /dev/null
+++ b/demo/1.md
@@ -0,0 +1 @@
+ ~~<u>**xyz**</u>~~
\ No newline at end of file
diff --git a/mineru/backend/office/office_middle_json_mkcontent.py b/mineru/backend/office/office_middle_json_mkcontent.py
index 0ed4e227..44c15904 100644
--- a/mineru/backend/office/office_middle_json_mkcontent.py
+++ b/mineru/backend/office/office_middle_json_mkcontent.py
@@ -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. underline(HTML <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