Browse Source

fix: db import of markdown tables and multiline blocks

Fixes LOG-3072 and fixes LOG-3070
Gabriel Horner 1 year ago
parent
commit
66439dde68

+ 21 - 13
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -658,40 +658,48 @@
   [blocks content format {:keys [user-config db-graph-mode?] :as options}]
   {:pre [(seq blocks) (string? content) (contains? #{:markdown :org} format)]}
   (let [encoded-content (utf8/encode content)
+        all-blocks (vec (reverse blocks))
         [blocks body pre-block-properties]
         (loop [headings []
                blocks (reverse blocks)
+               block-idx 0
                timestamps {}
                properties {}
                body []]
           (if (seq blocks)
-            (let [[block pos-meta] (first blocks)
-                  ;; in db-graph-mode, property part is not included in block/title
-                  pos-meta (if db-graph-mode?
-                             pos-meta
-                             (assoc pos-meta :end_pos
-                                    (if (seq headings)
-                                      (get-in (last headings) [:meta :start_pos])
-                                      nil)))]
+            (let [[block pos-meta] (first blocks)]
               (cond
                 (paragraph-timestamp-block? block)
                 (let [timestamps (extract-timestamps block)
                       timestamps' (merge timestamps timestamps)]
-                  (recur headings (rest blocks) timestamps' properties body))
+                  (recur headings (rest blocks) (inc block-idx) timestamps' properties body))
 
                 (gp-property/properties-ast? block)
                 (let [properties (extract-properties (second block) (assoc user-config :format format))]
-                  (recur headings (rest blocks) timestamps properties body))
+                  (recur headings (rest blocks) (inc block-idx) timestamps properties body))
 
                 (heading-block? block)
-                (let [block' (construct-block block properties timestamps body encoded-content format pos-meta options)
+                  ;; 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
+                                  ;; fix start_pos
+                                  (assoc pos-meta :end_pos
+                                         (if (seq headings)
+                                           (get-in (last headings) [:meta :start_pos])
+                                           nil)))
+                      block' (construct-block block properties timestamps body encoded-content format pos-meta' options)
                       block'' (if db-graph-mode?
                                 block'
                                 (assoc block' :macros (extract-macros-from-ast (cons block body))))]
-                  (recur (conj headings block'') (rest blocks) {} {} []))
+                  (recur (conj headings block'') (rest blocks) (inc block-idx) {} {} []))
 
                 :else
-                (recur headings (rest blocks) timestamps properties (conj body block))))
+                (recur headings (rest blocks) (inc block-idx) timestamps properties (conj body block))))
             [(-> (reverse headings)
                  sanity-blocks-data)
              body

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

@@ -167,7 +167,7 @@
 
       ;; Counts
       ;; Includes journals as property values e.g. :logseq.task/deadline
-      (is (= 17 (count (d/q '[:find ?b :where [?b :block/type "journal"]] @conn))))
+      (is (= 18 (count (d/q '[:find ?b :where [?b :block/type "journal"]] @conn))))
 
       ;; Don't count pages like url.md that have properties but no content
       (is (= 6
@@ -346,6 +346,11 @@
                   count))
           "A block with different case of same ref names has 1 distinct ref"))
 
+    (testing "multiline blocks"
+      (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 (= "logbook block" (:block/title (find-block-by-content @conn #"logbook block")))))
+
     (testing "block refs and path-refs"
       (let [block (find-block-by-content @conn "old todo block")]
         (is (set/subset?

+ 11 - 0
deps/graph-parser/test/resources/exporter-test-graph/journals/2024_08_07.md

@@ -0,0 +1,11 @@
+- test multilines
+- |markdown| table|
+  |some|thing|
+- multiline block
+  a 2nd
+  and a 3rd
+- DOING logbook block
+  :LOGBOOK:
+  CLOCK: [2024-08-07 Wed 11:47:50]
+  CLOCK: [2024-08-07 Wed 11:47:53]
+  :END: