Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: enhance reference list rendering to conditionally add unordered markers based on numeric prefix majority
myhloli4b8f48df5f70
diff --git a/docs/next/middle-json/rendering.md b/docs/next/middle-json/rendering.mdindex 285e1fd6..d68a959c 100644--- a/docs/next/middle-json/rendering.md+++ b/docs/next/middle-json/rendering.md@@ -46,7 +46,9 @@ text 合并允许跨越任意其他 block,后一个 text 的内容被吸收到- `text/ref_text`: 解析行内公式、样式和超链接;普通 text 转义可能误触发的 Markdown block 前缀。- `doc_title/paragraph_title`: 使用全局 `level`,Markdown 标题最多六级;anchor 输出为 HTML id。-- `list`: 递归缩进,直接使用 content 已内化的有序或无序前缀。+- `list`: 递归缩进;普通列表直接使用 content 已内化的前缀。`sub_type=ref_text`+ 时统计每个直属非空 item 的前五个可见字符,数字前缀未达到严格多数则给全部 item+ 补 `- `,已有 `- ` 不重复;嵌套列表独立判定。- `index`: 递归输出 GFM 列表;标题叶子使用 anchor 生成内部链接,不当作 heading 输出。- `equation`: content 非空时输出行间 LaTeX,空 content 回退图片。- `image/chart`: 图片优先,识别内容放入折叠 details;chart 的简单 HTML 表格转为 GFM,复杂表格保留 HTML,无图片时直接输出转换后的内容。diff --git a/mineru/render/markdown.py b/mineru/render/markdown.pyindex d776e715..542732c4 100644--- a/mineru/render/markdown.py+++ b/mineru/render/markdown.py@@ -58,6 +58,9 @@ _ALGORITHM_TOKEN_RE = re.compile()_VALID_CODE_LANGUAGE_RE = re.compile(r"[A-Za-z0-9_.+#-]+")_INDEX_ROMAN_RE = re.compile(r"[ivxlcdm]+", re.IGNORECASE)+# 去除首部空白后,前五个可见字符内出现 Unicode 数字即视为单项命中。+_REF_ITEM_NUMBER_PREFIX_RE = re.compile(r"^\D{0,4}\d")+_UNORDERED_LIST_ITEM_RE = re.compile(r"^[ \t]*-[ \t]+")def render_markdown(@@ -170,7 +173,8 @@ def _render_equation(def _render_list(block: ListBlock, delimiters: LatexDelimitersConfig, depth: int = 0) -> str:- """递归渲染已在 content 中内化编号的列表。"""+ """递归渲染列表,并给多数条目无数字前缀的参考文献补无序标记。"""+ add_ref_bullets = _should_add_reference_list_bullets(block)lines: list[str] = []for child in block.content:if isinstance(child, ListBlock):@@ -183,10 +187,34 @@ def _render_list(block: ListBlock, delimiters: LatexDelimitersConfig, depth: intcontinueitem = escape_standalone_marker_rule(item)indent = " " * depth- lines.extend(f"{indent}{line}" for line in item.splitlines() or [item])+ item_lines = item.splitlines() or [item]+ if add_ref_bullets and not _UNORDERED_LIST_ITEM_RE.match(item):+ lines.append(f"{indent}- {item_lines[0]}")+ lines.extend(f"{indent} {line}" for line in item_lines[1:])+ else:+ lines.extend(f"{indent}{line}" for line in item_lines)return "\n".join(lines)+def _should_add_reference_list_bullets(block: ListBlock) -> bool:+ """统计直属可见条目的数字前缀,未达到严格多数时给参考文献补项目符号。"""+ if block.sub_type != BlockType.REF_TEXT:+ return False++ item_count = 0+ numbered_count = 0+ for child in block.content:+ if isinstance(child, ListBlock):+ continue+ visible_text = inline_plain_text(parse_inline_content(child.content)).lstrip()+ if not visible_text:+ continue+ item_count += 1+ if _REF_ITEM_NUMBER_PREFIX_RE.match(visible_text):+ numbered_count += 1+ return item_count > 0 and numbered_count * 2 <= item_count++def _render_index(block: IndexBlock, delimiters: LatexDelimitersConfig, depth: int = 0) -> str:"""递归渲染目录列表,并给标题叶子添加内部 anchor 链接。"""lines: list[str] = []diff --git a/tests/unittest/test_markdown_render.py b/tests/unittest/test_markdown_render.pyindex f46217de..ce7b77c0 100644--- a/tests/unittest/test_markdown_render.py+++ b/tests/unittest/test_markdown_render.py@@ -221,6 +221,83 @@ def test_list_continuation_requires_adjacent_matching_subtype() -> None:assert render_markdown(mismatched) == "[1] first\n\n- second"+ ("item", "expected"),+ [+ ("[1] bracket", "[1] bracket"),+ ("1. dot", "1. dot"),+ ("(1) parenthesized", "(1) parenthesized"),+ ("1) closing parenthesis", "1) closing parenthesis"),+ ("1、cjk delimiter", "1、cjk delimiter"),+ ("[1]full width", "[1]full width"),+ ('<text style="bold">[1]</text> styled', "**[1]** styled"),+ ],+)+def test_reference_list_keeps_supported_numeric_prefix_styles(item: str, expected: str) -> None:+ """验证数字出现在前五个可见字符内时,单条参考文献保留原有编号。"""+ middle = _middle(_page(0, _list(0, item, sub_type="ref_text")), file_suffix="pdf")++ assert render_markdown(middle) == expected+++def test_reference_list_uses_strict_numeric_prefix_majority() -> None:+ """验证参考文献按全部直属非空条目的严格多数决定是否补无序标记。"""+ numbered_majority = _middle(+ _page(0, _list(0, "[1] first", "missing marker", "3) third", sub_type="ref_text")),+ file_suffix="pdf",+ )+ unordered_majority = _middle(+ _page(0, _list(0, "1470–1480 continuation", "Author A", "Author B", sub_type="ref_text")),+ file_suffix="pdf",+ )+ tied = _middle(+ _page(0, _list(0, "[1] first", "Author A", sub_type="ref_text")),+ file_suffix="pdf",+ )++ assert render_markdown(numbered_majority) == "[1] first\nmissing marker\n3) third"+ expected_unordered = "- 1470–1480 continuation\n- Author A\n- Author B"+ assert render_markdown(unordered_majority) == expected_unordered+ assert render_markdown(unordered_majority, mode=MarkdownRenderMode.FULL) == expected_unordered+ assert render_markdown(tied) == "- [1] first\n- Author A"+++def test_reference_list_bullets_mixed_children_without_duplication() -> None:+ """验证混合直属类型共同参与统计,已有短横线不重复且多行正文正确缩进。"""+ block = ListBlock(+ type="list",+ index=0,+ bbox=(0.1, 0.1, 0.9, 0.3),+ sub_type="ref_text",+ content=[+ TextBlock(type="text", content="- existing"),+ RefTextBlock(type="ref_text", content="Author A\ncontinued"),+ ],+ )++ assert render_markdown(_middle(_page(0, block), file_suffix="pdf")) == (+ "- existing\n- Author A\n continued"+ )+++def test_nested_reference_list_decides_bullets_independently() -> None:+ """验证嵌套参考文献依据自身子类型判定,外层普通列表行为保持不变。"""+ nested = ListBlock(+ type="list",+ sub_type="ref_text",+ content=[RefTextBlock(type="ref_text", content="Author A")],+ )+ outer = ListBlock(+ type="list",+ index=0,+ bbox=(0.1, 0.1, 0.9, 0.3),+ sub_type="text",+ content=[TextBlock(type="text", content="- outer"), nested],+ )++ assert render_markdown(_middle(_page(0, outer), file_suffix="pdf")) == "- outer\n - Author A"++def test_text_continuation_joins_url_candidates_but_separates_independent_urls() -> None:"""验证 Markdown 续写连接跨块 URL,同时保留两条独立 URL 的分隔。"""continued_url = _middle(