diff --git a/docling/document_converter.py b/docling/document_converter.py index b901fab723..35bc966320 100644 --- a/docling/document_converter.py +++ b/docling/document_converter.py @@ -196,6 +196,21 @@ class MarkdownFormatOption(FormatOption): backend: Type[AbstractDocumentBackend] = MarkdownDocumentBackend backend_options: Optional[MarkdownBackendOptions] = None + def backend_options_for_input( + self, source: Path | str | DocumentStream + ) -> MarkdownBackendOptions | None: + options = self.backend_options + if ( + options is None + or options.source_uri is not None + or isinstance(source, DocumentStream) + ): + return options + + return MarkdownBackendOptions.model_validate( + {**options.model_dump(), "source_uri": source} + ) + class AsciiDocFormatOption(FormatOption): pipeline_cls: Type = SimplePipeline diff --git a/tests/test_backend_markdown.py b/tests/test_backend_markdown.py index 745880ef94..63aea67961 100644 --- a/tests/test_backend_markdown.py +++ b/tests/test_backend_markdown.py @@ -17,7 +17,7 @@ DoclingDocument, InputDocument, ) -from docling.document_converter import DocumentConverter +from docling.document_converter import DocumentConverter, MarkdownFormatOption from tests.verify_utils import CONFID_PREC, COORD_PREC from .test_data_gen_flag import GEN_TEST_DATA @@ -211,6 +211,37 @@ def test_convert_embedded_base64_image_enforces_size_limit(): assert pictures[0].image is None +@pytest.mark.parametrize("as_path", [False, True], ids=["str", "Path"]) +def test_local_relative_image_resolves_from_document_dir(tmp_path: Path, as_path: bool): + """A local Markdown source resolves relative images from its own directory.""" + image_path = tmp_path / "figure.png" + Image.new("RGB", (7, 5), color=(0, 255, 0)).save(image_path) + md_path = tmp_path / "doc.md" + md_path.write_text("# Title\n\n![alt](figure.png)\n", encoding="utf-8") + + converter = DocumentConverter( + allowed_formats=[InputFormat.MD], + format_options={ + InputFormat.MD: MarkdownFormatOption( + backend_options=MarkdownBackendOptions( + fetch_images=True, enable_local_fetch=True + ) + ) + }, + ) + + doc = converter.convert(md_path if as_path else str(md_path)).document + + pictures = [ + item for item, _ in doc.iterate_items() if isinstance(item, PictureItem) + ] + assert len(pictures) == 1 + image = pictures[0].get_image(doc) + assert image is not None + assert image.size == (7, 5) + assert image.getpixel((0, 0)) == (0, 255, 0) + + def test_code_block_language_detection(): markdown = ( "```python\n"