Browse Source

fix: db import of advanced queries

Fixes LOG-3082
Gabriel Horner 1 year ago
parent
commit
dcb73b1003

+ 22 - 12
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -560,7 +560,7 @@
     properties))
     properties))
 
 
 (defn- construct-block
 (defn- construct-block
-  [block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter parse-block]}]
+  [block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter parse-block remove-properties?]}]
   (let [id (get-custom-id-or-new-id properties)
   (let [id (get-custom-id-or-new-id properties)
         ref-pages-in-properties (->> (:page-refs properties)
         ref-pages-in-properties (->> (:page-refs properties)
                                      (remove string/blank?))
                                      (remove string/blank?))
@@ -591,8 +591,10 @@
                     (update :properties-text-values dissoc :collapsed)
                     (update :properties-text-values dissoc :collapsed)
                     (update :properties-order (fn [keys] (vec (remove #{:collapsed} keys)))))
                     (update :properties-order (fn [keys] (vec (remove #{:collapsed} keys)))))
                 block)
                 block)
-        block (assoc block
-                     :block/title (get-block-content encoded-content block format pos-meta block-pattern))
+        title (cond->> (get-block-content encoded-content block format pos-meta block-pattern)
+                remove-properties?
+                (gp-property/remove-properties (:format block)))
+        block (assoc block :block/title title)
         block (if (seq timestamps)
         block (if (seq timestamps)
                 (merge block (timestamps->scheduled-and-deadline timestamps))
                 (merge block (timestamps->scheduled-and-deadline timestamps))
                 block)
                 block)
@@ -679,20 +681,28 @@
                   (recur headings (rest blocks) (inc block-idx) timestamps properties body))
                   (recur headings (rest blocks) (inc block-idx) timestamps properties body))
 
 
                 (heading-block? block)
                 (heading-block? block)
-                  ;; in db-graphs don't include property, deadline/scheduled or logbook text in :block/title
-                (let [pos-meta' (if (and db-graph-mode?
-                                         (when-let [prev-block (first (get all-blocks (dec block-idx)))]
-                                           (or (gp-property/properties-ast? prev-block)
-                                               (= ["Drawer" "logbook"] (take 2 prev-block))
-                                               (and (= "Paragraph" (first prev-block))
-                                                    (seq (set/intersection (set (flatten prev-block)) #{"Deadline" "Scheduled"}))))))
-                                  pos-meta
+                ;; in db-graphs cut multi-line when there is property, deadline/scheduled or logbook text in :block/title
+                (let [cut-multiline? (and db-graph-mode?
+                                          (when-let [prev-block (first (get all-blocks (dec block-idx)))]
+                                            (or (and (gp-property/properties-ast? prev-block)
+                                                     (not= "Custom" (ffirst (get all-blocks (- block-idx 2)))))
+                                                (= ["Drawer" "logbook"] (take 2 prev-block))
+                                                (and (= "Paragraph" (first prev-block))
+                                                     (seq (set/intersection (set (flatten prev-block)) #{"Deadline" "Scheduled"}))))))
+                      pos-meta' (if cut-multiline?
+                                 pos-meta
                                   ;; fix start_pos
                                   ;; fix start_pos
                                   (assoc pos-meta :end_pos
                                   (assoc pos-meta :end_pos
                                          (if (seq headings)
                                          (if (seq headings)
                                            (get-in (last headings) [:meta :start_pos])
                                            (get-in (last headings) [:meta :start_pos])
                                            nil)))
                                            nil)))
-                      block' (construct-block block properties timestamps body encoded-content format pos-meta' options)
+                      ;; Remove properties text from custom queries in db graphs
+                      options' (assoc options
+                                      :remove-properties?
+                                      (and db-graph-mode?
+                                           (and (gp-property/properties-ast? (first (get all-blocks (dec block-idx))))
+                                                (= "Custom" (ffirst (get all-blocks (- block-idx 2)))))))
+                      block' (construct-block block properties timestamps body encoded-content format pos-meta' options')
                       block'' (if db-graph-mode?
                       block'' (if db-graph-mode?
                                 block'
                                 block'
                                 (assoc block' :macros (extract-macros-from-ast (cons block body))))]
                                 (assoc block' :macros (extract-macros-from-ast (cons block body))))]

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

@@ -349,7 +349,9 @@
     (testing "multiline blocks"
     (testing "multiline blocks"
       (is (= "|markdown| table|\n|some|thing|" (:block/title (find-block-by-content @conn #"markdown.*table"))))
       (is (= "|markdown| table|\n|some|thing|" (:block/title (find-block-by-content @conn #"markdown.*table"))))
       (is (= "multiline block\na 2nd\nand a 3rd" (:block/title (find-block-by-content @conn #"multiline block"))))
       (is (= "multiline block\na 2nd\nand a 3rd" (:block/title (find-block-by-content @conn #"multiline block"))))
-      (is (= "logbook block" (:block/title (find-block-by-content @conn #"logbook block")))))
+      (is (= "logbook block" (:block/title (find-block-by-content @conn #"logbook block"))))
+      (is (is (re-find #"(?s)^Text before\n.*BEGIN_QUERY.*END_QUERY\nText after$"
+                       (:block/title (find-block-by-content @conn #":title \"tasks"))))))
 
 
     (testing "block refs and path-refs"
     (testing "block refs and path-refs"
       (let [block (find-block-by-content @conn "old todo block")]
       (let [block (find-block-by-content @conn "old todo block")]

+ 1 - 7
deps/graph-parser/test/resources/exporter-test-graph/journals/2024_02_29.md

@@ -1,10 +1,4 @@
 - b1
 - b1
   rating:: 5
   rating:: 5
 - :rating float
 - :rating float
-  rating:: 5.5
-- query-table:: false
-  FIXME
-  #+BEGIN_QUERY
-  {:title "2nd level tasks with `#p1`"
-  :query (and (task todo doing) [[p1]])}
-  #+END_QUERY
+  rating:: 5.5

+ 12 - 2
deps/graph-parser/test/resources/exporter-test-graph/journals/2024_08_07.md

@@ -1,6 +1,8 @@
-- test multilines
+- test multilines in this page
 - |markdown| table|
 - |markdown| table|
   |some|thing|
   |some|thing|
+- block with props
+  prop-num:: 10
 - multiline block
 - multiline block
   a 2nd
   a 2nd
   and a 3rd
   and a 3rd
@@ -8,4 +10,12 @@
   :LOGBOOK:
   :LOGBOOK:
   CLOCK: [2024-08-07 Wed 11:47:50]
   CLOCK: [2024-08-07 Wed 11:47:50]
   CLOCK: [2024-08-07 Wed 11:47:53]
   CLOCK: [2024-08-07 Wed 11:47:53]
-  :END:
+  :END:
+- Text before
+  query-table:: false
+  query-properties:: [:block]
+  #+BEGIN_QUERY
+  {:title "tasks with todo and doing"
+  :query (task todo doing)}
+  #+END_QUERY
+  Text after