Skip to content
Open
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
5 changes: 3 additions & 2 deletions packages/markitdown/src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,9 @@ def convert_response(
parts = response.headers["content-type"].split(";")
mimetype = parts.pop(0).strip()
for part in parts:
if part.strip().startswith("charset="):
_charset = part.split("=")[1].strip()
key, sep, value = part.strip().partition("=")
if sep and key.strip().lower() == "charset":
_charset = value.strip()
if len(_charset) > 0:
charset = _charset

Expand Down
4 changes: 2 additions & 2 deletions packages/markitdown/src/markitdown/_uri_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def parse_data_uri(uri: str) -> Tuple[str | None, Dict[str, str], bytes]:
# Handle key=value pairs in the middle
if "=" in part:
key, value = part.split("=", 1)
attributes[key] = value
attributes[key.lower()] = value
elif len(part) > 0:
attributes[part] = ""
attributes[part.lower()] = ""

content = base64.b64decode(data) if is_base64 else unquote_to_bytes(data)

Expand Down
18 changes: 18 additions & 0 deletions packages/markitdown/tests/test_module_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ def test_data_uris() -> None:
assert attributes["charset"] == "utf-8"
assert data == b"Hello, World!"

data_uri = "data:text/plain;Charset=utf-8,Hello%2C%20World%21"
mime_type, attributes, data = parse_data_uri(data_uri)
assert mime_type == "text/plain"
assert len(attributes) == 1
assert attributes["charset"] == "utf-8"
assert data == b"Hello, World!"


def test_file_uris() -> None:
# Test file URI with an empty host
Expand Down Expand Up @@ -252,6 +259,17 @@ def test_file_uris() -> None:
assert path == "/path/to/file.txt"


def test_response_content_type_charset_is_case_insensitive() -> None:
response = MagicMock()
response.headers = {"content-type": "text/plain; Charset=UTF-8"}
response.url = "https://example.com/test.txt"
response.iter_content.return_value = [b"Hello, World!"]

result = MarkItDown().convert_response(response)

assert result.text_content == "Hello, World!"


def test_docx_comments() -> None:
# Test DOCX processing, with comments and setting style_map on init
markitdown_with_style_map = MarkItDown(style_map="comment-reference => ")
Expand Down