Skip to content

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

feat: enhance list block processing to allow reference lists to skip page auxiliary blocks

myhlolid0edf83716d6
diff --git a/docs/next/middle-json/rendering.md b/docs/next/middle-json/rendering.md
index d68a959c..5bdb5228 100644
--- a/docs/next/middle-json/rendering.md
+++ b/docs/next/middle-json/rendering.md
@@ -29,18 +29,20 @@ backend 参数或旧 `Line/Span` renderer。渲染过程基于深拷贝,不修
- 隐藏 `header/footer/page_number/aside_text/page_footnote`。
- 合并页内和跨页 `text.continues_prev`。
+- 合并页内和跨页 `list.continues_prev`;ref list 可跨过页面辅助块查找前序列表。
- 只合并跨页 `table.continues_prev`。
- 页面之间不输出分割线。
`MarkdownRenderMode.FULL`:
- 展示全部顶层 block。
-- 只合并页内 `text.continues_prev`。
-- 不合并跨页 text/table。
+- 只合并页内 `text/list.continues_prev`。
+- 不合并跨页 text/list/table。
- 每两个相邻 `PageInfo` 之间输出 `---`,空白页边界同样保留。
text 合并允许跨越任意其他 block,后一个 text 的内容被吸收到前一个 text,
-后块不再在原位置输出。table 只接受跨页合并;失败时保留两张原表。
+后块不再在原位置输出。ref list 只跨过页面辅助块,其他语义块仍会阻断列表链;
+普通 list 仍要求物理相邻。table 只接受跨页合并;失败时保留两张原表。
## Block 规则
diff --git a/mineru/backend/postprocess/paragraphs.py b/mineru/backend/postprocess/paragraphs.py
index cfa20d53..735875ee 100644
--- a/mineru/backend/postprocess/paragraphs.py
+++ b/mineru/backend/postprocess/paragraphs.py
@@ -4,7 +4,7 @@
import math
from typing import Any, TypeAlias
-from ...types import BlockType
+from ...types import PAGE_AUXILIARY_BLOCK_TYPES, BlockType
LINE_STOP_FLAG = (".", "!", "?", "。", "!", "?", ")", ")", '"', "”", ":", ":", ";", ";")
SECTION_MERGE_BARRIER_TYPES = {
@@ -22,11 +22,7 @@ TEXT_MERGE_TRANSPARENT_TYPES = {
BlockType.TABLE,
BlockType.CHART,
BlockType.CODE,
- BlockType.HEADER,
- BlockType.FOOTER,
- BlockType.PAGE_NUMBER,
- BlockType.PAGE_FOOTNOTE,
- BlockType.ASIDE_TEXT,
+ *PAGE_AUXILIARY_BLOCK_TYPES,
}
VERTICAL_LINE_HEIGHT_TO_WIDTH_RATIO_THRESHOLD = 2
VERTICAL_LINE_IN_BLOCK_THRESHOLD = 0.8
@@ -169,14 +165,17 @@ def _find_previous_ref_text_list_block(
current_index: int,
current_block: BlockDict,
) -> OrderedBlock | None:
- """查找紧邻当前块的前一个 ref_text list,列表之间不允许跨过其他块。"""
- previous_index = current_index - 1
- if previous_index < 0:
+ """跳过页面辅助块查找前一个 ref_text list,其他语义块保持阻断。"""
+ if not _is_ref_text_list_block(current_block):
return None
- previous_block = ordered_blocks[previous_index][2]
- if not _is_ref_text_list_block(current_block) or not _is_ref_text_list_block(previous_block):
+ for previous_index in range(current_index - 1, -1, -1):
+ previous_block = ordered_blocks[previous_index][2]
+ if previous_block.get("type") in PAGE_AUXILIARY_BLOCK_TYPES:
+ continue
+ if _is_ref_text_list_block(previous_block):
+ return ordered_blocks[previous_index]
return None
- return ordered_blocks[previous_index]
+ return None
def _is_ref_text_list_block(block: BlockDict) -> bool:
diff --git a/mineru/render/utils/logical_blocks.py b/mineru/render/utils/logical_blocks.py
index b0bb97bb..4cf65452 100644
--- a/mineru/render/utils/logical_blocks.py
+++ b/mineru/render/utils/logical_blocks.py
@@ -7,7 +7,7 @@ from dataclasses import dataclass, field
from enum import Enum
from mineru.backend.postprocess.table_merge import merge_table_content
-from mineru.types import ListBlock, MiddleJson, PageBlock, TableBlock, TextBlock
+from mineru.types import PAGE_AUXILIARY_BLOCK_TYPES, BlockType, ListBlock, MiddleJson, PageBlock, TableBlock, TextBlock
class MarkdownRenderMode(str, Enum):
@@ -73,11 +73,11 @@ def _find_previous_planned_text(blocks: list[PlannedBlock], current_index: int)
def _merge_continued_list_blocks(blocks: list[PlannedBlock], mode: MarkdownRenderMode) -> None:
- """把续接列表吸收到紧邻且子类型一致的前序列表。"""
+ """把续接列表吸收到子类型一致的前序列表,参考文献可跨过页面辅助块。"""
for current_index, current in enumerate(blocks):
if current.removed or not isinstance(current.block, ListBlock) or current.block.continues_prev is not True:
continue
- previous = _find_adjacent_previous_planned_list(blocks, current_index)
+ previous = _find_previous_planned_list(blocks, current_index)
if previous is None or not isinstance(previous.block, ListBlock):
continue
if previous.block.sub_type != current.block.sub_type:
@@ -89,14 +89,21 @@ def _merge_continued_list_blocks(blocks: list[PlannedBlock], mode: MarkdownRende
current.removed = True
-def _find_adjacent_previous_planned_list(
+def _find_previous_planned_list(
blocks: list[PlannedBlock],
current_index: int,
) -> PlannedBlock | None:
- """查找原始连续列表链中的前序有效列表,不跨越其他类型块。"""
+ """查找前序有效列表,仅允许参考文献跳过页面辅助块。"""
+ current = blocks[current_index]
+ if not isinstance(current.block, ListBlock):
+ return None
+ can_skip_page_auxiliary = current.block.sub_type == BlockType.REF_TEXT
previous_index = current_index - 1
while previous_index >= 0:
candidate = blocks[previous_index]
+ if can_skip_page_auxiliary and candidate.block.type in PAGE_AUXILIARY_BLOCK_TYPES:
+ previous_index -= 1
+ continue
if not isinstance(candidate.block, ListBlock):
return None
if not candidate.removed:
diff --git a/mineru/types.py b/mineru/types.py
index 4027a7fb..a9e4f77f 100644
--- a/mineru/types.py
+++ b/mineru/types.py
@@ -245,13 +245,15 @@ PAGE_BLOCK_TYPES = {
BlockType.PARAGRAPH_TITLE,
}
-VISUAL_RELATION_IGNORED_TYPES = {
+# 页面装饰与辅助文本不参与正文、列表和视觉对象之间的语义边界判断。
+PAGE_AUXILIARY_BLOCK_TYPES = {
BlockType.HEADER,
BlockType.FOOTER,
BlockType.PAGE_NUMBER,
BlockType.PAGE_FOOTNOTE,
BlockType.ASIDE_TEXT,
}
+VISUAL_RELATION_IGNORED_TYPES = PAGE_AUXILIARY_BLOCK_TYPES
VISUAL_MAIN_TYPES = {
BlockType.IMAGE_BODY: BlockType.IMAGE,
BlockType.TABLE_BODY: BlockType.TABLE,
diff --git a/tests/unittest/test_markdown_render.py b/tests/unittest/test_markdown_render.py
index ce7b77c0..dfe115b0 100644
--- a/tests/unittest/test_markdown_render.py
+++ b/tests/unittest/test_markdown_render.py
@@ -197,8 +197,50 @@ def test_list_continuation_merges_cross_page_chain_only_in_default_mode() -> Non
)
-def test_list_continuation_requires_adjacent_matching_subtype() -> None:
- """验证列表续接不跨越其他块,且子类型不一致时保持独立输出。"""
+def test_ref_list_continuation_skips_page_auxiliary_blocks_without_mutating_input() -> None:
+ """验证默认模式跨页面辅助块合并参考文献,完整模式仍保留原始页界和顺序。"""
+ middle = _middle(
+ _page(
+ 0,
+ _list(0, "[1] first", sub_type="ref_text"),
+ PageAuxTextBlock(type="page_footnote", index=1, content="NOTE"),
+ ),
+ _page(
+ 1,
+ PageAuxTextBlock(type="header", index=0, content="HEADER"),
+ PageAuxTextBlock(type="page_number", index=1, content="2"),
+ _list(2, "[2] second", sub_type="ref_text", continues_prev=True),
+ ),
+ )
+ original = deepcopy(middle)
+
+ assert render_markdown(middle) == "[1] first\n[2] second"
+ assert render_markdown(middle, mode=MarkdownRenderMode.FULL) == (
+ "[1] first\n\nNOTE\n\n---\n\nHEADER\n\n2\n\n[2] second"
+ )
+ assert middle == original
+
+
+def test_ordinary_list_continuation_does_not_skip_page_auxiliary_blocks() -> None:
+ """验证页面辅助块透明规则只作用于参考文献,普通列表仍要求物理相邻。"""
+ middle = _middle(
+ _page(
+ 0,
+ _list(0, "- first"),
+ PageAuxTextBlock(type="page_footnote", index=1, content="NOTE"),
+ ),
+ _page(
+ 1,
+ PageAuxTextBlock(type="header", index=0, content="HEADER"),
+ _list(1, "- second", continues_prev=True),
+ ),
+ )
+
+ assert render_markdown(middle) == "- first\n\n- second"
+
+
+def test_list_continuation_keeps_semantic_barrier_and_matching_subtype() -> None:
+ """验证语义块仍会阻断列表续接,且子类型不一致时保持独立输出。"""
non_adjacent = _middle(
_page(
0,
diff --git a/tests/unittest/test_postprocess_paragraphs.py b/tests/unittest/test_postprocess_paragraphs.py
index 07409a15..8347e02d 100644
--- a/tests/unittest/test_postprocess_paragraphs.py
+++ b/tests/unittest/test_postprocess_paragraphs.py
@@ -24,6 +24,29 @@ def _text_block(
}
+def _list_block(
+ index: int,
+ content: str,
+ *,
+ sub_type: str = BlockType.REF_TEXT,
+) -> dict:
+ """构造带直属文本子项和临时行框的 dict list block。"""
+ child_type = BlockType.REF_TEXT if sub_type == BlockType.REF_TEXT else BlockType.TEXT
+ return {
+ "index": index,
+ "type": BlockType.LIST,
+ "sub_type": sub_type,
+ "bbox": [0.1, 0.1, 0.9, 0.3],
+ "content": [
+ {
+ "type": child_type,
+ "content": content,
+ "lines": [{"bbox": [0.1, 0.1, 0.9, 0.2]}],
+ }
+ ],
+ }
+
+
def _horizontal_pair() -> tuple[dict, dict]:
"""构造一组满足横排段落延续规则的前后 text block。"""
previous_block = _text_block(
@@ -707,6 +730,86 @@ def test_merge_para_text_blocks_marks_adjacent_ref_lists_without_moving_items()
merge_para_text_blocks(gap_pages)
assert "continues_prev" not in current_list
+ previous_list = _list_block(0, "ref one")
+ current_list = _list_block(0, "ordinary item", sub_type=BlockType.TEXT)
+ merge_para_text_blocks(
+ [
+ {"page_idx": 4, "blocks": [previous_list]},
+ {"page_idx": 5, "blocks": [current_list]},
+ ]
+ )
+ assert "continues_prev" not in current_list
+
+
+def test_merge_para_text_blocks_skips_page_auxiliary_blocks_between_ref_lists() -> None:
+ """验证跨页参考文献会跳过前后页的全部页面辅助块建立延续关系。"""
+ previous_list = _list_block(0, "ref one")
+ current_list = _list_block(3, "ref two")
+ pages = [
+ {
+ "page_idx": 8,
+ "blocks": [
+ previous_list,
+ {"index": 1, "type": BlockType.FOOTER, "content": "footer"},
+ {"index": 2, "type": BlockType.PAGE_FOOTNOTE, "content": "page footnote"},
+ ],
+ },
+ {
+ "page_idx": 9,
+ "blocks": [
+ {"index": 0, "type": BlockType.HEADER, "content": "header"},
+ {"index": 1, "type": BlockType.PAGE_NUMBER, "content": "9"},
+ {"index": 2, "type": BlockType.ASIDE_TEXT, "content": "aside"},
+ current_list,
+ ],
+ },
+ ]
+
+ merge_para_text_blocks(pages)
+
+ assert "continues_prev" not in previous_list
+ assert current_list["continues_prev"] is True
+ assert previous_list["content"][0]["content"] == "ref one"
+ assert current_list["content"][0]["content"] == "ref two"
+ assert "lines" not in previous_list["content"][0]
+ assert "lines" not in current_list["content"][0]
+
+
+ "barrier_type",
+ [BlockType.TEXT, BlockType.PARAGRAPH_TITLE, BlockType.IMAGE, BlockType.TABLE, BlockType.LIST],
+)
+def test_merge_para_text_blocks_keeps_semantic_barriers_between_ref_lists(barrier_type: str) -> None:
+ """验证页面辅助块透明后,其他语义块仍会阻断参考文献列表延续。"""
+ previous_list = _list_block(0, "ref one")
+ current_list = _list_block(2, "ref two")
+ barrier = {
+ "index": 1,
+ "type": barrier_type,
+ "content": [] if barrier_type == BlockType.LIST else "barrier",
+ }
+ pages = [
+ {
+ "page_idx": 8,
+ "blocks": [
+ previous_list,
+ {"index": 1, "type": BlockType.PAGE_FOOTNOTE, "content": "page footnote"},
+ ],
+ },
+ {
+ "page_idx": 9,
+ "blocks": [
+ {"index": 0, "type": BlockType.HEADER, "content": "header"},
+ barrier,
+ current_list,
+ ],
+ },
+ ]
+
+ merge_para_text_blocks(pages)
+
+ assert "continues_prev" not in current_list
+
def test_merge_para_text_blocks_cleans_nested_lines_and_invalid_stale_markers() -> None:
"""验证非法 text 不合并,同时递归清理临时行框和过期标记。"""