Fix file url parsing on Windows (#186)

pull/193/head
Demaro Stanberry 2 years ago committed by GitHub
parent 9e33d679e2
commit 0bfab74eac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"runtime"
"strings"
"github.com/mickael-menu/zk/internal/util/errors"
)
@ -17,7 +18,8 @@ func pathToURI(path string) string {
}
func uriToPath(uri string) (string, error) {
parsed, err := url.Parse(uri)
s := strings.ReplaceAll(uri, "%5C", "/")
parsed, err := url.Parse(s)
if err != nil {
return "", err
}
@ -28,7 +30,13 @@ func uriToPath(uri string) (string, error) {
if runtime.GOOS == "windows" {
// In Windows "file:///c:/tmp/foo.md" is parsed to "/c:/tmp/foo.md".
// Strip the first character to get a valid path.
return parsed.Path[1:], nil
if strings.Contains(parsed.Path[1:], ":") {
// url.Parse() behaves differently with "file:///c:/..." and "file://c:/..."
return parsed.Path[1:], nil
} else {
// if the windows drive is not included in Path it will be in Host
return parsed.Host + "/" + parsed.Path[1:], nil
}
}
return parsed.Path, nil
}

Loading…
Cancel
Save