fix(editor): handle protocol urls

Related: #9243
enhance/apis-improvements
Andelf 2023-04-26 11:09:28 +08:00 committed by Gabriel Horner
parent a720a85eed
commit 0c91829365
3 changed files with 17 additions and 4 deletions

View File

@ -309,3 +309,11 @@
(string/starts-with? p "/") (string/starts-with? p "/")
;; is windows dir ;; is windows dir
(re-find #"^[a-zA-Z]:[/\\]" p))))) (re-find #"^[a-zA-Z]:[/\\]" p)))))
(defn protocol-url?
"Whether path `p` is a protocol URL.
This is a loose check, it only checks if there is a valid protocol prefix."
[p]
(boolean (and (re-find #"^[a-zA-Z0-9_+\-\.]+:" p)
(not (string/includes? p " ")))))

View File

@ -80,6 +80,9 @@
(and (string? v) (>= (count v) 2) (= "\"" (first v) (last v)))) (and (string? v) (>= (count v) 2) (= "\"" (first v) (last v))))
(defn url? (defn url?
"Test if it is a `protocol://`-style URL.
NOTE: Can not handle mailto: links, use this with caution."
[s] [s]
(and (string? s) (and (string? s)
(try (try

View File

@ -998,7 +998,9 @@
(defn- relative-assets-path->absolute-path (defn- relative-assets-path->absolute-path
[path] [path]
(if (path/absolute? path) (when (path/protocol-url? path)
(js/console.error "BUG: relative-assets-path->absolute-path called with protocol url" path))
(if (or (path/absolute? path) (path/protocol-url? path))
path path
(.. util/node-path (.. util/node-path
(join (config/get-repo-dir (state/get-current-repo)) (join (config/get-repo-dir (state/get-current-repo))
@ -1080,7 +1082,7 @@
(not (string/includes? s ".")) (not (string/includes? s "."))
(page-reference (:html-export? config) s config label) (page-reference (:html-export? config) s config label)
(gp-util/url? s) (path/protocol-url? s)
(->elem :a {:href s (->elem :a {:href s
:data-href s :data-href s
:target "_blank"} :target "_blank"}
@ -1102,7 +1104,7 @@
(->elem (->elem
:a :a
(cond-> (cond->
{:href (str "file://" path) {:href (path/path-join "file://" path)
:data-href path :data-href path
:target "_blank"} :target "_blank"}
title title
@ -1184,7 +1186,7 @@
href)] href)]
(->elem (->elem
:a :a
(cond-> {:href (str "file://" href*) (cond-> {:href (path/path-join "file://" href*)
:data-href href* :data-href href*
:target "_blank"} :target "_blank"}
title (assoc :title title)) title (assoc :title title))