Skip to content

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

preserve edited algorithms and remote links

myhloliba0ab9059ae0
diff --git a/mineru/model/flash/html/resources.py b/mineru/model/flash/html/resources.py
index 6222ff5d..ed24b03b 100644
--- a/mineru/model/flash/html/resources.py
+++ b/mineru/model/flash/html/resources.py
@@ -110,11 +110,17 @@ class HtmlResourceContext:
def resolve_link(self, href: str) -> str | None:
"""解析安全外链、相对链接或实际存在的文档内 fragment。"""
- if self.anchors is not None and (fragment := self.same_document_fragment(href)) is not None:
+ candidate = (href or "").strip()
+ if self._remote_base and candidate.startswith("//"):
+ try:
+ candidate = urljoin(self._remote_base, candidate)
+ except ValueError:
+ return None
+ if self.anchors is not None and (fragment := self.same_document_fragment(candidate)) is not None:
if internal := self.anchors.resolve_fragment(fragment):
return internal
normalized = sanitize_hyperlink_target(
- href,
+ candidate,
allow_relative=True,
allow_fragment=True,
allow_root_relative=True,
diff --git a/mineru/model/flash/html/wire.py b/mineru/model/flash/html/wire.py
index 8de0b68c..8421d4d1 100644
--- a/mineru/model/flash/html/wire.py
+++ b/mineru/model/flash/html/wire.py
@@ -345,11 +345,14 @@ def _validate_visual_content(content_root: etree._Element, wrapper: etree._Eleme
raise _WireValidationError("invalid_visual_body_shape")
if parent_index is not None and child_index != parent_index:
raise _WireValidationError("visual_body_index_mismatch")
- if parent_type == BlockType.CODE and (wrapper.get("data-block-sub-type") or "").strip() == BlockType.CODE:
+ sub_type = (wrapper.get("data-block-sub-type") or "").strip()
+ if parent_type == BlockType.CODE and sub_type == BlockType.CODE:
_validate_code_body_shape(child)
+ elif parent_type == BlockType.CODE and sub_type == RAW_ALGORITHM:
+ _validate_algorithm_body_shape(child)
elif parent_type == BlockType.TABLE:
_validate_table_body_shape(child)
- elif parent_type == BlockType.IMAGE and (wrapper.get("data-block-sub-type") or "").strip() == "flowchart":
+ elif parent_type == BlockType.IMAGE and sub_type == "flowchart":
_validate_flowchart_body_shape(child)
for marker in _descendant_markers(child):
if marker is child:
@@ -384,6 +387,20 @@ def _validate_code_body_shape(body: etree._Element) -> None:
raise _WireValidationError("invalid_code_body_shape")
+def _validate_algorithm_body_shape(body: etree._Element) -> None:
+ """校验 algorithm body 只含规范行内容器,避免精确物化静默丢弃编辑内容。"""
+ _validate_element_only_content(body)
+ children = _element_children(body)
+ if not children:
+ return
+ if len(children) != 1:
+ raise _WireValidationError("invalid_algorithm_body_shape")
+ algorithm = children[0]
+ if local_name(algorithm) != "div" or "mineru-algorithm" not in _class_tokens(algorithm):
+ raise _WireValidationError("invalid_algorithm_body_shape")
+ _validate_inline_content_shape(algorithm)
+
+
def _validate_table_body_shape(body: etree._Element) -> None:
"""校验表格 body 只含一种规范载荷,避免精确物化静默丢弃旁路内容。"""
_validate_element_only_content(body)
diff --git a/tests/unittest/test_flash_html.py b/tests/unittest/test_flash_html.py
index 09fd76ee..557dcba7 100644
--- a/tests/unittest/test_flash_html.py
+++ b/tests/unittest/test_flash_html.py
@@ -956,6 +956,16 @@ def test_html_remote_source_resolves_relative_links_without_fetching_images() ->
assert _image_body(middle).image_base64 is None
+def test_html_remote_source_resolves_protocol_relative_links() -> None:
+ """验证远程来源按其安全协议补全协议相对链接。"""
+ context = HtmlSourceContext(source_uri="https://example.com/news/page.html")
+ payload = b'<html><body><p><a href="//cdn.example/path">Protocol link</a></p></body></html>'
+
+ markdown = render_markdown(doc_analyze(payload, file_suffix="html", source_context=context)[0])
+
+ assert "[Protocol link](https://cdn.example/path)" in markdown
+
+
def test_html_local_image_symlink_cannot_escape_resource_root(tmp_path: Path) -> None:
"""验证本地图片 symlink resolve 到根目录外时只保留 alt 文本。"""
outside = tmp_path.parent / "outside-symlink-image.png"
@@ -1364,6 +1374,47 @@ def test_html_versioned_wire_edited_code_body_falls_back_without_loss() -> None:
assert "NEW NOTE" in markdown
+def test_html_versioned_wire_edited_algorithm_body_falls_back_without_loss() -> None:
+ """验证 algorithm body 中新增可见节点会整体回退并保留算法与编辑内容。"""
+ source = MiddleJson.model_validate(
+ {
+ "pages": [
+ {
+ "page_idx": 0,
+ "blocks": [
+ {
+ "type": "code",
+ "index": 0,
+ "sub_type": "algorithm",
+ "content": [
+ {
+ "type": "code_body",
+ "index": 0,
+ "content": "Step A <eq>x+1</eq>",
+ }
+ ],
+ }
+ ],
+ }
+ ],
+ "is_full_document": True,
+ "file_suffix": "html",
+ "effort": "flash",
+ "parse_mode": "txt",
+ "mineru_version": "test",
+ }
+ )
+ soup = BeautifulSoup(render_html(source, standalone=False), "html.parser")
+ paragraph = soup.new_tag("p")
+ paragraph.string = "NEW NOTE"
+ soup.select_one('[data-block-type="code_body"]').append(paragraph)
+
+ markdown = render_markdown(doc_analyze(str(soup).encode(), file_suffix="html")[0])
+
+ assert "Step A" in markdown and "x+1" in markdown
+ assert "NEW NOTE" in markdown
+
+
def test_html_versioned_wire_edited_table_body_falls_back_without_loss() -> None:
"""验证表格 body 中新增可见节点会整体回退并保留表格与编辑内容。"""
source = MiddleJson.model_validate(