Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docling/document_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion tests/test_backend_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Loading