瀏覽代碼

Move worker.mldoc and worker.file.property.util to graph parser dep

Tienson Qin 1 年之前
父節點
當前提交
faf2d8116a

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

@@ -767,3 +767,56 @@
                                [others parents' result'])))]
                      (recur blocks parents result))))]
     (concat result other-blocks)))
+
+(defn extract-plain
+  "Extract plain elements including page refs"
+  [repo content]
+  (let [ast (gp-mldoc/->edn repo content :markdown)
+        *result (atom [])]
+    (walk/prewalk
+     (fn [f]
+       (cond
+           ;; tag
+         (and (vector? f)
+              (= "Tag" (first f)))
+         nil
+
+           ;; nested page ref
+         (and (vector? f)
+              (= "Nested_link" (first f)))
+         (swap! *result conj (:content (second f)))
+
+           ;; page ref
+         (and (vector? f)
+              (= "Link" (first f))
+              (map? (second f))
+              (vector? (:url (second f)))
+              (= "Page_ref" (first (:url (second f)))))
+         (swap! *result conj
+                (:full_text (second f)))
+
+           ;; plain
+         (and (vector? f)
+              (= "Plain" (first f)))
+         (swap! *result conj (second f))
+
+         :else
+         f))
+     ast)
+    (-> (string/trim (apply str @*result))
+        text/page-ref-un-brackets!)))
+
+(defn extract-refs-from-text
+  [repo db text date-formatter]
+  (when (string? text)
+    (let [ast-refs (gp-mldoc/get-references text (gp-mldoc/get-default-config repo :markdown))
+          page-refs (map #(get-page-reference % :markdown) ast-refs)
+          block-refs (map get-block-reference ast-refs)
+          refs' (->> (concat page-refs block-refs)
+                     (remove string/blank?)
+                     distinct)]
+      (-> (map #(if (common-util/uuid-string? %)
+                  {:block/uuid (uuid %)}
+                  (page-name->map % true db true date-formatter))
+               refs')
+          set))))

+ 54 - 15
deps/graph-parser/src/logseq/graph_parser/mldoc.cljc

@@ -14,7 +14,8 @@
             [clojure.string :as string]
             [logseq.common.util :as common-util]
             [logseq.common.config :as common-config]
-            [logseq.graph-parser.schema.mldoc :as mldoc-schema]))
+            [logseq.graph-parser.schema.mldoc :as mldoc-schema]
+            [logseq.db.sqlite.util :as sqlite-util]))
 
 (defonce parseJson (gobj/get Mldoc "parseJson"))
 (defonce parseInlineJson (gobj/get Mldoc "parseInlineJson"))
@@ -136,22 +137,35 @@
         (cons [["Properties" properties] nil] other-ast)
         original-ast))))
 
+(defn get-default-config
+  "Gets a mldoc default config for the given format. Works for DB and file graphs"
+  [repo format]
+  (let [db-based? (sqlite-util/db-based-graph? repo)]
+    (->>
+     (cond-> (default-config-map format)
+       db-based?
+       (assoc :enable_drawers false))
+     bean/->js
+     js/JSON.stringify)))
+
 (defn ->edn
   {:malli/schema [:=> [:cat :string :string] mldoc-schema/block-ast-with-pos-coll-schema]}
-  [content config]
-  (if (string? content)
-    (try
-      (if (string/blank? content)
-        []
-        (-> content
-            (parse-json config)
-            (common-util/json->clj)
-            (update-src-full-content content)
-            (collect-page-properties config)))
-      (catch :default e
-        (log/error :unexpected-error e)
-        []))
-    (log/error :edn/wrong-content-type content)))
+  ([content config]
+   (if (string? content)
+     (try
+       (if (string/blank? content)
+         []
+         (-> content
+             (parse-json config)
+             (common-util/json->clj)
+             (update-src-full-content content)
+             (collect-page-properties config)))
+       (catch :default e
+         (log/error :unexpected-error e)
+         []))
+     (log/error :edn/wrong-content-type content)))
+  ([repo content format]
+   (->edn content (get-default-config repo format))))
 
 (defn inline->edn
   [text config]
@@ -195,3 +209,28 @@
      (let [result' (first result)]
        (or (contains? #{"Nested_link"} (first result'))
            (contains? #{"Page_ref" "Block_ref" "Complex"} (first (:url (second result')))))))))
+
+(defn properties?
+  [ast]
+  (contains? #{"Properties" "Property_Drawer"} (ffirst ast)))
+
+(defn block-with-title?
+  [type]
+  (contains? #{"Paragraph"
+               "Raw_Html"
+               "Hiccup"
+               "Heading"} type))
+
+(defn- has-title?
+  [repo content format]
+  (let [ast (->edn repo content format)]
+    (block-with-title? (ffirst (map first ast)))))
+
+(defn get-title&body
+  "parses content and returns [title body]
+   returns nil if no title"
+  [repo content format]
+  (let [lines (string/split-lines content)]
+    (if (has-title? repo content format)
+      [(first lines) (string/join "\n" (rest lines))]
+      [nil (string/join "\n" lines)])))

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

@@ -4,7 +4,8 @@
             [clojure.string :as string]
             [clojure.set :as set]
             [goog.string :as gstring]
-            [goog.string.format]))
+            [goog.string.format]
+            [logseq.graph-parser.mldoc :as gp-mldoc]))
 
 (def colons "Property delimiter for markdown mode" "::")
 (defn colons-org
@@ -158,3 +159,174 @@
           (string/join "\n" lines))
         content))
     content))
+
+(defn- build-properties-str
+  [format properties]
+  (when (seq properties)
+    (let [org? (= format :org)
+          kv-format (if org? ":%s: %s" (str "%s" colons " %s"))
+          full-format (if org? ":PROPERTIES:\n%s\n:END:" "%s\n")
+          properties-content (->> (map (fn [[k v]] (common-util/format kv-format (name k) v)) properties)
+                                  (string/join "\n"))]
+      (common-util/format full-format properties-content))))
+
+(defn simplified-property?
+  [line]
+  (boolean
+   (and (string? line)
+        (re-find (re-pattern (str "^\\s?[^ ]+" colons)) line))))
+
+(defn- front-matter-property?
+  [line]
+  (boolean
+   (and (string? line)
+        (common-util/safe-re-find #"^\s*[^ ]+:" line))))
+
+(defn insert-property
+  "Only accept nake content (without any indentation)"
+  ([repo format content key value]
+   (insert-property repo format content key value false))
+  ([repo format content key value front-matter?]
+   (when (string? content)
+     (let [ast (gp-mldoc/->edn repo content format)
+           title? (gp-mldoc/block-with-title? (ffirst (map first ast)))
+           has-properties? (or (and title?
+                                    (or (gp-mldoc/properties? (second ast))
+                                        (gp-mldoc/properties? (second
+                                                            (remove
+                                                             (fn [[x _]]
+                                                               (contains? #{"Hiccup" "Raw_Html"} (first x)))
+                                                             ast)))))
+                               (gp-mldoc/properties? (first ast)))
+           lines (string/split-lines content)
+           [title body] (gp-mldoc/get-title&body repo content format)
+           scheduled (filter #(string/starts-with? % "SCHEDULED") lines)
+           deadline (filter #(string/starts-with? % "DEADLINE") lines)
+           body-without-timestamps (filter
+                                    #(not (or (string/starts-with? % "SCHEDULED")
+                                              (string/starts-with? % "DEADLINE")))
+                                    (string/split-lines body))
+           org? (= :org format)
+           key (string/lower-case (name key))
+           value (string/trim (str value))
+           start-idx (.indexOf lines properties-start)
+           end-idx (.indexOf lines properties-end)
+           result (cond
+                    (and org? (not has-properties?))
+                    (let [properties (build-properties-str format {key value})]
+                      (if title
+                        (string/join "\n" (concat [title] scheduled deadline [properties] body-without-timestamps))
+                        (str properties "\n" content)))
+
+                    (and has-properties? (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
+                    (let [exists? (atom false)
+                          before (subvec lines 0 start-idx)
+                          middle (doall
+                                  (->> (subvec lines (inc start-idx) end-idx)
+                                       (mapv (fn [text]
+                                               (let [[k v] (common-util/split-first ":" (subs text 1))]
+                                                 (if (and k v)
+                                                   (let [key-exists? (= k key)
+                                                         _ (when key-exists? (reset! exists? true))
+                                                         v (if key-exists? value v)]
+                                                     (str ":" k ": "  (string/trim v)))
+                                                   text))))))
+                          middle (if @exists? middle (conj middle (str ":" key ": "  value)))
+                          after (subvec lines (inc end-idx))
+                          lines (concat before [properties-start] middle [properties-end] after)]
+                      (string/join "\n" lines))
+
+                    (not org?)
+                    (let [exists? (atom false)
+                          sym (if front-matter? ": " (str colons " "))
+                          new-property-s (str key sym value)
+                          property-f (if front-matter? front-matter-property? simplified-property?)
+                          groups (partition-by property-f lines)
+                          compose-lines (fn []
+                                          (mapcat (fn [lines]
+                                                    (if (property-f (first lines))
+                                                      (let [lines (doall
+                                                                   (mapv (fn [text]
+                                                                           (let [[k v] (common-util/split-first sym text)]
+                                                                             (if (and k v)
+                                                                               (let [key-exists? (= k key)
+                                                                                     _ (when key-exists? (reset! exists? true))
+                                                                                     v (if key-exists? value v)]
+                                                                                 (str k sym  (string/trim v)))
+                                                                               text)))
+                                                                         lines))
+                                                            lines (if @exists? lines (conj lines new-property-s))]
+                                                        lines)
+                                                      lines))
+                                                  groups))
+                          lines (cond
+                                  has-properties?
+                                  (compose-lines)
+
+                                  title?
+                                  (cons (first lines) (cons new-property-s (rest lines)))
+
+                                  :else
+                                  (cons new-property-s lines))]
+                      (string/join "\n" lines))
+
+                    :else
+                    content)]
+       (string/trimr result)))))
+
+(defn remove-property
+  ([format key content]
+   (remove-property format key content true))
+  ([format key content first?]
+   (when (not (string/blank? (name key)))
+     (let [format (or format :markdown)
+           key (string/lower-case (name key))
+           remove-f (if first? common-util/remove-first remove)]
+       (if (and (= format :org) (not (contains-properties? content)))
+         content
+         (let [lines (->> (string/split-lines content)
+                          (remove-f (fn [line]
+                                      (let [s (string/triml (string/lower-case line))]
+                                        (or (string/starts-with? s (str ":" key ":"))
+                                            (string/starts-with? s (str key colons " ")))))))]
+           (string/join "\n" lines)))))))
+
+(defn remove-properties
+  [format content]
+  (cond
+    (contains-properties? content)
+    (let [lines (string/split-lines content)
+          [title-lines properties&body] (split-with #(-> (string/triml %)
+                                                         string/upper-case
+                                                         (string/starts-with? properties-start)
+                                                         not)
+                                                    lines)
+          body (drop-while #(-> (string/trim %)
+                                string/upper-case
+                                (string/starts-with? properties-end)
+                                not
+                                (or (string/blank? %)))
+                           properties&body)
+          body (if (and (seq body)
+                        (-> (first body)
+                            string/triml
+                            string/upper-case
+                            (string/starts-with? properties-end)))
+                 (let [line (string/replace (first body) #"(?i):END:\s?" "")]
+                   (if (string/blank? line)
+                     (rest body)
+                     (cons line (rest body))))
+                 body)]
+      (->> (concat title-lines body)
+           (string/join "\n")))
+
+    (not= format :org)
+    (let [lines (string/split-lines content)
+          lines (if (simplified-property? (first lines))
+                  (drop-while simplified-property? lines)
+                  (cons (first lines)
+                        (drop-while simplified-property? (rest lines))))]
+      (string/join "\n" lines))
+
+    :else
+    content))

+ 6 - 6
src/main/frontend/format/mldoc.cljs

@@ -12,7 +12,7 @@
             [logseq.graph-parser.text :as text]
             [logseq.graph-parser.block :as gp-block]
             [clojure.walk :as walk]
-            [frontend.worker.mldoc :as worker-mldoc]))
+            [logseq.graph-parser.mldoc :as gp-mldoc]))
 
 (defonce anchorLink (gobj/get Mldoc "anchorLink"))
 (defonce parseOPML (gobj/get Mldoc "parseOPML"))
@@ -37,7 +37,7 @@
                       title
                       (or references gp-mldoc/default-references)))
 
-(def block-with-title? worker-mldoc/block-with-title?)
+(def block-with-title? gp-mldoc/block-with-title?)
 
 (defn opml->edn
   [config content]
@@ -52,11 +52,11 @@
 
 (defn get-default-config
   [format]
-  (worker-mldoc/get-default-config (state/get-current-repo) format))
+  (gp-mldoc/get-default-config (state/get-current-repo) format))
 
 (defn ->edn
   [content format]
-  (worker-mldoc/->edn (state/get-current-repo) content format))
+  (gp-mldoc/->edn (state/get-current-repo) content format))
 
 (defrecord MldocMode []
   protocol/Format
@@ -73,7 +73,7 @@
   [plains]
   (string/join (map last plains)))
 
-(def properties? worker-mldoc/properties?)
+(def properties? gp-mldoc/properties?)
 
 (defn typ-drawer?
   [ast typ]
@@ -120,4 +120,4 @@
    returns nil if no title"
   [content format]
   (when-let [repo (state/get-current-repo)]
-    (worker-mldoc/get-title&body repo content format)))
+    (gp-mldoc/get-title&body repo content format)))

+ 5 - 6
src/main/frontend/handler/file_based/property/util.cljs

@@ -10,8 +10,7 @@
             [logseq.graph-parser.text :as text]
             [frontend.db :as db]
             [frontend.state :as state]
-            [frontend.util.cursor :as cursor]
-            [frontend.worker.file.property-util :as wpu]))
+            [frontend.util.cursor :as cursor]))
 
 (defn hidden-properties
   "These are properties hidden from user including built-in ones and ones
@@ -35,7 +34,7 @@
                     "")
     content))
 
-(def simplified-property? wpu/simplified-property?)
+(def simplified-property? gp-property/simplified-property?)
 
 (defn- get-property-key
   [line format]
@@ -107,7 +106,7 @@
   (let [from (cursor/pos input)]
     (cursor/move-cursor-to-thing input properties-end from)))
 
-(def remove-properties wpu/remove-properties)
+(def remove-properties gp-property/remove-properties)
 
 ;; title properties body
 (defn with-built-in-properties
@@ -162,7 +161,7 @@
    (insert-property format content key value false))
   ([format content key value front-matter?]
    (let [repo (state/get-current-repo)]
-     (wpu/insert-property repo format content key value front-matter?))))
+     (gp-property/insert-property repo format content key value front-matter?))))
 
 (defn insert-properties
   [format content kvs]
@@ -182,7 +181,7 @@
        (insert-property format content k v)))
    content kvs))
 
-(def remove-property wpu/remove-property)
+(def remove-property gp-property/remove-property)
 
 (defn remove-id-property
   [format content]

+ 8 - 9
src/main/frontend/modules/outliner/core.cljs

@@ -10,12 +10,11 @@
             [logseq.common.util :as common-util]
             [cljs.spec.alpha :as s]
             [logseq.db :as ldb]
-            [frontend.worker.mldoc :as mldoc]
-            [frontend.worker.file.property-util :as wpu]
+            [logseq.graph-parser.block :as gp-block]
+            [logseq.graph-parser.property :as gp-property]
             [logseq.db.frontend.property :as db-property]
             [logseq.db.sqlite.util :as sqlite-util]
-            [cljs.pprint :as pprint]
-            [frontend.worker.util :as util]))
+            [cljs.pprint :as pprint]))
 
 (s/def ::block-map (s/keys :opt [:db/id :block/uuid :block/page :block/left :block/parent]))
 
@@ -150,7 +149,7 @@
    [repo conn db date-formatter txs-state block-entity m tags-has-class?]
    (if tags-has-class?
      (let [content (state/get-edit-content)
-           linked-page (some-> content #(mldoc/extract-plain repo %))
+           linked-page (some-> content #(gp-block/extract-plain repo %))
            sanity-linked-page (some-> linked-page util/page-name-sanity-lc)
            linking-page? (and (not (string/blank? sanity-linked-page))
                               @(:editor/create-page? @state/state))]
@@ -201,10 +200,10 @@
                                                    [v])))
 
                                              (and (coll? v) (string? (first v)))
-                                             (mapcat #(mldoc/extract-refs-from-text repo db % date-formatter) v)
+                                             (mapcat #(gp-block/extract-refs-from-text repo db % date-formatter) v)
 
                                              (string? v)
-                                             (mldoc/extract-refs-from-text repo db v date-formatter)
+                                             (gp-block/extract-refs-from-text repo db v date-formatter)
 
                                              :else
                                              nil))))
@@ -213,7 +212,7 @@
                            (remove (fn [b] (nil? (d/entity db [:block/uuid (:block/uuid b)])))))
         content-refs (when-not skip-content-parsing?
                        (when-let [content (:block/content block)]
-                         (mldoc/extract-refs-from-text repo db content date-formatter)))]
+                         (gp-block/extract-refs-from-text repo db content date-formatter)))]
     (concat property-refs content-refs)))
 
 (defn- rebuild-refs
@@ -561,7 +560,7 @@
            (update :block/properties assoc k list-type)
 
            (not (sqlite-util/db-based-graph? repo))
-           (assoc :block/content (wpu/insert-property repo format content :logseq.order-list-type list-type))))
+           (assoc :block/content (gp-property/insert-property repo format content :logseq.order-list-type list-type))))
        blocks)
       blocks)))
 

+ 2 - 2
src/main/frontend/modules/outliner/datascript.cljs

@@ -2,7 +2,7 @@
   (:require [logseq.common.util :as common-util]
             [logseq.graph-parser.util.block-ref :as block-ref]
             [logseq.db.sqlite.util :as sqlite-util]
-            [frontend.worker.file.property-util :as wpu]
+            [logseq.graph-parser.property :as gp-property]
             [datascript.core :as d]
             [clojure.string :as string]))
 
@@ -38,7 +38,7 @@
                               (let [refs (:block/_refs block)]
                                 (map (fn [ref]
                                        (let [id (:db/id ref)
-                                             block-content (wpu/remove-properties
+                                             block-content (gp-property/remove-properties
                                                             (:block/format block) (:block/content block))
                                              new-content (some-> (:block/content ref)
                                                                  (string/replace (re-pattern (common-util/format "(?i){{embed \\(\\(%s\\)\\)\\s?}}" (str (:block/uuid block))))

+ 4 - 4
src/main/frontend/worker/file/core.cljs

@@ -2,7 +2,7 @@
   "Save file to disk"
   (:require [clojure.string :as string]
             [frontend.worker.file.util :as wfu]
-            [frontend.worker.file.property-util :as property-util]
+            [logseq.graph-parser.property :as gp-property]
             [logseq.common.path :as path]
             [datascript.core :as d]
             [logseq.db :as ldb]
@@ -21,11 +21,11 @@
   [repo format content collapsed?]
   (cond
     collapsed?
-    (property-util/insert-property repo format content :collapsed true)
+    (gp-property/insert-property repo format content :collapsed true)
 
     ;; Don't check properties. Collapsed is an internal state log as property in file, but not counted into properties
     (false? collapsed?)
-    (property-util/remove-property format :collapsed content)
+    (gp-property/remove-property format :collapsed content)
 
     :else
     content))
@@ -84,7 +84,7 @@
                               " ")]
                     (str prefix sep new-content)))
         content (if block-ref-not-saved?
-                  (property-util/insert-property repo format content :id (str (:block/uuid b)))
+                  (gp-property/insert-property repo format content :id (str (:block/uuid b)))
                   content)]
     content))
 

+ 0 - 178
src/main/frontend/worker/file/property_util.cljs

@@ -1,178 +0,0 @@
-(ns frontend.worker.file.property-util
-  "Property fns needed by the rest of the app and not graph-parser"
-  (:require [clojure.string :as string]
-            [logseq.common.util :as common-util]
-            [logseq.graph-parser.property :as gp-property :refer [properties-start properties-end]]
-            [frontend.worker.mldoc :as worker-mldoc]
-            [frontend.worker.util :as util]))
-
-(defn- build-properties-str
-  [format properties]
-  (when (seq properties)
-    (let [org? (= format :org)
-          kv-format (if org? ":%s: %s" (str "%s" gp-property/colons " %s"))
-          full-format (if org? ":PROPERTIES:\n%s\n:END:" "%s\n")
-          properties-content (->> (map (fn [[k v]] (common-util/format kv-format (name k) v)) properties)
-                                  (string/join "\n"))]
-      (common-util/format full-format properties-content))))
-
-(defn simplified-property?
-  [line]
-  (boolean
-   (and (string? line)
-        (re-find (re-pattern (str "^\\s?[^ ]+" gp-property/colons)) line))))
-
-(defn- front-matter-property?
-  [line]
-  (boolean
-   (and (string? line)
-        (common-util/safe-re-find #"^\s*[^ ]+:" line))))
-
-(defn insert-property
-  "Only accept nake content (without any indentation)"
-  ([repo format content key value]
-   (insert-property repo format content key value false))
-  ([repo format content key value front-matter?]
-   (when (string? content)
-     (let [ast (worker-mldoc/->edn repo content format)
-           title? (worker-mldoc/block-with-title? (ffirst (map first ast)))
-           has-properties? (or (and title?
-                                    (or (worker-mldoc/properties? (second ast))
-                                        (worker-mldoc/properties? (second
-                                                            (remove
-                                                             (fn [[x _]]
-                                                               (contains? #{"Hiccup" "Raw_Html"} (first x)))
-                                                             ast)))))
-                               (worker-mldoc/properties? (first ast)))
-           lines (string/split-lines content)
-           [title body] (worker-mldoc/get-title&body repo content format)
-           scheduled (filter #(string/starts-with? % "SCHEDULED") lines)
-           deadline (filter #(string/starts-with? % "DEADLINE") lines)
-           body-without-timestamps (filter
-                                    #(not (or (string/starts-with? % "SCHEDULED")
-                                              (string/starts-with? % "DEADLINE")))
-                                    (string/split-lines body))
-           org? (= :org format)
-           key (string/lower-case (name key))
-           value (string/trim (str value))
-           start-idx (.indexOf lines properties-start)
-           end-idx (.indexOf lines properties-end)
-           result (cond
-                    (and org? (not has-properties?))
-                    (let [properties (build-properties-str format {key value})]
-                      (if title
-                        (string/join "\n" (concat [title] scheduled deadline [properties] body-without-timestamps))
-                        (str properties "\n" content)))
-
-                    (and has-properties? (>= start-idx 0) (> end-idx 0) (> end-idx start-idx))
-                    (let [exists? (atom false)
-                          before (subvec lines 0 start-idx)
-                          middle (doall
-                                  (->> (subvec lines (inc start-idx) end-idx)
-                                       (mapv (fn [text]
-                                               (let [[k v] (common-util/split-first ":" (subs text 1))]
-                                                 (if (and k v)
-                                                   (let [key-exists? (= k key)
-                                                         _ (when key-exists? (reset! exists? true))
-                                                         v (if key-exists? value v)]
-                                                     (str ":" k ": "  (string/trim v)))
-                                                   text))))))
-                          middle (if @exists? middle (conj middle (str ":" key ": "  value)))
-                          after (subvec lines (inc end-idx))
-                          lines (concat before [properties-start] middle [properties-end] after)]
-                      (string/join "\n" lines))
-
-                    (not org?)
-                    (let [exists? (atom false)
-                          sym (if front-matter? ": " (str gp-property/colons " "))
-                          new-property-s (str key sym value)
-                          property-f (if front-matter? front-matter-property? simplified-property?)
-                          groups (partition-by property-f lines)
-                          compose-lines (fn []
-                                          (mapcat (fn [lines]
-                                                    (if (property-f (first lines))
-                                                      (let [lines (doall
-                                                                   (mapv (fn [text]
-                                                                           (let [[k v] (common-util/split-first sym text)]
-                                                                             (if (and k v)
-                                                                               (let [key-exists? (= k key)
-                                                                                     _ (when key-exists? (reset! exists? true))
-                                                                                     v (if key-exists? value v)]
-                                                                                 (str k sym  (string/trim v)))
-                                                                               text)))
-                                                                         lines))
-                                                            lines (if @exists? lines (conj lines new-property-s))]
-                                                        lines)
-                                                      lines))
-                                                  groups))
-                          lines (cond
-                                  has-properties?
-                                  (compose-lines)
-
-                                  title?
-                                  (cons (first lines) (cons new-property-s (rest lines)))
-
-                                  :else
-                                  (cons new-property-s lines))]
-                      (string/join "\n" lines))
-
-                    :else
-                    content)]
-       (string/trimr result)))))
-
-(defn remove-property
-  ([format key content]
-   (remove-property format key content true))
-  ([format key content first?]
-   (when (not (string/blank? (name key)))
-     (let [format (or format :markdown)
-           key (string/lower-case (name key))
-           remove-f (if first? common-util/remove-first remove)]
-       (if (and (= format :org) (not (gp-property/contains-properties? content)))
-         content
-         (let [lines (->> (string/split-lines content)
-                          (remove-f (fn [line]
-                                      (let [s (string/triml (string/lower-case line))]
-                                        (or (string/starts-with? s (str ":" key ":"))
-                                            (string/starts-with? s (str key gp-property/colons " ")))))))]
-           (string/join "\n" lines)))))))
-
-(defn remove-properties
-  [format content]
-  (cond
-    (gp-property/contains-properties? content)
-    (let [lines (string/split-lines content)
-          [title-lines properties&body] (split-with #(-> (string/triml %)
-                                                         string/upper-case
-                                                         (string/starts-with? properties-start)
-                                                         not)
-                                                    lines)
-          body (drop-while #(-> (string/trim %)
-                                string/upper-case
-                                (string/starts-with? properties-end)
-                                not
-                                (or (string/blank? %)))
-                           properties&body)
-          body (if (and (seq body)
-                        (-> (first body)
-                            string/triml
-                            string/upper-case
-                            (string/starts-with? properties-end)))
-                 (let [line (string/replace (first body) #"(?i):END:\s?" "")]
-                   (if (string/blank? line)
-                     (rest body)
-                     (cons line (rest body))))
-                 body)]
-      (->> (concat title-lines body)
-           (string/join "\n")))
-
-    (not= format :org)
-    (let [lines (string/split-lines content)
-          lines (if (simplified-property? (first lines))
-                  (drop-while simplified-property? lines)
-                  (cons (first lines)
-                        (drop-while simplified-property? (rest lines))))]
-      (string/join "\n" lines))
-
-    :else
-    content))

+ 0 - 104
src/main/frontend/worker/mldoc.cljs

@@ -1,104 +0,0 @@
-(ns frontend.worker.mldoc
-  "Mldoc related fns"
-  (:require [logseq.graph-parser.mldoc :as gp-mldoc]
-            [cljs-bean.core :as bean]
-            [logseq.db.sqlite.util :as sqlite-util]
-            [clojure.string :as string]
-            [logseq.graph-parser.text :as text]
-            [clojure.walk :as walk]
-            [logseq.graph-parser.block :as gp-block]
-            [logseq.common.util :as common-util]))
-
-(defn get-default-config
-  "Gets a mldoc default config for the given format. Works for DB and file graphs"
-  [repo format]
-  (let [db-based? (sqlite-util/db-based-graph? repo)]
-    (->>
-     (cond-> (gp-mldoc/default-config-map format)
-       db-based?
-       (assoc :enable_drawers false))
-     bean/->js
-     js/JSON.stringify)))
-
-(defn ->edn
-  "Wrapper around gp-mldoc/->edn that builds mldoc config given a format"
-  [repo content format]
-  (gp-mldoc/->edn content (get-default-config repo format)))
-
-(defn properties?
-  [ast]
-  (contains? #{"Properties" "Property_Drawer"} (ffirst ast)))
-
-(defn block-with-title?
-  [type]
-  (contains? #{"Paragraph"
-               "Raw_Html"
-               "Hiccup"
-               "Heading"} type))
-
-(defn- has-title?
-  [repo content format]
-  (let [ast (->edn repo content format)]
-    (block-with-title? (ffirst (map first ast)))))
-
-(defn get-title&body
-  "parses content and returns [title body]
-   returns nil if no title"
-  [repo content format]
-  (let [lines (string/split-lines content)]
-    (if (has-title? repo content format)
-      [(first lines) (string/join "\n" (rest lines))]
-      [nil (string/join "\n" lines)])))
-
-(defn extract-plain
-  "Extract plain elements including page refs"
-  [repo content]
-  (let [ast (->edn repo content :markdown)
-        *result (atom [])]
-    (walk/prewalk
-     (fn [f]
-       (cond
-           ;; tag
-         (and (vector? f)
-              (= "Tag" (first f)))
-         nil
-
-           ;; nested page ref
-         (and (vector? f)
-              (= "Nested_link" (first f)))
-         (swap! *result conj (:content (second f)))
-
-           ;; page ref
-         (and (vector? f)
-              (= "Link" (first f))
-              (map? (second f))
-              (vector? (:url (second f)))
-              (= "Page_ref" (first (:url (second f)))))
-         (swap! *result conj
-                (:full_text (second f)))
-
-           ;; plain
-         (and (vector? f)
-              (= "Plain" (first f)))
-         (swap! *result conj (second f))
-
-         :else
-         f))
-     ast)
-    (-> (string/trim (apply str @*result))
-        text/page-ref-un-brackets!)))
-
-(defn extract-refs-from-text
-  [repo db text date-formatter]
-  (when (string? text)
-    (let [ast-refs (gp-mldoc/get-references text (get-default-config repo :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)
-                     (remove string/blank?)
-                     distinct)]
-      (-> (map #(if (common-util/uuid-string? %)
-                  {:block/uuid (uuid %)}
-                  (gp-block/page-name->map % true db true date-formatter))
-               refs')
-          set))))

+ 2 - 2
src/test/frontend/format/mldoc_test.cljs

@@ -1,11 +1,11 @@
 (ns frontend.format.mldoc-test
-  (:require [frontend.worker.mldoc :as worker-mldoc]
+  (:require [logseq.graph-parser.block :as gp-block]
             [cljs.test :refer [deftest testing are]]
             [frontend.test.helper :as test-helper]))
 
 (deftest test-extract-plain
   (testing "normalize date values"
-    (are [x y] (= (worker-mldoc/extract-plain test-helper/test-db x) y)
+    (are [x y] (= (gp-block/extract-plain test-helper/test-db x) y)
       "foo #book #[[nice test]]"
       "foo"
 

+ 3 - 3
src/test/frontend/util/property_test.cljs

@@ -1,7 +1,7 @@
 (ns frontend.util.property-test
   (:require [cljs.test :refer [are deftest testing]]
             [frontend.handler.file-based.property.util :as property-util]
-            [frontend.worker.file.property-util :as wfp]))
+            [logseq.graph-parser.property :as gp-property]))
 
 (deftest remove-id-property
   (testing "org"
@@ -168,14 +168,14 @@ SCHEDULED: <2021-10-25 Mon>\n:PROPERTIES:\n:a: b\n:END:\nworld\n" "c" "d")
     "abcd\nempty::\nanother-empty::\nid:: 123"))
 
 (deftest test-build-properties-str
-  (are [x y] (= (#'wfp/build-properties-str :mardown x) y)
+  (are [x y] (= (#'gp-property/build-properties-str :mardown x) y)
     {:title "a"}
     "title:: a\n"
     {:title "a/b/c"}
     "title:: a/b/c\n"
     {:title "a/b/c" :tags "d,e"}
     "title:: a/b/c\ntags:: d,e\n")
-  (are [x y] (= (#'wfp/build-properties-str :org x) y)
+  (are [x y] (= (#'gp-property/build-properties-str :org x) y)
     {:title "a"}
     ":PROPERTIES:\n:title: a\n:END:"
     {:title "a/b/c"}