ソースを参照

refactor: decouple block extraction for db graphs and exporting

Coupling db graph app to export needs could lead to bugs and is harder
to maintain. Also fix a test assertion
Gabriel Horner 1 年間 前
コミット
3d8e579e67

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

@@ -650,14 +650,14 @@
     block))
 
 (defn extract-blocks
-  "Extract headings from mldoc ast.
-  Args:
-    `blocks`: mldoc ast.
-    `content`: markdown or org-mode text.
-    `format`: content's format, it could be either :markdown or :org-mode.
-    `options`: Options supported are :user-config, :block-pattern, parse-block,
-               :extract-macros, :date-formatter, :page-name, :db-graph-mode? and :db"
-  [blocks content format {:keys [user-config db-graph-mode?] :as options}]
+  "Extract headings from mldoc ast. Args:
+  *`blocks`: mldoc ast.
+  *  `content`: markdown or org-mode text.
+  *  `format`: content's format, it could be either :markdown or :org-mode.
+  *  `options`: Options are :user-config, :block-pattern, :parse-block, :date-formatter, :db and
+     * :db-graph-mode? : Set when a db graph in the frontend
+     * :export-to-db-graph? : Set when exporting to a db graph"
+  [blocks content format {:keys [user-config db-graph-mode? export-to-db-graph?] :as options}]
   {:pre [(seq blocks) (string? content) (contains? #{:markdown :org} format)]}
   (let [encoded-content (utf8/encode content)
         all-blocks (vec (reverse blocks))
@@ -681,8 +681,8 @@
                   (recur headings (rest blocks) (inc block-idx) timestamps properties body))
 
                 (heading-block? block)
-                ;; 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?
+                ;; for db-graphs cut multi-line when there is property, deadline/scheduled or logbook text in :block/title
+                (let [cut-multiline? (and export-to-db-graph?
                                           (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)))))
@@ -690,7 +690,7 @@
                                                 (and (= "Paragraph" (first prev-block))
                                                      (seq (set/intersection (set (flatten prev-block)) #{"Deadline" "Scheduled"}))))))
                       pos-meta' (if cut-multiline?
-                                 pos-meta
+                                  pos-meta
                                   ;; fix start_pos
                                   (assoc pos-meta :end_pos
                                          (if (seq headings)
@@ -699,11 +699,11 @@
                       ;; Remove properties text from custom queries in db graphs
                       options' (assoc options
                                       :remove-properties?
-                                      (and db-graph-mode?
+                                      (and export-to-db-graph?
                                            (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 (or db-graph-mode? export-to-db-graph?)
                                 block'
                                 (assoc block' :macros (extract-macros-from-ast (cons block body))))]
                   (recur (conj headings block'') (rest blocks) (inc block-idx) {} {} []))

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

@@ -888,7 +888,7 @@
         extract-options' (merge {:block-pattern (common-config/get-block-pattern format)
                                  :date-formatter "MMM do, yyyy"
                                  :uri-encoded? false
-                                 :db-graph-mode? true
+                                 :export-to-db-graph? true
                                  :filename-format :legacy}
                                 extract-options
                                 {:db db})]

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

@@ -350,8 +350,8 @@
       (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"))))
-      (is (is (re-find #"(?s)^Text before\n.*BEGIN_QUERY.*END_QUERY\nText after$"
-                       (:block/title (find-block-by-content @conn #":title \"tasks"))))))
+      (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"
       (let [block (find-block-by-content @conn "old todo block")]