Explorar el Código

enhance: normalize all paths in app

Junyi Du hace 4 años
padre
commit
37f08d96e1

+ 20 - 18
src/main/frontend/config.cljs

@@ -371,26 +371,28 @@
     (util/node-path.join (get-repo-dir repo-url) path)))
 
 (defn get-file-path
+  "Normalization happens here"
   [repo-url relative-path]
   (when (and repo-url relative-path)
-    (cond
-      (and (or (util/electron?) (mobile-util/native-android?)) (local-db? repo-url))
-      (let [dir (get-repo-dir repo-url)]
-        (if (string/starts-with? relative-path dir)
-          relative-path
-          (str dir "/"
-               (string/replace relative-path #"^/" ""))))
-
-      (and (mobile-util/native-ios?) (local-db? repo-url))
-      (let [dir (-> (get-repo-dir repo-url)
-                    (string/replace "file:///" "file:/"))]
-        (js/decodeURI (str dir relative-path)))
-
-      (= "/" (first relative-path))
-      (subs relative-path 1)
-
-      :else
-      relative-path)))
+    (let [path (cond
+                 (and (or (util/electron?) (mobile-util/native-android?)) (local-db? repo-url))
+                 (let [dir (get-repo-dir repo-url)]
+                   (if (string/starts-with? relative-path dir)
+                     relative-path
+                     (str dir "/"
+                          (string/replace relative-path #"^/" ""))))
+
+                 (and (mobile-util/native-ios?) (local-db? repo-url))
+                 (let [dir (-> (get-repo-dir repo-url)
+                               (string/replace "file:///" "file:/"))]
+                   (js/decodeURI (str dir relative-path)))
+
+                 (= "/" (first relative-path))
+                 (subs relative-path 1)
+
+                 :else
+                 relative-path)]
+      (util/path-normalize path))))
 
 (defn get-config-path
   ([]

+ 1 - 1
src/main/frontend/handler/extract.cljs

@@ -76,7 +76,7 @@
                          (util/remove-nils
                           (assoc
                            (block/page-name->map page false)
-                           :block/file {:file/path file}))
+                           :block/file {:file/path (util/path-normalize file)}))
                          (seq properties)
                          (assoc :block/properties properties)
 

+ 3 - 1
src/main/frontend/handler/file.cljs

@@ -98,7 +98,7 @@
                                         (seq images)
                                         (merge (zipmap images (repeat (count images) ""))))
                         file-contents (for [[file content] file-contents]
-                                        {:file/path file
+                                        {:file/path (util/path-normalize file)
                                          :file/content content})]
                     (ok-handler file-contents))))
         (p/catch (fn [error]
@@ -124,6 +124,7 @@
       data)))
 
 (defn- page-exists-in-another-file
+  "Conflict of files towards same page"
   [repo-url page file]
   (when-let [page-name (:block/name page)]
     (let [current-file (:file/path (db/get-page-file repo-url page-name))]
@@ -153,6 +154,7 @@
 
                :else
                file)
+        file (util/path-normalize file)
         new? (nil? (db/entity [:file/path file]))]
     ;; TODO: refactor with reset-file!
     ;; TODO: missing text/scheduled-deadline-dash->star

+ 3 - 3
src/main/frontend/handler/web/nfs.cljs

@@ -47,7 +47,7 @@
    (cond
      mobile-native?
      (map (fn [{:keys [uri content size mtime]}]
-            {:file/path             (string/replace uri "file://" "")
+            {:file/path             (util/path-normalize (string/replace uri "file://" ""))
              :file/last-modified-at mtime
              :file/size             size
              :file/content content})
@@ -56,7 +56,7 @@
      electron?
      (map (fn [{:keys [path stat content]}]
             (let [{:keys [mtime size]} stat]
-              {:file/path             path
+              {:file/path             (util/path-normalize path)
                :file/last-modified-at mtime
                :file/size             size
                :file/content content}))
@@ -70,7 +70,7 @@
                     path (-> (get-attr "webkitRelativePath")
                              (string/replace-first (str dir-name "/") ""))]
                 {:file/name             (get-attr "name")
-                 :file/path             path
+                 :file/path             (util/path-normalize path)
                  :file/last-modified-at (get-attr "lastModified")
                  :file/size             (get-attr "size")
                  :file/type             (get-attr "type")

+ 4 - 6
src/main/frontend/util.cljc

@@ -1184,12 +1184,10 @@
 (defn normalize
   [s]
   (.normalize s "NFC"))
-
-(defn search-normalize-content
-  "Normalize string for searching (loose, without lowercasing)
-   Case-sensitivity is ensured by the search engine"
+(defn path-normalize
+  "Normalize file path (for reading, not writting)"
   [s]
-  (.normalize s "NFKD"))
+  (.normalize s "NFC"))
 
 (defn search-normalize
   "Normalize string for searching (loose)"
@@ -1203,7 +1201,7 @@
     (.normalize (string/lower-case s) "NFKD") s))
 
 (defn page-name-sanity
-  "Sanitize the page-name for file name (strict)"
+  "Sanitize the page-name for file name (strict), for file writting"
   ([page-name]
    (page-name-sanity page-name false))
   ([page-name replace-slash?]