瀏覽代碼

Revert "Remove :block/macros"

This reverts commit 21d550de12b1366a89751f6501b4519abb900334.
Tienson Qin 1 年之前
父節點
當前提交
ac31842cec

+ 1 - 0
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -274,6 +274,7 @@
    ;; refs
    ;; refs
    [:block/page :int]
    [:block/page :int]
    [:block/path-refs {:optional true} [:set :int]]
    [:block/path-refs {:optional true} [:set :int]]
+   [:block/macros {:optional true} [:set :int]]
    [:block/link {:optional true} :int]
    [:block/link {:optional true} :int]
     ;; other
     ;; other
    [:block/marker {:optional true} :string]
    [:block/marker {:optional true} :string]

+ 5 - 1
deps/db/src/logseq/db/frontend/schema.cljs

@@ -100,7 +100,9 @@
    :block/journal-day {}
    :block/journal-day {}
    ;; page's namespace
    ;; page's namespace
    :block/namespace {:db/valueType :db.type/ref}
    :block/namespace {:db/valueType :db.type/ref}
-
+   ;; macros in block
+   :block/macros {:db/valueType :db.type/ref
+                  :db/cardinality :db.cardinality/many}
    ;; block's file
    ;; block's file
    :block/file {:db/valueType :db.type/ref}
    :block/file {:db/valueType :db.type/ref}
 
 
@@ -144,6 +146,7 @@
     :block/properties
     :block/properties
     :block/properties-order
     :block/properties-order
     :block/properties-text-values
     :block/properties-text-values
+    :block/macros
     :block/invalid-properties
     :block/invalid-properties
     :block/warning
     :block/warning
     }
     }
@@ -152,6 +155,7 @@
 ;; If only block/content changes
 ;; If only block/content changes
 (def db-version-retract-attributes
 (def db-version-retract-attributes
   #{:block/refs
   #{:block/refs
+    :block/macros
     :block/warning})
     :block/warning})
 
 
 
 

+ 25 - 2
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -494,6 +494,27 @@
                (conj acc (assoc block :block/path-refs path-ref-pages))
                (conj acc (assoc block :block/path-refs path-ref-pages))
                parents)))))
                parents)))))
 
 
+(defn- macro->block
+  "macro: {:name \"\" arguments [\"\"]}"
+  [macro]
+  {:block/uuid (random-uuid)
+   :block/type "macro"
+   :block/properties {:logseq.macro-name (:name macro)
+                      :logseq.macro-arguments (:arguments macro)}})
+
+(defn extract-macros-from-ast
+  [ast]
+  (let [*result (atom #{})]
+    (walk/postwalk
+     (fn [f]
+       (if (and (vector? f) (= (first f) "Macro"))
+         (do
+           (swap! *result conj (second f))
+           nil)
+         f))
+     ast)
+    (mapv macro->block @*result)))
+
 (defn with-pre-block-if-exists
 (defn with-pre-block-if-exists
   [blocks body pre-block-properties encoded-content {:keys [db date-formatter user-config]}]
   [blocks body pre-block-properties encoded-content {:keys [db date-formatter user-config]}]
   (let [first-block (first blocks)
   (let [first-block (first blocks)
@@ -524,6 +545,7 @@
                                 :block/properties-text-values properties-text-values
                                 :block/properties-text-values properties-text-values
                                 :block/invalid-properties invalid-properties
                                 :block/invalid-properties invalid-properties
                                 :block/pre-block? pre-block?
                                 :block/pre-block? pre-block?
+                                :block/macros (extract-macros-from-ast body)
                                 :block/body body}
                                 :block/body body}
                          {:keys [tags refs]}
                          {:keys [tags refs]}
                          (with-page-block-refs {:body body :refs property-refs} false db date-formatter)]
                          (with-page-block-refs {:body body :refs property-refs} false db date-formatter)]
@@ -669,8 +691,9 @@
                   (recur headings (rest blocks) timestamps properties body))
                   (recur headings (rest blocks) timestamps properties body))
 
 
                 (heading-block? block)
                 (heading-block? block)
-                (let [block' (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)]
-                  (recur (conj headings block') (rest blocks) {} {} []))
+                (let [block' (construct-block block properties timestamps body encoded-content format pos-meta with-id? options)
+                      block'' (assoc block' :macros (extract-macros-from-ast (cons block body)))]
+                  (recur (conj headings block'') (rest blocks) {} {} []))
 
 
                 :else
                 :else
                 (recur headings (rest blocks) timestamps properties (conj body block))))
                 (recur headings (rest blocks) timestamps properties (conj body block))))

+ 15 - 0
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -529,6 +529,20 @@
                      (map #(add-uuid-to-page-map % page-names-to-uuids)))))
                      (map #(add-uuid-to-page-map % page-names-to-uuids)))))
       block)))
       block)))
 
 
+(defn- update-block-macros
+  [block db page-names-to-uuids]
+  (if (seq (:block/macros block))
+    (update block :block/macros
+            (fn [macros]
+              (mapv (fn [m]
+                      (-> m
+                          (update :block/properties
+                                  (fn [props]
+                                    (update-keys props #(cached-prop-name->uuid db page-names-to-uuids %))))
+                          (assoc :block/uuid (d/squuid))))
+                    macros)))
+    block))
+
 (defn- fix-pre-block-references
 (defn- fix-pre-block-references
   [{:block/keys [left parent page] :as block} pre-blocks]
   [{:block/keys [left parent page] :as block} pre-blocks]
   (cond-> block
   (cond-> block
@@ -545,6 +559,7 @@
   (let [old-property-schemas @(:property-schemas import-state)]
   (let [old-property-schemas @(:property-schemas import-state)]
     (-> block
     (-> block
         (fix-pre-block-references pre-blocks)
         (fix-pre-block-references pre-blocks)
+        (update-block-macros db page-names-to-uuids)
         ;; needs to come before update-block-refs to detect new property schemas
         ;; needs to come before update-block-refs to detect new property schemas
         (handle-block-properties db page-names-to-uuids (:block/refs block) options)
         (handle-block-properties db page-names-to-uuids (:block/refs block) options)
         (update-block-refs page-names-to-uuids old-property-schemas options)
         (update-block-refs page-names-to-uuids old-property-schemas options)

+ 3 - 2
deps/graph-parser/src/logseq/graph_parser/test/docs_graph_helper.cljs

@@ -114,7 +114,8 @@
             :card-next-schedule 6, :ls-type 79, :card-last-interval 6, :type 107,
             :card-next-schedule 6, :ls-type 79, :card-last-interval 6, :type 107,
             :template 5, :title 114, :alias 41, :supports 5, :id 145, :url 5,
             :template 5, :title 114, :alias 41, :supports 5, :id 145, :url 5,
             :card-ease-factor 6, :created-at 46,
             :card-ease-factor 6, :created-at 46,
-            :card-last-reviewed 6, :platforms 51, :initial-version 8, :heading 226}
+            :card-last-reviewed 6, :platforms 51, :initial-version 8, :heading 226
+            :logseq.macro-arguments 109, :logseq.macro-name 109}
            (get-top-block-properties db))
            (get-top-block-properties db))
         "Counts for top block properties")
         "Counts for top block properties")
 
 
@@ -161,7 +162,7 @@
   ;; only increase over time as the docs graph rarely has deletions
   ;; only increase over time as the docs graph rarely has deletions
   (testing "Counts"
   (testing "Counts"
     (is (= 303 (count files)) "Correct file count")
     (is (= 303 (count files)) "Correct file count")
-    (is (= 63954 (count (d/datoms db :eavt))) "Correct datoms count")
+    (is (= 64390 (count (d/datoms db :eavt))) "Correct datoms count")
 
 
     (is (= 5866
     (is (= 5866
            (ffirst
            (ffirst

+ 9 - 2
deps/outliner/src/logseq/outliner/datascript.cljs

@@ -60,12 +60,19 @@
                                          {:tx tx :revert-tx revert-tx})) refs)))
                                          {:tx tx :revert-tx revert-tx})) refs)))
                             (apply concat))
                             (apply concat))
           retracted-tx' (mapcat :tx retracted-tx)
           retracted-tx' (mapcat :tx retracted-tx)
-          revert-tx (mapcat :revert-tx retracted-tx)]
+          revert-tx (mapcat :revert-tx retracted-tx)
+          macros-tx (mapcat (fn [b]
+                              ;; Only delete if last reference
+                              (keep #(when (<= (count (:block/_macros (d/entity db (:db/id %))))
+                                               1)
+                                       (when (:db/id %) (vector :db.fn/retractEntity (:db/id %))))
+                                    (:block/macros b)))
+                            retracted-blocks)]
       (when (and (seq retracted-tx') (fn? set-state-fn))
       (when (and (seq retracted-tx') (fn? set-state-fn))
         (set-state-fn [:editor/last-replace-ref-content-tx repo]
         (set-state-fn [:editor/last-replace-ref-content-tx repo]
                       {:retracted-block-ids retracted-block-ids
                       {:retracted-block-ids retracted-block-ids
                        :revert-tx revert-tx}))
                        :revert-tx revert-tx}))
-      (concat txs retracted-tx'))
+      (concat txs retracted-tx' macros-tx))
     txs))
     txs))
 
 
 (defn transact!
 (defn transact!

+ 13 - 3
src/main/frontend/handler/editor.cljs

@@ -66,7 +66,8 @@
             [promesa.core :as p]
             [promesa.core :as p]
             [rum.core :as rum]
             [rum.core :as rum]
             [frontend.fs.capacitor-fs :as capacitor-fs]
             [frontend.fs.capacitor-fs :as capacitor-fs]
-            [logseq.db :as ldb]))
+            [logseq.db :as ldb]
+            [frontend.db.query-dsl :as query-dsl]))
 
 
 ;; FIXME: should support multiple images concurrently uploading
 ;; FIXME: should support multiple images concurrently uploading
 
 
@@ -3407,8 +3408,17 @@
 (defn- valid-dsl-query-block?
 (defn- valid-dsl-query-block?
   "Whether block has a valid dsl query."
   "Whether block has a valid dsl query."
   [block]
   [block]
-  (some-> (:block/content block)
-          (string/includes? "{{query ")))
+  (->> (:block/macros (db/entity (:db/id block)))
+       (some (fn [macro]
+               (let [properties (:block/properties macro)
+                     macro-name (pu/lookup properties :logseq.property/macro-name)
+                     macro-arguments (pu/lookup properties :logseq.property/macro-arguments)]
+                 (when-let [query-body (and (= "query" macro-name) (not-empty (string/join " " macro-arguments)))]
+                   (seq (:query
+                         (try
+                           (query-dsl/parse-query query-body)
+                           (catch :default _e
+                             nil))))))))))
 
 
 (defn- valid-custom-query-block?
 (defn- valid-custom-query-block?
   "Whether block has a valid custom query."
   "Whether block has a valid custom query."

+ 6 - 6
src/main/frontend/handler/export/text.cljs

@@ -516,13 +516,13 @@
    :export-blocks-as-markdown
    :export-blocks-as-markdown
    (try
    (try
      (let [content
      (let [content
-          (if (string? root-block-uuids-or-page-name)
+           (if (string? root-block-uuids-or-page-name)
               ;; page
               ;; page
-            (common/get-page-content root-block-uuids-or-page-name)
-            (common/root-block-uuids->content repo root-block-uuids-or-page-name))
-          first-block (db/entity [:block/uuid (first root-block-uuids-or-page-name)])
-          format (or (:block/format first-block) (state/get-preferred-format))]
-      (export-helper content format options))
+             (common/get-page-content root-block-uuids-or-page-name)
+             (common/root-block-uuids->content repo root-block-uuids-or-page-name))
+           first-block (db/entity [:block/uuid (first root-block-uuids-or-page-name)])
+           format (or (:block/format first-block) (state/get-preferred-format))]
+       (export-helper content format options))
      (catch :default e
      (catch :default e
        (js/console.error e)))))
        (js/console.error e)))))