Parcourir la source

fix(editor): handle protocol urls

Related: #9243
Andelf il y a 2 ans
Parent
commit
0c91829365

+ 8 - 0
deps/common/src/logseq/common/path.cljs

@@ -309,3 +309,11 @@
                  (string/starts-with? p "/")
                  ;; is windows dir
                  (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 " ")))))

+ 3 - 0
deps/graph-parser/src/logseq/graph_parser/util.cljs

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

+ 6 - 4
src/main/frontend/components/block.cljs

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