Skip to content

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

fix: preserve HTML visual annotations and remote images

myhloli455fd11860c6
diff --git a/mineru/doclib/server.py b/mineru/doclib/server.py
index 021e610d..419da259 100644
--- a/mineru/doclib/server.py
+++ b/mineru/doclib/server.py
@@ -25,6 +25,7 @@ from ..config import config
from ..errors import InvalidRequestError, MineruError, NotFoundError, error_response, http_status_for
from ..filetypes import IMAGE_EXTENSIONS, TEXT_EXTENSIONS, TEXT_FILE_TYPES
from ..parser.tier import TierDependencyError, ensure_tier_runtime_dependencies
+from ..render._internal.markdown.assets import build_markdown_image
from ..render._internal.markdown.blocks import render_single_block
from ..types import (
DEPLOYMENT_TIERS,
@@ -1830,9 +1831,11 @@ def _make_doclib_image_renderer(
def _render(block: BlockBase) -> str:
label = _VISUAL_BLOCK_LABELS.get(block.type, "Image block")
source = _resolve_block_image_source(block, image_dir=image_dir)
- block_no = block.index + 1 if block.index is not None else 0 # TODO: change after block.index's type changed.
- locator = block_ref(short_id, tier, page_no, block_no) if source is not None else ""
- return f"![{label}]({locator})"
+ if source is not None:
+ block_no = block.index + 1 if block.index is not None else 0 # TODO: change after block.index's type changed.
+ return build_markdown_image(block_ref(short_id, tier, page_no, block_no), label)
+ remote_url = next((payload.image_url for payload in _iter_block_image_payloads(block) if payload.image_url), None)
+ return build_markdown_image(remote_url, label) if remote_url is not None else f"![{label}]()"
return _render
diff --git a/mineru/model/flash/_shared/markup/projector.py b/mineru/model/flash/_shared/markup/projector.py
index aff51d5e..06dcfefc 100644
--- a/mineru/model/flash/_shared/markup/projector.py
+++ b/mineru/model/flash/_shared/markup/projector.py
@@ -576,25 +576,18 @@ class MarkupProjector:
resolved = self.stylesheet.resolve(annotation, style, visibility_hidden)
if resolved.subtree_hidden:
continue
- content, extras = self._render_inline_children(annotation, resolved.text, resolved.visibility_hidden)
target = self._figure_annotation_target(annotation, element, visual_blocks_by_child)
- annotation_blocks = list(extras)
- if not content.strip():
- if target is not None:
- annotations_by_visual.setdefault(id(target), []).extend(annotation_blocks)
- else:
- unbound_annotations.extend(annotation_blocks)
- continue
visual_type = _raw_visual_type(target.get("type")) if target is not None else None
- if visual_type is not None:
- annotation_blocks.append(
- {
- "type": VISUAL_TYPE_MAPPING[visual_type][kind],
- "content": content.strip(),
- }
- )
- else:
- annotation_blocks.append({"type": BlockType.TEXT, "content": content.strip()})
+ annotation_type = VISUAL_TYPE_MAPPING[visual_type][kind] if visual_type is not None else BlockType.TEXT
+ annotation_blocks: list[dict[str, object]] = []
+ for segment in self._render_inline_children_ordered(annotation, resolved.text, resolved.visibility_hidden):
+ if isinstance(segment, str):
+ if content := segment.strip():
+ annotation_blocks.append({"type": annotation_type, "content": content})
+ continue
+ if visual_type is not None and segment.get("type") == BlockType.TEXT:
+ segment = {**segment, "type": annotation_type}
+ annotation_blocks.append(segment)
if target is not None and visual_type is not None:
annotations_by_visual.setdefault(id(target), []).extend(annotation_blocks)
else:
diff --git a/tests/unittest/test_doclib_progressive_reading.py b/tests/unittest/test_doclib_progressive_reading.py
index fee4891c..538633c9 100644
--- a/tests/unittest/test_doclib_progressive_reading.py
+++ b/tests/unittest/test_doclib_progressive_reading.py
@@ -8,13 +8,12 @@ from mineru.doclib.server import (
)
from mineru.doclib.types import ContentRange
from mineru.errors import InvalidRequestError
-from mineru.types import BlockType, PageInfo, TableBlock, TableBodyBlock, TextBlock
+from mineru.types import BlockType, ImageBlock, ImageBodyBlock, PageInfo, TableBlock, TableBodyBlock, TextBlock
def _page(page_idx: int, *texts: str) -> PageInfo:
blocks = [
- TextBlock(type=BlockType.TEXT, index=index, bbox=(0.0, 0.0, 0.1, 0.1), content=text)
- for index, text in enumerate(texts)
+ TextBlock(type=BlockType.TEXT, index=index, bbox=(0.0, 0.0, 0.1, 0.1), content=text) for index, text in enumerate(texts)
]
return PageInfo(page_idx=page_idx, blocks=blocks)
@@ -235,3 +234,26 @@ def test_page_markdown_blocks_prefers_markdown_table_in_doclib() -> None:
),
)
]
+
+
+def test_page_markdown_blocks_preserves_remote_only_image() -> None:
+ """验证 Doclib Markdown 在没有 bbox/sidecar 时仍输出安全编码的远程图片 URL。"""
+ page = PageInfo(
+ page_idx=0,
+ blocks=[
+ ImageBlock(
+ type=BlockType.IMAGE,
+ index=0,
+ content=[
+ ImageBodyBlock(
+ type=BlockType.IMAGE_BODY,
+ index=0,
+ content="",
+ image_url="https://cdn.example.com/remote image(1).png",
+ )
+ ],
+ )
+ ],
+ )
+
+ assert _page_markdown_blocks(page) == [(0, "![Image block](https://cdn.example.com/remote%20image%281%29.png)")]
diff --git a/tests/unittest/test_flash_html.py b/tests/unittest/test_flash_html.py
index fcb8454b..44d62ac2 100644
--- a/tests/unittest/test_flash_html.py
+++ b/tests/unittest/test_flash_html.py
@@ -616,6 +616,60 @@ def test_html_interleaved_figure_captions_bind_to_each_nearest_image() -> None:
assert [child.content for child in images[1].content if child.type == BlockType.IMAGE_CAPTION] == ["Caption B"]
+ ("visual_markup", "parent_type", "body_type", "annotation_markup", "annotation_type"),
+ [
+ (
+ '<img src="https://example.com/a.png">',
+ BlockType.IMAGE,
+ BlockType.IMAGE_BODY,
+ "<figcaption><p>A</p><p>B</p></figcaption>",
+ BlockType.IMAGE_CAPTION,
+ ),
+ (
+ '<img src="https://example.com/a.png">',
+ BlockType.IMAGE,
+ BlockType.IMAGE_BODY,
+ '<div class="footnote"><p>A</p><p>B</p></div>',
+ BlockType.IMAGE_FOOTNOTE,
+ ),
+ (
+ "<table><tr><td>X</td></tr></table>",
+ BlockType.TABLE,
+ BlockType.TABLE_BODY,
+ "<figcaption><p>A</p><p>B</p></figcaption>",
+ BlockType.TABLE_CAPTION,
+ ),
+ (
+ "<table><tr><td>X</td></tr></table>",
+ BlockType.TABLE,
+ BlockType.TABLE_BODY,
+ '<div class="footnote"><p>A</p><p>B</p></div>',
+ BlockType.TABLE_FOOTNOTE,
+ ),
+ ],
+)
+def test_html_figure_block_children_keep_visual_annotation_relationship(
+ visual_markup: str,
+ parent_type: BlockType,
+ body_type: BlockType,
+ annotation_markup: str,
+ annotation_type: BlockType,
+) -> None:
+ """验证 figure 中的块级说明文本仍按目标 visual 类型保留 caption/footnote 关系。"""
+ payload = f"<html><body><figure>{visual_markup}{annotation_markup}</figure></body></html>".encode()
+
+ middle, model = doc_analyze(payload, file_suffix="html")
+
+ assert [block["type"] for block in model.pages[0]] == [parent_type, annotation_type, annotation_type]
+ assert [block.get("content") for block in model.pages[0][1:]] == ["A", "B"]
+ assert len(middle.pages[0].blocks) == 1
+ visual = middle.pages[0].blocks[0]
+ assert visual.type == parent_type
+ assert [child.type for child in visual.content] == [body_type, annotation_type, annotation_type] # type: ignore[union-attr]
+ assert [child.content for child in visual.content[1:]] == ["A", "B"] # type: ignore[union-attr]
+
+
def test_html_auto_selected_contextual_div_keeps_figure_caption_relation() -> None:
"""验证 auto 直接选中 visual wrapper div 时仍执行根节点 caption 关联。"""
payload = b"""<html><body><div><figure><img src="https://example.com/a.png"></figure>