Przeglądaj źródła

fix: importing assets in subdirectories

Also fix lint
Gabriel Horner 4 miesięcy temu
rodzic
commit
bb31c4e6f1

+ 1 - 0
deps/db/.carve/config.edn

@@ -9,6 +9,7 @@
                   logseq.db.common.order
                   logseq.db.sqlite.create-graph
                   logseq.db.frontend.malli-schema
+                  logseq.db.frontend.asset
                   ;; Some fns are used by frontend but not worth moving over yet
                   logseq.db.frontend.schema
                   logseq.db.frontend.validate

+ 1 - 1
deps/graph-parser/script/db_import.cljs

@@ -53,7 +53,7 @@
   (p/let [buffer (fs/readFileSync (:path file))
           checksum (db-asset/<get-file-array-buffer-checksum buffer)]
     (swap! assets assoc
-           (node-path/basename (:path file))
+           (gp-exporter/asset-path->name (:path file))
            {:size (.-length buffer)
             :checksum checksum
             :type (db-asset/asset-path->type (:path file))

+ 11 - 5
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -20,6 +20,7 @@
             [logseq.db :as ldb]
             [logseq.db.common.order :as db-order]
             [logseq.db.common.property-util :as db-property-util]
+            [logseq.db.frontend.asset :as db-asset]
             [logseq.db.frontend.class :as db-class]
             [logseq.db.frontend.content :as db-content]
             [logseq.db.frontend.db-ident :as db-ident]
@@ -32,8 +33,7 @@
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.extract :as extract]
             [logseq.graph-parser.property :as gp-property]
-            [promesa.core :as p]
-            [logseq.db.frontend.asset :as db-asset]))
+            [promesa.core :as p]))
 
 (defn- add-missing-timestamps
   "Add updated-at or created-at timestamps if they doesn't exist"
@@ -888,6 +888,12 @@
     (:block/name (:block/parent block))
     (assoc :block/parent {:block/uuid (get-page-uuid page-names-to-uuids (:block/name (:block/parent block)) {:block block :block/parent (:block/parent block)})})))
 
+(defn asset-path->name
+  "Given an asset's relative or full path, create a unique name for identifying an asset.
+   Must handle to paths as ../assets/*, assets/* and with subdirectories"
+  [path]
+  (re-find #"assets/.*$" path))
+
 (defn- handle-assets-in-block
   [block block-ast {:keys [assets ignored-assets]}]
   (let [asset-links
@@ -899,7 +905,7 @@
                                 (filter #(and (= "Link" (first %))
                                               (common-config/local-asset? (second (:url (second %))))))))))
         asset-link (first asset-links)
-        asset-name (some-> asset-link second :url second node-path/basename)]
+        asset-name (some->> asset-link second :url second asset-path->name)]
     (when (> (count asset-links) 1)
       (swap! ignored-assets into
              (map #(hash-map
@@ -910,7 +916,7 @@
     (if asset-name
       (if-let [asset-data (get @assets asset-name)]
         (do
-          (prn :asset-added! asset-name (get @assets asset-name))
+          (prn :asset-added! (node-path/basename asset-name) #_(get @assets asset-name))
         ;; (cljs.pprint/pprint asset-link)
           (swap! assets assoc-in [asset-name :block/uuid] (:block/uuid block))
           (merge block
@@ -918,7 +924,7 @@
                   :logseq.property.asset/type (:type asset-data)
                   :logseq.property.asset/checksum (:checksum asset-data)
                   :logseq.property.asset/size (:size asset-data)
-                  :block/title (db-asset/asset-name->title asset-name)}
+                  :block/title (db-asset/asset-name->title (node-path/basename asset-name))}
                  (when-let [metadata (not-empty (common-util/safe-read-map-string (:metadata (second asset-link))))]
                    {:logseq.property.asset/resize-metadata metadata})))
         (do

+ 4 - 4
deps/graph-parser/test/logseq/graph_parser/exporter_test.cljs

@@ -99,7 +99,7 @@
   (p/let [buffer (fs/readFileSync (:path file))
           checksum (db-asset/<get-file-array-buffer-checksum buffer)]
     (swap! assets assoc
-           (node-path/basename (:path file))
+           (gp-exporter/asset-path->name (:path file))
            {:size (.-length buffer)
             :checksum checksum
             :type (db-asset/asset-path->type (:path file))
@@ -183,9 +183,9 @@
 
       ;; Counts
       ;; Includes journals as property values e.g. :logseq.property/deadline
-      (is (= 25 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
+      (is (= 26 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Journal]] @conn))))
 
-      (is (= 1 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
+      (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Asset]] @conn))))
       (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Task]] @conn))))
       (is (= 4 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Query]] @conn))))
       (is (= 2 (count (d/q '[:find ?b :where [?b :block/tags :logseq.class/Card]] @conn))))
@@ -211,7 +211,7 @@
       (is (= 0 (count @(:ignored-properties import-state))) "No ignored properties")
       (is (= 0 (count @(:ignored-assets import-state))) "No ignored assets")
       (is (= 1 (count @(:ignored-files import-state))) "Ignore .edn for now")
-      (is (= 1 (count @assets))))
+      (is (= 2 (count @assets))))
 
     (testing "logseq files"
       (is (= ".foo {}\n"

BIN
deps/graph-parser/test/resources/exporter-test-graph/assets/subdir/partydino.gif


+ 1 - 0
deps/graph-parser/test/resources/exporter-test-graph/journals/2025_06_12.md

@@ -0,0 +1 @@
+- ![dino!](assets/subdir/partydino.gif){:width 105} tests an asset with a manual link, custom title and in a subdirectory

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

@@ -36,7 +36,6 @@
             [logseq.shui.ui :as shui]
             [promesa.core :as p]
             [rum.core :as rum]
-            ["path" :as node-path]
             [logseq.common.config :as common-config]))
 
 ;; Can't name this component as `frontend.components.import` since shadow-cljs
@@ -353,7 +352,7 @@
       (p/then (fn [buffer]
                 (p/let [checksum (db-asset/<get-file-array-buffer-checksum buffer)]
                   (swap! assets assoc
-                         (node-path/basename (:path file))
+                         (gp-exporter/asset-path->name (:path file))
                          {:size (.-size (:file-object file))
                           :checksum checksum
                           :type (db-asset/asset-path->type (:path file))