Explorar o código

fix: update all db graph uses of mldoc to use db config

Left a few uses of gp-mldoc/default-config but only in file graph
specific locations or functionality that wasn't related to db graphs
e.g. handler. plugin. Also removed format/get-default-config as it was unused

Part of LOG-2741
Gabriel Horner %!s(int64=2) %!d(string=hai) anos
pai
achega
3f773ed876

+ 2 - 2
src/main/frontend/components/block.cljs

@@ -961,7 +961,7 @@
    (inline-text {} format v))
   ([config format v]
    (when (string? v)
-     (let [inline-list (gp-mldoc/inline->edn v (gp-mldoc/default-config format))]
+     (let [inline-list (gp-mldoc/inline->edn v (mldoc/get-default-config format))]
        [:div.inline.mr-1 (map-inline config inline-list)]))))
 
 (defn- render-macro
@@ -969,7 +969,7 @@
   [:div.macro {:data-macro-name name}
 
    (if macro-content
-     (let [ast (->> (mldoc/->edn macro-content (gp-mldoc/default-config format))
+     (let [ast (->> (mldoc/->edn macro-content format)
                     (map first))
            paragraph? (and (= 1 (count ast))
                            (= "Paragraph" (ffirst ast)))]

+ 2 - 1
src/main/frontend/components/page.cljs

@@ -23,6 +23,7 @@
             [frontend.extensions.graph :as graph]
             [frontend.extensions.pdf.utils :as pdf-utils]
             [frontend.format.block :as block]
+            [frontend.format.mldoc :as mldoc]
             [frontend.handler.common :as common-handler]
             [frontend.handler.config :as config-handler]
             [frontend.handler.dnd :as dnd]
@@ -502,7 +503,7 @@
             (let [nested? (and (string/includes? title page-ref/left-brackets)
                                (string/includes? title page-ref/right-brackets))]
               (cond untitled? [:span.opacity-50 (t :untitled)]
-                    nested? (component-block/map-inline {} (gp-mldoc/inline->edn title (gp-mldoc/default-config
+                    nested? (component-block/map-inline {} (gp-mldoc/inline->edn title (mldoc/get-default-config
                                                                                         (:block/format page))))
                     :else title))])]
         (when (and db-based? (seq (:block/tags page)))

+ 10 - 23
src/main/frontend/format.cljs

@@ -18,29 +18,16 @@ and edn. Can handle org-mode and markdown formats"
     mldoc-record
     nil))
 
-;; html
-(defn get-default-config
-  ([format]
-   (gp-mldoc/default-config format))
-  ([format options]
-   (gp-mldoc/default-config format options)))
-
 (defn to-html
-  ([content format]
-   (to-html content format (get-default-config format)))
-  ([content format config]
-   (let [config (if config config (get-default-config format))]
-     (if (string/blank? content)
-       ""
-       (if-let [record (get-format-record format)]
-         (protocol/toHtml record content config gp-mldoc/default-references)
-         content)))))
+  [content format config]
+  (if (string/blank? content)
+    ""
+    (if-let [record (get-format-record format)]
+      (protocol/toHtml record content config gp-mldoc/default-references)
+      content)))
 
 (defn to-edn
-  ([content format]
-   (to-edn content format (get-default-config format)))
-  ([content format config]
-   (let [config (or config (get-default-config format))]
-     (if-let [record (get-format-record format)]
-       (protocol/toEdn record content config)
-       nil))))
+  [content format config]
+  (if-let [record (get-format-record format)]
+    (protocol/toEdn record content config)
+    nil))

+ 5 - 17
src/main/frontend/format/block.cljs

@@ -16,7 +16,7 @@
             [frontend.util :as util]
             [datascript.core :as d]
             [logseq.db.frontend.property :as db-property]
-            [cljs-bean.core :as bean]))
+            [frontend.format.mldoc :as mldoc]))
 
 (defn- update-extracted-block-properties
   "Updates DB graph blocks to ensure that built-in properties are using uuids
@@ -72,12 +72,10 @@ and handles unexpected failure."
   ([original-page-name with-id? with-timestamp?]
    (gp-block/page-name->map original-page-name with-id? (db/get-db (state/get-current-repo)) with-timestamp? (state/get-date-formatter))))
 
-(def ^:private gp-mldoc-config (gp-mldoc/default-config :markdown))
-
 (defn extract-refs-from-text
   [text]
   (when (string? text)
-    (let [ast-refs (gp-mldoc/get-references text gp-mldoc-config)
+    (let [ast-refs (gp-mldoc/get-references text (mldoc/get-default-config :markdown))
           page-refs (map #(gp-block/get-page-reference % :markdown) ast-refs)
           block-refs (map #(gp-block/get-block-reference %) ast-refs)
           refs' (->> (concat page-refs block-refs)
@@ -115,16 +113,6 @@ and handles unexpected failure."
        (remove nil?)
        (first)))
 
-(defn- get-db-based-parse-config
-  [format]
-  (let [db-based? (config/db-based-graph? (state/get-current-repo))]
-    (->>
-     (cond-> (gp-mldoc/default-config-map format)
-       db-based?
-       (assoc :enable_drawers false))
-     bean/->js
-     js/JSON.stringify)))
-
 (defn parse-block
   ([block]
    (parse-block block nil))
@@ -133,12 +121,12 @@ and handles unexpected failure."
    (when-not (string/blank? content)
      (let [block (dissoc block :block/pre-block?)
            format (or format :markdown)
-           parse-config (get-db-based-parse-config format)
+           parse-config (mldoc/get-default-config format)
            ast (format/to-edn content format parse-config)
            blocks (extract-blocks ast content format {:with-id? with-id?})
            new-block (first blocks)
            block (cond->
-                   (merge block new-block)
+                  (merge block new-block)
                    (> (count blocks) 1)
                    (assoc :block/warning :multiple-blocks))
            block (dissoc block :block/title :block/body :block/level)]
@@ -158,7 +146,7 @@ and handles unexpected failure."
                        (str (config/get-block-pattern format) " " (string/triml content)))]
        (if-let [result (state/get-block-ast block-uuid content)]
          result
-         (let [parse-config (get-db-based-parse-config format)
+         (let [parse-config (mldoc/get-default-config format)
                ast (->> (format/to-edn content format parse-config)
                         (map first))
                title (when (gp-block/heading-block? (first ast))

+ 20 - 7
src/main/frontend/format/mldoc.cljs

@@ -3,6 +3,8 @@
   protocol for org and and markdown formats"
   (:require [clojure.string :as string]
             [frontend.format.protocol :as protocol]
+            [frontend.config :as config]
+            [frontend.state :as state]
             [goog.object :as gobj]
             [lambdaisland.glogi :as log]
             ["mldoc" :as mldoc :refer [Mldoc]]
@@ -11,6 +13,7 @@
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.block :as gp-block]
             [clojure.walk :as walk]
+            [cljs-bean.core :as bean]
             [logseq.graph-parser.util.page-ref :as page-ref]))
 
 (defonce anchorLink (gobj/get Mldoc "anchorLink"))
@@ -54,16 +57,26 @@
       (log/error :edn/convert-failed e)
       [])))
 
+(defn get-default-config
+  "Gets a mldoc default config for the given format. Works for DB and file graphs"
+  [format]
+  (let [db-based? (config/db-based-graph? (state/get-current-repo))]
+    (->>
+     (cond-> (gp-mldoc/default-config-map format)
+       db-based?
+       (assoc :enable_drawers false))
+     bean/->js
+     js/JSON.stringify)))
+
 (defn ->edn
-  "Alias to gp-mldoc/->edn but could serve as a wrapper e.g. handle
-  gp-mldoc/default-config"
-  [content config]
-  (gp-mldoc/->edn content config))
+  "Wrapper around gp-mldoc/->edn that builds mldoc config given a format"
+  [content format]
+  (gp-mldoc/->edn content (get-default-config format)))
 
 (defrecord MldocMode []
   protocol/Format
   (toEdn [_this content config]
-    (->edn content config))
+    (gp-mldoc/->edn content config))
   (toHtml [_this content config references]
     (export "html" content config references))
   (exportMarkdown [_this content config references]
@@ -99,7 +112,7 @@
 (defn extract-plain
   "Extract plain elements including page refs"
   [content]
-  (let [ast (->edn content (gp-mldoc/default-config :markdown))
+  (let [ast (->edn content :markdown)
         *result (atom [])]
     (walk/prewalk
      (fn [f]
@@ -137,7 +150,7 @@
 (defn extract-tags
   "Extract tags from content"
   [content]
-  (let [ast (->edn content (gp-mldoc/default-config :markdown))
+  (let [ast (->edn content :markdown)
         *result (atom [])]
     (walk/prewalk
      (fn [f]

+ 2 - 2
src/main/frontend/handler/common/developer.cljs

@@ -7,7 +7,7 @@
             [frontend.ui :as ui]
             [frontend.util.page :as page-util]
             [frontend.handler.property.util :as pu]
-            [logseq.graph-parser.mldoc :as gp-mldoc]))
+            [frontend.format.mldoc :as mldoc]))
 
 ;; Fns used between menus and commands
 (defn show-entity-data
@@ -29,7 +29,7 @@
 
 (defn show-content-ast
   [content format]
-  (let [ast-data (-> (gp-mldoc/->edn content (gp-mldoc/default-config format))
+  (let [ast-data (-> (mldoc/->edn content format)
                      pprint/pprint
                      with-out-str)]
     (println ast-data)

+ 1 - 2
src/main/frontend/handler/db_based/editor.cljs

@@ -8,7 +8,6 @@
             [frontend.format.mldoc :as mldoc]
             [frontend.util :as util]
             [frontend.state :as state]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.handler.ui :as ui-handler]
             [frontend.handler.common.config-edn :as config-edn-common-handler]
@@ -75,7 +74,7 @@
   (let [block (or (and (:db/id block) (db/pull (:db/id block))) block)
         block (if (string/blank? content)
                 block
-                (let [ast (mldoc/->edn (string/trim content) (gp-mldoc/default-config :markdown))
+                (let [ast (mldoc/->edn (string/trim content) :markdown)
                       first-elem-type (first (ffirst ast))
                       block-with-title? (mldoc/block-with-title? first-elem-type)
                       content' (str (config/get-block-pattern :markdown) (if block-with-title? " " "\n") content)

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

@@ -2064,7 +2064,7 @@
                     props (into [] (:properties block))
                     content* (str (if (= :markdown format) "- " "* ")
                                   (property-file/insert-properties-when-file-based repo format content props))
-                    ast (mldoc/->edn content* (gp-mldoc/default-config format))
+                    ast (mldoc/->edn content* format)
                     blocks (->> (block/extract-blocks ast content* format {:page-name page-name})
                                 (map wrap-parse-block))
                     fst-block (first blocks)
@@ -3351,7 +3351,7 @@
   [format content semantic?]
   (and (string/includes? content "\n")
        (if semantic?
-         (let [ast (mldoc/->edn content (gp-mldoc/default-config format))
+         (let [ast (mldoc/->edn content format)
                first-elem-type (first (ffirst ast))]
            (mldoc/block-with-title? first-elem-type))
          true)))
@@ -3379,7 +3379,7 @@
     (when content
       (when (and (string/includes? content "#+BEGIN_QUERY")
                  (string/includes? content "#+END_QUERY"))
-        (let [ast (mldoc/->edn (string/trim content) (gp-mldoc/default-config (or (:block/format entity) :markdown)))
+        (let [ast (mldoc/->edn (string/trim content) (or (:block/format entity) :markdown))
               q (mldoc/extract-first-query-from-ast ast)]
           (some? (:query (gp-util/safe-read-string q))))))))
 

+ 2 - 3
src/main/frontend/handler/export.cljs

@@ -21,7 +21,6 @@
    [frontend.handler.property.file :as property-file]
    [goog.dom :as gdom]
    [lambdaisland.glogi :as log]
-   [logseq.graph-parser.mldoc :as gp-mldoc]
    [logseq.graph-parser.property :as gp-property]
    [logseq.graph-parser.util.block-ref :as block-ref]
    [logseq.graph-parser.util.page-ref :as page-ref]
@@ -192,7 +191,7 @@
   (let [block (db/entity [:block/uuid (uuid block-uuid)])
         block-content (get-blocks-contents repo (:block/uuid block))
         format (:block/format block)
-        ast (mldoc/->edn block-content (gp-mldoc/default-config format))
+        ast (mldoc/->edn block-content format)
         embed-pages-new  (get-embed-pages-from-ast ast)
         embed-blocks-new  (get-embed-blocks-from-ast ast)
         block-refs-new (get-block-refs-from-ast ast)
@@ -227,7 +226,7 @@
   (let [page-name* (util/page-name-sanity-lc page-name)
         page-content (get-page-content repo page-name*)
         format (:block/format (db/entity [:block/name page-name*]))
-        ast (mldoc/->edn page-content (gp-mldoc/default-config format))
+        ast (mldoc/->edn page-content format)
         embed-pages-new (get-embed-pages-from-ast ast)
         embed-blocks-new (get-embed-blocks-from-ast ast)
         block-refs-new (get-block-refs-from-ast ast)

+ 4 - 4
src/main/frontend/handler/export/common.cljs

@@ -7,11 +7,11 @@
             [clojure.string :as string]
             [datascript.core :as d]
             [frontend.db :as db]
+            [frontend.format.mldoc :as mldoc]
             [frontend.modules.file.core :as outliner-file]
             [frontend.modules.outliner.tree :as outliner-tree]
             [frontend.state :as state]
             [frontend.util :as util :refer [concatv mapcatv removev]]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.util :as gp-util]
             [frontend.handler.property.util :as pu]
             [malli.core :as m]
@@ -78,7 +78,7 @@
     (when content
       (removev Properties-block-ast?
                (mapv remove-block-ast-pos
-                     (gp-mldoc/->edn content (gp-mldoc/default-config format)))))))
+                     (mldoc/->edn content format))))))
 
 (defn- block-uuid->ast-with-children
   [block-uuid]
@@ -87,7 +87,7 @@
     (when content
       (removev Properties-block-ast?
                (mapv remove-block-ast-pos
-                     (gp-mldoc/->edn content (gp-mldoc/default-config format)))))))
+                     (mldoc/->edn content format))))))
 
 (defn get-page-content
   ([page-name]
@@ -102,7 +102,7 @@
     (let [format :markdown]
       (removev Properties-block-ast?
                (mapv remove-block-ast-pos
-                     (gp-mldoc/->edn content (gp-mldoc/default-config format)))))))
+                     (mldoc/->edn content format))))))
 
 (defn- update-level-in-block-ast-coll
   [block-ast-coll origin-level]

+ 2 - 2
src/main/frontend/handler/export/html.cljs

@@ -11,7 +11,7 @@
             [frontend.state :as state]
             [frontend.util :as util :refer [concatv mapcatv removev]]
             [hiccups.runtime :as h]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
+            [frontend.format.mldoc :as mldoc]
             [malli.core :as m]))
 
 (def ^:private hiccup-malli-schema
@@ -383,7 +383,7 @@
                                :remove-page-ref-brackets? (contains? remove-options :page-ref)
                                :remove-tags? (contains? remove-options :tag)
                                :keep-only-level<=N (:keep-only-level<=N other-options)}})]
-      (let [ast (util/profile :gp-mldoc/->edn (gp-mldoc/->edn content (gp-mldoc/default-config format)))
+      (let [ast (util/profile :mldoc/->edn (mldoc/->edn content format))
             ast (util/profile :remove-pos (mapv common/remove-block-ast-pos ast))
             ast (removev common/Properties-block-ast? ast)
             keep-level<=n (get-in *state* [:export-options :keep-only-level<=N])

+ 2 - 2
src/main/frontend/handler/export/opml.cljs

@@ -14,7 +14,7 @@
             [frontend.util :as util :refer [concatv mapcatv removev]]
             [goog.dom :as gdom]
             [hiccups.runtime :as h]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
+            [frontend.format.mldoc :as mldoc]
             [promesa.core :as p]))
 
 ;;; *opml-state*
@@ -401,7 +401,7 @@
                                :remove-tags? (contains? remove-options :tag)
                                :keep-only-level<=N (:keep-only-level<=N other-options)}})
               *opml-state* *opml-state*]
-      (let [ast (gp-mldoc/->edn content (gp-mldoc/default-config format))
+      (let [ast (mldoc/->edn content format)
             ast (mapv common/remove-block-ast-pos ast)
             ast (removev common/Properties-block-ast? ast)
             keep-level<=n (get-in *state* [:export-options :keep-only-level<=N])

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

@@ -11,7 +11,7 @@
             [frontend.state :as state]
             [frontend.util :as util :refer [concatv mapcatv removev]]
             [goog.dom :as gdom]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
+            [frontend.format.mldoc :as mldoc]
             [malli.core :as m]
             [promesa.core :as p]))
 
@@ -446,7 +446,7 @@
                                :remove-properties? (contains? remove-options :property)
                                :keep-only-level<=N (:keep-only-level<=N other-options)
                                :newline-after-block (:newline-after-block other-options)}})]
-      (let [ast (gp-mldoc/->edn content (gp-mldoc/default-config format))
+      (let [ast (mldoc/->edn content format)
             ast (mapv common/remove-block-ast-pos ast)
             ast (removev common/Properties-block-ast? ast)
             ast* (common/replace-block&page-reference&embed ast)

+ 1 - 2
src/main/frontend/handler/file_based/editor.cljs

@@ -18,7 +18,6 @@
             [frontend.handler.file-based.property.util :as property-util]
             [logseq.db.frontend.schema :as db-schema]
             [logseq.graph-parser.block :as gp-block]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.util.block-ref :as block-ref]))
 
 (defn- remove-non-existed-refs!
@@ -90,7 +89,7 @@
         content (drawer/with-logbook block content)
         content (with-timetracking block content)
         first-block? (= left page)
-        ast (mldoc/->edn (string/trim content) (gp-mldoc/default-config format))
+        ast (mldoc/->edn (string/trim content) format)
         first-elem-type (first (ffirst ast))
         first-elem-meta (second (ffirst ast))
         properties? (contains? #{"Property_Drawer" "Properties"} first-elem-type)

+ 2 - 3
src/main/frontend/handler/file_based/property/util.cljs

@@ -5,7 +5,6 @@
             [clojure.set :as set]
             [frontend.config :as config]
             [logseq.graph-parser.util :as gp-util]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.property :as gp-property :refer [properties-start properties-end]]
             [logseq.graph-parser.util.page-ref :as page-ref]
             [frontend.format.mldoc :as mldoc]
@@ -175,7 +174,7 @@
         properties (filter (fn [[k _v]] ((built-in-properties) k)) properties)]
     (if (seq properties)
       (let [lines (string/split-lines content)
-            ast (mldoc/->edn content (gp-mldoc/default-config format))
+            ast (mldoc/->edn content format)
             [title body] (if (mldoc/block-with-title? (first (ffirst ast)))
                            [(first lines) (rest lines)]
                            [nil lines])
@@ -221,7 +220,7 @@
    (insert-property format content key value false))
   ([format content key value front-matter?]
    (when (string? content)
-     (let [ast (mldoc/->edn content (gp-mldoc/default-config format))
+     (let [ast (mldoc/->edn content format)
            title? (mldoc/block-with-title? (ffirst (map first ast)))
            has-properties? (or (and title?
                                     (or (mldoc/properties? (second ast))

+ 1 - 2
src/main/frontend/handler/paste.cljs

@@ -3,7 +3,6 @@
             [frontend.db :as db]
             [frontend.format.block :as block]
             [logseq.graph-parser.util :as gp-util]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
             [logseq.graph-parser.block :as gp-block]
             [logseq.graph-parser.util.block-ref :as block-ref]
             [clojure.string :as string]
@@ -25,7 +24,7 @@
   (when-let [editing-block (state/get-edit-block)]
     (let [page-id (:db/id (:block/page editing-block))
           blocks (block/extract-blocks
-                  (mldoc/->edn text (gp-mldoc/default-config format))
+                  (mldoc/->edn text format)
                   text format
                   {:page-name (:block/name (db/entity page-id))})
           blocks' (gp-block/with-parent-and-left page-id blocks)]

+ 4 - 5
src/main/frontend/util/drawer.cljs

@@ -1,9 +1,8 @@
 (ns ^:no-doc frontend.util.drawer
   (:require [clojure.string :as string]
             [frontend.util :as util]
-            [logseq.graph-parser.mldoc :as gp-mldoc]
-            [logseq.graph-parser.property :as gp-property]
-            [frontend.format.mldoc :as mldoc]))
+            [frontend.format.mldoc :as mldoc]
+            [logseq.graph-parser.property :as gp-property]))
 
 (defn drawer-start
   [typ]
@@ -23,7 +22,7 @@
 
 (defn get-drawer-ast
   [format content typ]
-  (let [ast (mldoc/->edn content (gp-mldoc/default-config format))
+  (let [ast (mldoc/->edn content format)
         typ-drawer (ffirst (filter (fn [x]
                                      (mldoc/typ-drawer? x typ)) ast))]
     typ-drawer))
@@ -32,7 +31,7 @@
   [format content typ value]
   (when (string? content)
     (try
-      (let [ast (mldoc/->edn content (gp-mldoc/default-config format))
+      (let [ast (mldoc/->edn content format)
             has-properties? (some (fn [x] (mldoc/properties? x)) ast)
             has-typ-drawer? (some (fn [x] (mldoc/typ-drawer? x typ)) ast)
             lines (string/split-lines content)