Преглед изворни кода

enhance: import page and block embeds

One embed per block as that's what DB version supports.
Fixes https://github.com/logseq/db-test/issues/198
Gabriel Horner пре 4 месеци
родитељ
комит
507ff3ce29

+ 41 - 3
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -13,6 +13,7 @@
             [logseq.common.config :as common-config]
             [logseq.common.path :as path]
             [logseq.common.util :as common-util]
+            [logseq.common.util.block-ref :as block-ref]
             [logseq.common.util.date-time :as date-time-util]
             [logseq.common.util.macro :as macro-util]
             [logseq.common.util.namespace :as ns-util]
@@ -1024,7 +1025,7 @@
          (apply str)
          string/trim)))
 
-(defn- handle-quote-in-block
+(defn- handle-quotes
   "If a block contains a quote, convert block to #Quote node"
   [block opts]
   (if-let [ast-block (first (filter #(= "Quote" (first %)) (:block.temp/ast-blocks block)))]
@@ -1034,6 +1035,42 @@
             :block/tags [:logseq.class/Quote-block]})
     block))
 
+(defn- handle-embeds
+  "If a block contains page or block embeds, converts block to a :block/link based embed"
+  [block page-names-to-uuids {:keys [log-fn]
+                              :or {log-fn prn}}]
+  (let [results (atom [])
+        ;; Combine this prewalk with find-asset-links if we do this prewalk again
+        _ (when (string/includes? (:block/title block) "{{embed")
+            (walk/prewalk
+             (fn [x]
+               (when (and (vector? x)
+                          (= "Macro" (first x))
+                          (= "embed" (:name (second x))))
+                 (swap! results conj x))
+               x)
+             (:block.temp/ast-blocks block)))]
+    (if-let [embed-node (first @results)]
+      (cond
+        (page-ref/page-ref? (str (first (:arguments (second embed-node)))))
+        (let [page-uuid (get-page-uuid page-names-to-uuids
+                                       (some-> (page-ref/get-page-name (first (:arguments (second embed-node))))
+                                               common-util/page-name-sanity-lc)
+                                       {:block block})]
+          (merge block
+                 {:block/title ""
+                  :block/link [:block/uuid page-uuid]}))
+        (block-ref/block-ref? (str (first (:arguments (second embed-node)))))
+        (let [block-uuid (uuid (block-ref/get-block-ref-id (first (:arguments (second embed-node)))))]
+          (merge block
+                 {:block/title ""
+                  :block/link [:block/uuid block-uuid]}))
+        :else
+        (do
+          (log-fn :invalid-embed-arguments "Ignore embed because of invalid arguments" :args (:arguments (second embed-node)))
+          block))
+      block)))
+
 (defn- build-block-tx
   [db block* pre-blocks {:keys [page-names-to-uuids] :as per-file-state} {:keys [import-state journal-created-ats] :as options}]
   ;; (prn ::block-in block*)
@@ -1053,7 +1090,8 @@
                    (fix-block-name-lookup-ref page-names-to-uuids)
                    (update-block-refs page-names-to-uuids options)
                    (update-block-tags db (:user-options options) per-file-state (:all-idents import-state))
-                   (handle-quote-in-block (select-keys options [:log-fn]))
+                   (handle-embeds page-names-to-uuids (select-keys options [:log-fn]))
+                   (handle-quotes (select-keys options [:log-fn]))
                    (update-block-marker options)
                    (update-block-priority options)
                    add-missing-timestamps
@@ -1527,7 +1565,7 @@
                       :or {notify-user #(println "[WARNING]" (:msg %))
                            log-fn prn}
                       :as *options}]
-  (let [options (assoc *options :notify-user notify-user :log-fn log-fn)
+  (let [options (assoc *options :notify-user notify-user :log-fn log-fn :file file)
         {:keys [pages blocks]} (extract-pages-and-blocks @conn file content options)
         tx-options (merge (build-tx-options options)
                           {:journal-created-ats (build-journal-created-ats pages)})

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

@@ -429,6 +429,26 @@
              (:block/title (db-test/find-block-by-content @conn #"Learn Datalog")))
           "Imports full quote with various ast types"))
 
+    (testing "embeds"
+      (is (= {:block/title ""}
+             (-> (d/q '[:find [(pull ?b [*]) ...]
+                        :in $ ?title
+                        :where [?b :block/link ?l] [?b :block/page ?bp] [?bp :block/journal-day 20250612] [?l :block/title ?title]]
+                      @conn
+                      "page embed")
+                 first
+                 (select-keys [:block/title])))
+          "Page embed linked correctly")
+      (is (= {:block/title ""}
+             (-> (d/q '[:find [(pull ?b [*]) ...]
+                        :in $ ?title
+                        :where [?b :block/link ?l] [?b :block/page ?bp] [?bp :block/journal-day 20250612] [?l :block/title ?title]]
+                      @conn
+                      "test block embed")
+                 first
+                 (select-keys [:block/title])))
+          "Block embed linked correctly"))
+
     (testing "tags convert to classes"
       (is (= :user.class/Quotes___life
              (:db/ident (db-test/find-page-by-title @conn "life")))

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

@@ -16,4 +16,9 @@
   #+BEGIN_QUOTE
   *Italic* ~~Strikethrough~~ ^^Highlight^^ #[[foo]]
   **Learn Datalog Today** is an interactive tutorial designed to teach you the [Datomic](http://datomic.com/) dialect of [Datalog](http://en.wikipedia.org/wiki/Datalog). Datalog is a declarative **database query language** with roots in logic programming. Datalog has similar expressive power as [SQL](http://en.wikipedia.org/wiki/Sql).
-  #+END_QUOTE
+  #+END_QUOTE
+- test page embed
+  {{embed [[page embed]]}}
+- test block embed
+  id:: 685434e1-0bb9-468c-a660-1642b00b2854
+- {{embed ((685434e1-0bb9-468c-a660-1642b00b2854))}}