Skip to content

Python tool for converting files and office documents to Markdown.

fix: handle URI schemes case-insensitively (#2121)

Lucas Ma05dfe1771689
diff --git a/packages/markitdown/src/markitdown/_markitdown.py b/packages/markitdown/src/markitdown/_markitdown.py
index 50901133..fa67aad2 100644
--- a/packages/markitdown/src/markitdown/_markitdown.py
+++ b/packages/markitdown/src/markitdown/_markitdown.py
@@ -307,12 +307,8 @@ class MarkItDown:
# Local path or url
if isinstance(source, str):
- if (
- source.startswith("http:")
- or source.startswith("https:")
- or source.startswith("file:")
- or source.startswith("data:")
- ):
+ scheme = urlparse(source.strip()).scheme.lower()
+ if scheme in ("http", "https", "file", "data"):
# Rename the url argument to mock_url
# (Deprecated -- use stream_info)
_kwargs = {k: v for k, v in kwargs.items()}
@@ -456,9 +452,10 @@ class MarkItDown:
**kwargs: Any,
) -> DocumentConverterResult:
uri = uri.strip()
+ scheme = urlparse(uri).scheme.lower()
# File URIs
- if uri.startswith("file:"):
+ if scheme == "file":
netloc, path = file_uri_to_path(uri)
if netloc and netloc != "localhost":
raise ValueError(
@@ -472,7 +469,7 @@ class MarkItDown:
**kwargs,
)
# Data URIs
- elif uri.startswith("data:"):
+ elif scheme == "data":
mimetype, attributes, data = parse_data_uri(uri)
base_guess = StreamInfo(
@@ -490,7 +487,7 @@ class MarkItDown:
**kwargs,
)
# HTTP/HTTPS URIs
- elif uri.startswith("http:") or uri.startswith("https:"):
+ elif scheme in ("http", "https"):
response = self._requests_session.get(uri, stream=True)
response.raise_for_status()
return self.convert_response(
diff --git a/packages/markitdown/src/markitdown/_uri_utils.py b/packages/markitdown/src/markitdown/_uri_utils.py
index 9a529574..1c0c6aa2 100644
--- a/packages/markitdown/src/markitdown/_uri_utils.py
+++ b/packages/markitdown/src/markitdown/_uri_utils.py
@@ -20,7 +20,7 @@ def file_uri_to_path(file_uri: str) -> Tuple[str | None, str]:
def parse_data_uri(uri: str) -> Tuple[str | None, Dict[str, str], bytes]:
- if not uri.startswith("data:"):
+ if uri[:5].lower() != "data:":
raise ValueError("Not a data URI")
header, _, data = uri.partition(",")
diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py
index a8f4d744..4924e8a7 100644
--- a/packages/markitdown/tests/test_module_misc.py
+++ b/packages/markitdown/tests/test_module_misc.py
@@ -261,6 +261,25 @@ def test_file_uris() -> None:
assert netloc == "localhost"
assert path == "/path/to/file.txt"
+ # URI schemes are case-insensitive
+ file_uri = "FILE:///path/to/file.txt"
+ netloc, path = file_uri_to_path(file_uri)
+ assert netloc is None
+ assert path == "/path/to/file.txt"
+
+
+def test_convert_case_insensitive_uri_schemes(tmp_path) -> None:
+ markitdown = MarkItDown()
+
+ data_result = markitdown.convert("DATA:text/plain;base64,SGVsbG8sIFdvcmxkIQ==")
+ assert data_result.markdown == "Hello, World!"
+
+ text_file = tmp_path / "hello.txt"
+ text_file.write_text("Hello from file", encoding="utf-8")
+
+ file_result = markitdown.convert(text_file.as_uri().replace("file:", "FILE:", 1))
+ assert file_result.markdown == "Hello from file"
+
# Test file URI with query parameters
file_uri = "file:///path/to/file.txt?param=value"
netloc, path = file_uri_to_path(file_uri)