Incorrect file:// URLs generated by drop on Windows

Steps to reproduce

Drag a file from Windows Explorer to an Obsidian editor pane, hold Ctrl and drop.

Expected result

A standard Windows file:///C|/path/to/file-style URL should be inserted (note vertical bar and forward slashes)

Actual result

The actual link is file:///C:%5Cpath%5Cto%5Cfile (colon and escaped backslashes), which is not supported by all Windows tools. (For example, attempting to open such URLs in Chrome does not work.)

Environment

  • Operating system: Windows
  • Obsidian version: 0.12.10

Additional information

To create a proper Windows file: URL, the backslashes must be replaced with slashes, and the colon replaced with a vertical bar, before URL-encoding the result. (e.g. in handleDropIntoEditor(), but ideally anywhere else Obsidian converts a Windows path into a file: URL as well.)

4 Likes

For node.js fileURLToPath and file url schema specification, it’s consider invalid to include encoded slash in pathname:

To create a proper Windows file: URL, the backslashes must be replaced with slashes, and the colon replaced with a vertical bar, before URL-encoding the result. (e.g. in handleDropIntoEditor() , but ideally anywhere else Obsidian converts a Windows path into a file: URL as well.)

I can confirm that this issue persist in v1.5.3.

It seems that in handleDropIntoEditor, the url is created by concating file:// with url-encoded file path directly, which is obviously not a correct way to deal with file url, especially in win32 env. node.js url.pathToFileURL(path) is specificly designed for this purpose. Since url.fileURLToPath is already used in app.vault.resolveFileUrl, it should be okay to use this api as well