Python tool for converting files and office documents to Markdown.
Fix percent-encoded Windows drive paths in file URIs (#2315)
Zhewen Tanf1df5ac4ab84
diff --git a/packages/markitdown/src/markitdown/_uri_utils.py b/packages/markitdown/src/markitdown/_uri_utils.pyindex 603da63e..9a529574 100644--- a/packages/markitdown/src/markitdown/_uri_utils.py+++ b/packages/markitdown/src/markitdown/_uri_utils.py@@ -12,7 +12,10 @@ def file_uri_to_path(file_uri: str) -> Tuple[str | None, str]:raise ValueError(f"Not a file URL: {file_uri}")netloc = parsed.netloc if parsed.netloc else None- path = os.path.abspath(url2pathname(parsed.path))+ path = url2pathname(parsed.path)+ if os.name == "nt" and path[:1] in "/\\" and path[2:3] == ":":+ path = path[1:]+ path = os.path.abspath(path)return netloc, pathdiff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.pyindex 135fb770..6804c806 100644--- a/packages/markitdown/tests/test_module_misc.py+++ b/packages/markitdown/tests/test_module_misc.py@@ -1,12 +1,15 @@#!/usr/bin/env python3 -m pytestimport ioimport json+import ntpathimport osimport reimport shutil+from types import SimpleNamespaceimport pytestfrom unittest.mock import MagicMock+import markitdown._uri_utils as uri_utilsfrom markitdown._uri_utils import parse_data_uri, file_uri_to_pathfrom markitdown._markitdown import _get_content_disposition_filenamefrom markitdown.converters import RssConverter@@ -255,6 +258,20 @@ def test_file_uris() -> None:assert path == "/path/to/file.txt"+def test_file_uri_with_percent_encoded_windows_drive(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from nturl2path import url2pathname as windows_url2pathname++ monkeypatch.setattr(uri_utils, "os", SimpleNamespace(name="nt", path=ntpath))+ monkeypatch.setattr(uri_utils, "url2pathname", windows_url2pathname)++ netloc, path = uri_utils.file_uri_to_path("file:///C%3A/Temp/example.md")++ assert netloc is None+ assert path == r"C:\Temp\example.md"++def test_docx_comments() -> None:# Test DOCX processing, with comments and setting style_map on initmarkitdown_with_style_map = MarkItDown(style_map="comment-reference => ")