Skip to content

Commit e57e332

Browse files
fix: truncate uppercase data image URIs (#2122)
* fix: truncate uppercase data image URIs --------- Co-authored-by: Lucas Ma <7184042+pony-maggie@users.noreply.github.com> Co-authored-by: afourney <adamfo@microsoft.com>
1 parent 149f8b2 commit e57e332

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

packages/markitdown/src/markitdown/converters/_markdownify.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def convert_img(
127127
return alt
128128

129129
# Remove dataURIs
130-
if src.startswith("data:") and not self.options["keep_data_uris"]:
130+
if src[:5].lower() == "data:" and not self.options["keep_data_uris"]:
131131
src = src.split(",")[0] + "..."
132132

133133
return "![%s](%s%s)" % (alt, src, title_part)

packages/markitdown/tests/test_module_misc.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ def test_data_uris() -> None:
227227
assert data == b"Hello, World!"
228228

229229

230+
def test_uppercase_data_image_uri_is_truncated_by_default() -> None:
231+
markitdown = MarkItDown()
232+
html = b'<html><body><img alt="dot" src="DATA:image/png;base64,AAAA"></body></html>'
233+
stream_info = StreamInfo(mimetype="text/html", extension=".html")
234+
235+
result = markitdown.convert_stream(io.BytesIO(html), stream_info=stream_info)
236+
assert result.markdown == "![dot](DATA:image/png;base64...)"
237+
assert "AAAA" not in result.markdown
238+
239+
result = markitdown.convert_stream(
240+
io.BytesIO(html), stream_info=stream_info, keep_data_uris=True
241+
)
242+
assert result.markdown == "![dot](DATA:image/png;base64,AAAA)"
243+
244+
230245
def test_file_uris() -> None:
231246
# Test file URI with an empty host
232247
file_uri = "file:///path/to/file.txt"

0 commit comments

Comments
 (0)