Skip to content

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

fix: enhance RTF annotation handling and preserve metadata integrity

myhlolic83d5d05a474
diff --git a/CLAUDE.md b/CLAUDE.md
index d9ca645b..9d1ffbae 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -85,7 +85,7 @@ MinerU 文档解析的目标是尽可能保留具有跨格式价值的内容和
- Markdown 是主要兼容基线,但不是纯文本上限;Markdown 原生能力不足时,可以复用现有安全 HTML、图片资源和结构化 block。
- 不为单一输入格式增加只用于还原罕见排版、编号样式或物理分页的私有 block、私有字段或 renderer 特判。
- 只有同时满足“常见、语义价值高、可进入统一 Middle JSON、可被多个 renderer 稳定消费”的格式私有信息,才应扩展公共契约。
-- Review 时,源格式细节未被 100% 还原不应自动视为缺陷。只有丢失统一契约支持的有价值内容、破坏结构、产生错误语义,或引入安全和资源问题时才应报告。
+- Review 时,源格式细节未被 100% 还原不应自动视为缺陷。只有丢失统一契约支持的有价值内容、破坏结构、产生错误语义时才应报告。
- 禁止通过不断增加格式例外来追求源文件视觉级复刻;优先规范化为确定、可静态分析、跨格式一致的表示。
具体投影约束:
diff --git a/mineru/model/flash/office/rtf/models.py b/mineru/model/flash/office/rtf/models.py
index 2ae4c0eb..ffe85c88 100644
--- a/mineru/model/flash/office/rtf/models.py
+++ b/mineru/model/flash/office/rtf/models.py
@@ -136,10 +136,10 @@ RtfBlock: TypeAlias = Union[RtfParagraph, RtfDisplayEquation, RtfTable]
@dataclass(slots=True)
class RtfNote:
- """保存脚注或尾注正文。"""
+ """保存脚注、尾注或批注正文。"""
id: str
- kind: Literal["footnote", "endnote"]
+ kind: Literal["footnote", "endnote", "annotation"]
blocks: list[RtfBlock] = field(default_factory=list)
diff --git a/mineru/model/flash/office/rtf/parser.py b/mineru/model/flash/office/rtf/parser.py
index a066f99f..9e4d5b81 100644
--- a/mineru/model/flash/office/rtf/parser.py
+++ b/mineru/model/flash/office/rtf/parser.py
@@ -95,7 +95,6 @@ _TITLE_STYLE_NAMES = {"document title", "title"}
_HEADER_DESTINATIONS = {"header", "headerf", "headerl", "headerr"}
_FOOTER_DESTINATIONS = {"footer", "footerf", "footerl", "footerr"}
_SUPPRESSED_DESTINATIONS = {
- "annotation",
"atnauthor",
"atnid",
"background",
@@ -392,7 +391,7 @@ class _GroupFrame:
capture_text: list[str] = field(default_factory=list)
instruction: list[str] = field(default_factory=list)
pict: _PictCapture | None = None
- note_kind: Literal["footnote", "endnote"] = "footnote"
+ note_kind: Literal["footnote", "endnote", "annotation"] = "footnote"
upr_child_count: int = 0
@@ -933,12 +932,14 @@ class RtfParser:
frame.destination = "fldrslt"
self.state.destination = "body"
return True
- if name in {"footnote", "endnote"}:
+ if name in {"footnote", "endnote", "annotation"}:
frame.destination = "note"
- frame.note_kind = "endnote" if name == "endnote" else "footnote"
+ frame.note_kind = "endnote" if name == "endnote" else "annotation" if name == "annotation" else "footnote"
frame.parent_context = self.context
self.context = _OutputContext()
self.state.destination = "body"
+ if name == "annotation":
+ self.state.hidden = False
return True
if name in _HEADER_DESTINATIONS | _FOOTER_DESTINATIONS:
frame.destination = "header" if name in _HEADER_DESTINATIONS else "footer"
@@ -1028,7 +1029,7 @@ class RtfParser:
self.context.inlines[index] = replace(inline, hyperlink=target)
def _finish_note(self, frame: _GroupFrame) -> None:
- """结束隔离 note context,在父正文插入引用并登记正文。"""
+ """结束隔离 note context,登记正文并按类型决定是否插入引用。"""
note_context = self.context
self._finalize_context(note_context)
parent = frame.parent_context or _OutputContext()
@@ -1037,7 +1038,8 @@ class RtfParser:
return
note_id = f"rtf{len(self.document.notes)}"
self.document.notes.append(RtfNote(note_id, frame.note_kind, note_context.blocks))
- self._append_inline(RtfNoteReference(note_id))
+ if frame.note_kind != "annotation":
+ self._append_inline(RtfNoteReference(note_id))
def _finish_auxiliary(self, frame: _GroupFrame, destination: str) -> None:
"""结束页眉页脚隔离 context,并恢复父正文。"""
diff --git a/tests/unittest/test_flash_rtf.py b/tests/unittest/test_flash_rtf.py
index 7ee778ed..95e53ff8 100644
--- a/tests/unittest/test_flash_rtf.py
+++ b/tests/unittest/test_flash_rtf.py
@@ -490,6 +490,34 @@ def test_rtf_footnote_literal_protocol_is_escaped_before_rendering() -> None:
assert "javascript:alert(1)" not in relationships
+def test_rtf_annotation_preserves_comment_as_page_footnote() -> None:
+ """验证 annotation 正文进入页脚注块且不会生成虚假的正文脚注引用。"""
+ source = rb"{\rtf1\ansi Before {\*\annotation Review comment} After\par}"
+
+ pages = RtfModel().predict(BytesIO(source))
+
+ assert pages[0][0]["type"] == BlockType.TEXT
+ assert pages[0][0]["content"].split() == ["Before", "After"]
+ assert "[1]" not in pages[0][0]["content"]
+ assert pages[0][1] == {"type": BlockType.PAGE_FOOTNOTE, "content": "[1] Review comment"}
+
+
+def test_rtf_hidden_annotation_preserves_body_and_suppresses_metadata() -> None:
+ """验证隐藏批注可恢复正文,同时作者和标识元数据不会泄漏。"""
+ source = (
+ rb"{\rtf1\ansi Body {\v {\*\atnauthor Alice}{\*\atnid AB}\chatn"
+ rb"{\*\annotation Review <eq>x</eq> comment}} end\par}"
+ )
+
+ pages = RtfModel().predict(BytesIO(source))
+
+ footnote = next(block for block in pages[0] if block.get("type") == BlockType.PAGE_FOOTNOTE)
+ assert "Review &lt;eq&gt;x&lt;/eq&gt; comment" in footnote["content"]
+ assert "Alice" not in footnote["content"]
+ assert "AB" not in footnote["content"]
+ assert "<eq>" not in footnote["content"]
+
+
def test_rtf_equation_only_paragraph_preserves_note_reference() -> None:
"""验证纯公式段落仍保留脚注引用,不生成孤立脚注正文。"""
source = rb"{\rtf1\ansi\pard {\mmath{\*\moMath{\mr x}}}{\footnote Foot.}\par}"