Explorar o código

refactor: path/absolute?

Tienson Qin %!s(int64=2) %!d(string=hai) anos
pai
achega
c1e8d6ed8e

+ 9 - 2
deps/common/src/logseq/common/path.cljs

@@ -1,8 +1,9 @@
 (ns logseq.common.path
   "Path manipulation functions, use '/' sep on all platforms.
    Also handles URL paths."
-  (:require [clojure.string :as string]))
-
+  (:require [clojure.string :as string]
+            ["path" :as path]
+            ["/frontend/utils" :as utils]))
 
 (defn- safe-decode-uri-component
   [uri]
@@ -300,3 +301,9 @@
 (defn dirname
   [path]
   (parent path))
+
+(defn absolute?
+  "Whether path `p` is absolute."
+  [p]
+  (or (.isAbsolute path p)
+      (utils/win32 p)))

+ 8 - 0
deps/common/test/logseq/common/path_test.cljs

@@ -25,3 +25,11 @@
     (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar" "baz")))
     (is (= "assets:///foo.bar/baz" (path/path-join "assets:///foo.bar/" "baz")))))
 
+(deftest path-absolute
+  (testing "absolute"
+    (is (true? (path/absolute? "D:\\sources\\sources.md")))
+    (is (true? (path/absolute? "/home/xxx/logseq/test.md")))
+    (is (false? (path/absolute? "logseq/test.md")))
+    (is (false? (path/absolute? "test.md")))
+    (is (false? (path/absolute? "test")))
+    (is (false? (path/absolute? "D:test.md")))))

+ 1 - 0
externs.js

@@ -139,6 +139,7 @@ dummy.DOCUMENT = function() {};
 dummy.DOCUMENT_TYPE = function() {};
 dummy.ELEMENT = function() {};
 dummy.TEXT = function() {};
+dummy.isAbsolute = function() {};
 
 /**
  * @typedef {{

+ 2 - 2
src/main/frontend/components/block.cljs

@@ -711,7 +711,7 @@
                         (gp-util/url? path)
                         path
 
-                        (util/absolute-path? path)
+                        (path/absolute? path)
                         path
 
                         :else
@@ -1009,7 +1009,7 @@
 
 (defn- relative-assets-path->absolute-path
   [path]
-  (if (util/absolute-path? path)
+  (if (path/absolute? path)
     path
     (.. util/node-path
         (join (config/get-repo-dir (state/get-current-repo))

+ 2 - 1
src/main/frontend/components/file.cljs

@@ -102,7 +102,8 @@
         rel-path (when (string/starts-with? path repo-dir)
                    (path/trim-dir-prefix repo-dir path))
         original-name (db/get-file-page (or path rel-path))
-        in-db? (boolean (db/get-file (or path rel-path)))
+        in-db? (when-not (path/absolute? path)
+                 (boolean (db/get-file (or path rel-path))))
         file-fpath (if in-db?
                      (path/path-join repo-dir path)
                      path)

+ 0 - 2
src/main/frontend/db/model.cljs

@@ -261,8 +261,6 @@
   ([path]
    (get-file (state/get-current-repo) path))
   ([repo path]
-   (when (string/starts-with? path "/")
-     (js/console.error "BUG: Using absolute path while querying DB" path))
    (when (and repo path)
      (when-let [db (conn/get-db repo)]
        (:file/content (db-utils/entity db [:file/path path]))))))

+ 4 - 3
src/main/frontend/handler/assets.cljs

@@ -5,7 +5,8 @@
             [frontend.config :as config]
             [frontend.mobile.util :as mobile-util]
             [logseq.graph-parser.config :as gp-config]
-            [clojure.string :as string]))
+            [clojure.string :as string]
+            [logseq.common.path :as path]))
 
 (defn alias-enabled?
   []
@@ -89,7 +90,7 @@
       protocol-link?
       full-path
 
-      (util/absolute-path? full-path)
+      (path/absolute? full-path)
       (str "file://" full-path)
 
       :else
@@ -112,4 +113,4 @@
  (normalize-asset-resource-url "https://x.com/a.pdf")
  (normalize-asset-resource-url "./a/b.pdf")
  (normalize-asset-resource-url "assets/a/b.pdf")
- (normalize-asset-resource-url "@图书/a/b.pdf"))
+ (normalize-asset-resource-url "@图书/a/b.pdf"))

+ 0 - 8
src/main/frontend/util.cljc

@@ -922,14 +922,6 @@
 (defonce win32? #?(:cljs goog.userAgent/WINDOWS
                    :clj nil))
 
-#?(:cljs
-   (defn absolute-path?
-     [path]
-     (try
-       (js/window.apis.isAbsolutePath path)
-       (catch :default _
-         (utils/win32 path)))))
-
 (defn default-content-with-title
   [text-format]
   (case (name text-format)