Просмотр исходного кода

remove file based page handler

Tienson Qin 3 дней назад
Родитель
Сommit
32cbac5a52

+ 4 - 9
src/main/frontend/worker/db_worker.cljs

@@ -24,7 +24,6 @@
             [frontend.worker.file :as file]
             [frontend.worker.file.reset :as file-reset]
             [frontend.worker.handler.page :as worker-page]
-            [frontend.worker.handler.page.file-based.rename :as file-worker-page-rename]
             [frontend.worker.pipeline :as worker-pipeline]
             [frontend.worker.rtc.asset-db-listener]
             [frontend.worker.rtc.client-op :as client-op]
@@ -809,12 +808,8 @@
       (p/all (map #(.unsafeUnlinkDB this (:name %)) dbs)))))
 
 (defn- rename-page!
-  [repo conn page-uuid new-name]
-  (let [config (worker-state/get-config repo)
-        f (if (sqlite-util/db-based-graph? repo)
-            (throw (ex-info "Rename page is a file graph only operation" {}))
-            file-worker-page-rename/rename!)]
-    (f repo conn config page-uuid new-name)))
+  []
+  (throw (ex-info "Rename page is a file graph only operation" {})))
 
 (defn- delete-page!
   [repo conn page-uuid]
@@ -837,8 +832,8 @@
   (outliner-op/register-op-handlers!
    {:create-page (fn [repo conn [title options]]
                    (create-page! repo conn title options))
-    :rename-page (fn [repo conn [page-uuid new-name]]
-                   (rename-page! repo conn page-uuid new-name))
+    :rename-page (fn [& _]
+                   (rename-page!))
     :delete-page (fn [repo conn [page-uuid]]
                    (delete-page! repo conn page-uuid))}))
 

+ 3 - 9
src/main/frontend/worker/handler/page.cljs

@@ -1,8 +1,6 @@
 (ns frontend.worker.handler.page
   "Page operations"
-  (:require [frontend.worker.handler.page.file-based.delete :as file-worker-page-delete]
-            [frontend.worker.handler.page.file-based.page :as file-worker-page]
-            [logseq.common.config :as common-config]
+  (:require [logseq.common.config :as common-config]
             [logseq.common.util :as common-util]
             [logseq.db :as ldb]
             [logseq.graph-parser.block :as gp-block]
@@ -35,14 +33,10 @@
    * :properties               - properties to add to the page
   TODO: Add other options"
   [repo conn config title & {:as options}]
-  (if (ldb/db-based-graph? @conn)
-    (outliner-page/create! conn title options)
-    (file-worker-page/create! repo conn config title options)))
+  (outliner-page/create! conn title options))
 
 (defn delete!
   "Deletes a page. Returns true if able to delete page. If unable to delete,
   calls error-handler fn and returns false"
   [repo conn page-uuid & {:as options}]
-  (if (ldb/db-based-graph? @conn)
-    (outliner-page/delete! conn page-uuid options)
-    (file-worker-page-delete/delete! repo conn page-uuid options)))
+  (outliner-page/delete! conn page-uuid options))

+ 0 - 39
src/main/frontend/worker/handler/page/file_based/delete.cljs

@@ -1,39 +0,0 @@
-(ns frontend.worker.handler.page.file-based.delete
-  "File graph page delete"
-  (:require [datascript.core :as d]
-            [logseq.db :as ldb]
-            [logseq.graph-parser.db :as gp-db]))
-
-(defn delete!
-  "Deletes a page. Returns true if able to delete page. If unable to delete,
-  calls error-handler fn and returns false"
-  [repo conn page-uuid & {:keys [persist-op? rename?]
-                          :or {persist-op? true}}]
-  (assert (uuid? page-uuid) (str "frontend.worker.handler.page/delete! requires page-uuid: " (if page-uuid page-uuid "nil")))
-  (when (and repo page-uuid)
-    (when-let [page (d/entity @conn [:block/uuid page-uuid])]
-      (let [page-name (:block/name page)
-            blocks (:block/_page page)
-            truncate-blocks-tx-data (mapv
-                                     (fn [block]
-                                       [:db.fn/retractEntity [:block/uuid (:block/uuid block)]])
-                                     blocks)
-            db @conn
-            file (gp-db/get-page-file db page-name)
-            file-path (:file/path file)
-            delete-file-tx (when file
-                             [[:db.fn/retractEntity [:file/path file-path]]])
-            delete-page-tx [[:db.fn/retractEntity (:db/id page)]]
-            tx-data (concat truncate-blocks-tx-data
-                            delete-page-tx
-                            delete-file-tx)]
-
-        (ldb/transact! conn tx-data
-                       (cond-> {:outliner-op :delete-page
-                                :deleted-page (str (:block/uuid page))
-                                :persist-op? persist-op?}
-                         rename?
-                         (assoc :real-outliner-op :rename-page)
-                         file-path
-                         (assoc :file-path file-path)))
-        true))))

+ 0 - 104
src/main/frontend/worker/handler/page/file_based/page.cljs

@@ -1,104 +0,0 @@
-(ns frontend.worker.handler.page.file-based.page
-  "Page operations for file graphs"
-  (:require [clojure.string :as string]
-            [datascript.core :as d]
-            [logseq.common.config :as common-config]
-            [logseq.common.date :as common-date]
-            [logseq.common.util :as common-util]
-            [logseq.db :as ldb]
-            [logseq.db.common.order :as db-order]
-            [logseq.graph-parser.block :as gp-block]
-            [logseq.graph-parser.property :as gp-property]
-            [logseq.graph-parser.text :as text]))
-
-(defn- file-based-properties-block
-  [repo conn config date-formatter properties format page]
-  (let [content (gp-property/insert-properties repo format "" properties)
-        refs (gp-block/get-page-refs-from-properties properties @conn date-formatter config)]
-    {:block/pre-block? true
-     :block/uuid (ldb/new-block-id)
-     :block/properties properties
-     :block/properties-order (keys properties)
-     :block/refs refs
-     :block/order (db-order/gen-key nil nil)
-     :block/format format
-     :block/title content
-     :block/parent page
-     :block/page page}))
-
-(defn- build-page-tx [repo conn config date-formatter format properties page {:keys [whiteboard? tags]}]
-  (when (:block/uuid page)
-    (let [page-entity   [:block/uuid (:block/uuid page)]
-          page'          (merge page
-                                (when whiteboard? {:block/type "whiteboard"})
-                                (when tags {:block/tags (mapv #(hash-map :db/id
-                                                                         (:db/id (d/entity @conn [:block/uuid %])))
-                                                              tags)}))
-          file-page (merge page'
-                           (when (seq properties) {:block/properties properties}))]
-      (if (and (seq properties)
-               (not whiteboard?)
-               (ldb/page-empty? @conn (:block/name page)))
-        [file-page (file-based-properties-block repo conn config date-formatter properties format page-entity)]
-        [file-page]))))
-
-(defn get-title-and-pagename
-  [title]
-  (let [title      (-> (string/trim title)
-                       (text/page-ref-un-brackets!)
-                        ;; remove `#` from tags
-                       (string/replace #"^#+" ""))
-        title      (common-util/remove-boundary-slashes title)
-        page-name  (common-util/page-name-sanity-lc title)]
-    [title page-name]))
-
-(defn create!
-  [repo conn config title {:keys [format properties uuid persist-op? today-journal?]
-                           :or   {format                   nil
-                                  properties               nil
-                                  uuid                     nil
-                                  persist-op?              true}
-                           :as options}]
-  (let [date-formatter (common-config/get-date-formatter config)
-        split-namespace? (not (or (string/starts-with? title "hls__")
-                                  (common-date/valid-journal-title? date-formatter title)))
-        [title page-name] (get-title-and-pagename title)]
-    (when-not (ldb/get-page @conn page-name)
-      (let [pages    (if split-namespace?
-                       (common-util/split-namespace-pages title)
-                       [title])
-            format   (or format (common-config/get-preferred-format config))
-            pages    (map (fn [page]
-                            ;; only apply uuid to the deepest hierarchy of page to create if provided.
-                            (-> (gp-block/page-name->map page @conn true date-formatter
-                                                         {:page-uuid (when (uuid? uuid) uuid)})
-                                (assoc :block/format format)))
-                          pages)
-            txs      (->> pages
-                            ;; for namespace pages, only last page need properties
-                          drop-last
-                          (mapcat #(build-page-tx repo conn config date-formatter format nil % {}))
-                          (remove nil?))
-            txs      (map-indexed (fn [i page]
-                                    (if (zero? i)
-                                      page
-                                      (assoc page :block/namespace
-                                             [:block/uuid (:block/uuid (nth txs (dec i)))])))
-                                  txs)
-            page-uuid (:block/uuid (last pages))
-            page-txs (build-page-tx repo conn config date-formatter format properties (last pages) (select-keys options [:whiteboard? :tags]))
-            page-txs (if (seq txs)
-                       (update page-txs 0
-                               (fn [p]
-                                 (assoc p :block/namespace [:block/uuid (:block/uuid (last txs))])))
-                       page-txs)
-            txs      (concat
-                      txs
-                      page-txs)]
-        (when (seq txs)
-          (ldb/transact! conn txs (cond-> {:persist-op? persist-op?
-                                           :outliner-op :create-page}
-                                    today-journal?
-                                    (assoc :create-today-journal? true
-                                           :today-journal-name page-name))))
-        [page-name page-uuid]))))

+ 0 - 345
src/main/frontend/worker/handler/page/file_based/rename.cljs

@@ -1,345 +0,0 @@
-(ns frontend.worker.handler.page.file-based.rename
-  "File based page rename"
-  (:require [clojure.string :as string]
-            [clojure.walk :as walk]
-            [datascript.core :as d]
-            [frontend.common.file-based.db :as common-file-db]
-            [frontend.common.file.util :as wfu]
-            [frontend.worker.handler.page :as worker-page]
-            [logseq.common.config :as common-config]
-            [logseq.common.util :as common-util]
-            [logseq.common.util.page-ref :as page-ref]
-            [logseq.db :as ldb]
-            [logseq.db.common.order :as db-order]
-            [logseq.db.file-based.entity-util :as file-entity-util]
-            [logseq.graph-parser.property :as gp-property]
-            [logseq.graph-parser.text :as text]))
-
-(defn- replace-page-ref-aux
-  "Unsanitized names"
-  [config content old-name new-name]
-  (let [preferred-format (common-config/get-preferred-format config)
-        [original-old-name original-new-name] (map string/trim [old-name new-name])
-        [old-ref new-ref] (map page-ref/->page-ref [old-name new-name])
-        [old-name new-name] (map #(if (string/includes? % "/")
-                                    (string/replace % "/" ".")
-                                    %)
-                                 [original-old-name original-new-name])
-        old-org-ref (and (= :org preferred-format)
-                         (:org-mode/insert-file-link? config)
-                         (re-find
-                          (re-pattern
-                           (common-util/format
-                            "\\[\\[file:\\.*/.*%s\\.org\\]\\[(.*?)\\]\\]" old-name))
-                          content))]
-    (-> (if old-org-ref
-          (let [[old-full-ref old-label] old-org-ref
-                new-label (if (= old-label original-old-name)
-                            original-new-name
-                            old-label)
-                new-full-ref (-> (string/replace old-full-ref old-name new-name)
-                                 (string/replace (str "[" old-label "]")
-                                                 (str "[" new-label "]")))]
-            (string/replace content old-full-ref new-full-ref))
-          content)
-        (string/replace old-ref new-ref))))
-
-(defn replace-tag-ref!
-  [content old-name new-name]
-  (let [old-tag (common-util/format "#%s" old-name)
-        new-tag (if (re-find #"[\s\t]+" new-name)
-                  (common-util/format "#[[%s]]" new-name)
-                  (str "#" new-name))]
-    ;; hash tag parsing rules https://github.com/logseq/mldoc/blob/701243eaf9b4157348f235670718f6ad19ebe7f8/test/test_markdown.ml#L631
-    ;; Safari doesn't support look behind, don't use
-    ;; TODO: parse via mldoc
-    (string/replace content
-                    (re-pattern (str "(?i)(^|\\s)(" (common-util/escape-regex-chars old-tag) ")(?=[,\\.]*($|\\s))"))
-                    ;;    case_insense^    ^lhs   ^_grp2                       look_ahead^         ^_grp3
-                    (fn [[_match lhs _grp2 _grp3]]
-                      (str lhs new-tag)))))
-
-(defn- replace-property-ref!
-  [content old-name new-name format]
-  (let [new-name (keyword (string/replace (string/lower-case new-name) #"\s+" "-"))
-        org-format? (= :org format)
-        old-property (if org-format? (gp-property/colons-org old-name) (str old-name gp-property/colons))
-        new-property (if org-format? (gp-property/colons-org (name new-name)) (str (name new-name) gp-property/colons))]
-    (common-util/replace-ignore-case content old-property new-property)))
-
-(defn- replace-old-page!
-  "Unsanitized names"
-  [config content old-name new-name format]
-  (when (and (string? content) (string? old-name) (string? new-name))
-    (-> (replace-page-ref-aux config content old-name new-name)
-        (replace-tag-ref! old-name new-name)
-        (replace-property-ref! old-name new-name format))))
-
-(defn- walk-replace-old-page!
-  "Unsanitized names"
-  [config form old-name new-name format]
-  (walk/postwalk (fn [f]
-                   (cond
-                     (and (vector? f)
-                          (contains? #{"Search" "Label"} (first f))
-                          (string/starts-with? (second f) (str old-name "/")))
-                     [(first f) (string/replace-first (second f)
-                                                      (str old-name "/")
-                                                      (str new-name "/"))]
-
-                     (string? f)
-                     (if (= f old-name)
-                       new-name
-                       (replace-old-page! config f old-name new-name format))
-
-                     (and (keyword f) (= (name f) old-name))
-                     (keyword (string/replace (string/lower-case new-name) #"\s+" "-"))
-
-                     :else
-                     f))
-                 form))
-
-(defn- rename-update-block-refs!
-  [refs from-id to-id]
-  (if to-id
-    (->> refs
-         (remove #{{:db/id from-id}})
-         (cons {:db/id to-id})
-         (distinct)
-         (vec))
-    ;; New page not exists so that we keep using the old page's block as a ref
-    refs))
-
-(defn replace-page-ref
-  "Unsanitized only"
-  [db config page new-name]
-  ;; update all pages which have references to this page
-  (let [to-page (ldb/get-page db new-name)
-        old-title (:block/title page)
-        blocks (:block/_refs (d/entity db (:db/id page)))
-        tx     (->> (map (fn [{:block/keys [uuid title properties format] :as block}]
-                           (let [content    (let [content' (replace-old-page! config title old-title new-name format)]
-                                              (when-not (= content' title)
-                                                content'))
-                                 properties (let [properties' (walk-replace-old-page! config properties old-title new-name format)]
-                                              (when-not (= properties' properties)
-                                                properties'))]
-                             (when (or content properties)
-                               (common-util/remove-nils-non-nested
-                                {:block/uuid       uuid
-                                 :block/title    content
-                                 :block/properties properties
-                                 :block/properties-order (when (seq properties)
-                                                           (map first properties))
-                                 :block/refs (->> (rename-update-block-refs! (:block/refs block) (:db/id page) (:db/id to-page))
-                                                  (map :db/id)
-                                                  (set))})))) blocks)
-                    (remove nil?))]
-    tx))
-
-(defn rename-update-namespace!
-  "update :block/namespace of the renamed block"
-  [repo conn config page old-title new-name]
-  (let [old-namespace? (text/namespace-page? old-title)
-        new-namespace? (text/namespace-page? new-name)]
-    (cond
-      new-namespace?
-      ;; update namespace
-      (let [namespace (first (common-util/split-last "/" new-name))]
-        (when namespace
-          (worker-page/create! repo conn config namespace) ;; create parent page if not exist, creation of namespace ref is handled in `create!`
-          (let [namespace-block (d/entity @conn [:block/name (common-util/page-name-sanity-lc namespace)])
-                page-txs [{:db/id (:db/id page)
-                           :block/namespace (:db/id namespace-block)}]]
-            (ldb/transact! conn page-txs {:persist-op? true}))))
-
-      old-namespace?
-      ;; retract namespace
-      (ldb/transact! conn [[:db/retract (:db/id page) :block/namespace]] {:persist-op? true})
-
-      :else
-      nil)))
-
-(declare rename-page-aux)
-
-(defn- based-merge-pages!
-  [repo conn config from-page-name to-page-name {:keys [old-name new-name]}]
-  (let [db @conn
-        to-page (d/entity db [:block/name to-page-name])
-        to-id (:db/id to-page)
-        from-page (d/entity db [:block/name from-page-name])
-        from-id (:db/id from-page)]
-    (when (and from-page to-page (not= from-page-name to-page-name))
-      (let [datoms (d/datoms @conn :avet :block/page from-id)
-            block-eids (mapv :e datoms)
-            blocks (d/pull-many db '[:db/id :block/page :block/refs :block/order :block/parent] block-eids)
-            blocks-tx-data (map (fn [block]
-                                  (let [id (:db/id block)]
-                                    (cond->
-                                     {:db/id id
-                                      :block/page {:db/id to-id}
-                                      :block/refs (rename-update-block-refs! (:block/refs block) from-id to-id)
-                                      :block/order (db-order/gen-key nil)}
-
-                                      (= (:block/parent block) {:db/id from-id})
-                                      (assoc :block/parent {:db/id to-id})))) blocks)
-            replace-ref-tx-data (replace-page-ref db config from-page to-page-name)
-            tx-data (concat blocks-tx-data replace-ref-tx-data)]
-
-        (rename-page-aux repo conn config old-name new-name
-                         :merge? true
-                         :other-tx tx-data)
-
-        (worker-page/delete! repo conn (:block/uuid from-page) {:rename? true})))))
-
-(defn- compute-new-file-path
-  "Construct the full path given old full path and the file sanitized body.
-   Ext. included in the `old-path`."
-  [old-path new-file-name-body]
-  (let [result (string/split old-path "/")
-        ext (last (string/split (last result) "."))
-        new-file (str new-file-name-body "." ext)
-        parts (concat (butlast result) [new-file])]
-    (common-util/string-join-path parts)))
-
-(defn- update-file-tx
-  [db old-page-name new-page-name]
-  (let [page (d/entity db [:block/name old-page-name])
-        file (:block/file page)]
-    (when (and file (not (file-entity-util/journal? page)))
-      (let [old-path (:file/path file)
-            new-file-name (wfu/file-name-sanity new-page-name) ;; w/o file extension
-            new-path (compute-new-file-path old-path new-file-name)]
-        {:old-path old-path
-         :new-path new-path
-         :tx-data [{:db/id (:db/id file)
-                    :file/path new-path}]}))))
-
-(defn- rename-page-aux
-  "Only accepts unsanitized page names"
-  [repo conn config old-name new-name & {:keys [merge? other-tx]}]
-  (let [db                  @conn
-        old-page-name       (common-util/page-name-sanity-lc old-name)
-        new-page-name       (common-util/page-name-sanity-lc new-name)
-        page                (d/pull @conn '[*] [:block/name old-page-name])]
-    (when (and repo page)
-      (let [old-title   (:block/title page)
-            page-txs            (when-not merge?
-                                  [{:db/id               (:db/id page)
-                                    :block/uuid          (:block/uuid page)
-                                    :block/name          new-page-name
-                                    :block/title new-name}])
-            {:keys [old-path new-path tx-data]} (update-file-tx db old-page-name new-name)
-            txs (concat page-txs
-                        other-tx
-                        (->>
-                         (concat
-                            ;;  update page refes in block content when ref name changes
-                          (replace-page-ref db config page new-name)
-                            ;; update file path
-                          tx-data)
-
-                         (remove nil?)))]
-
-        (ldb/transact! conn txs {:outliner-op :rename-page
-                                 :data (cond->
-                                        {:page-id (:db/id page)
-                                         :old-name old-name
-                                         :new-name new-name}
-                                         (and old-path new-path)
-                                         (merge {:old-path old-path
-                                                 :new-path new-path}))})
-
-        (rename-update-namespace! repo conn config page old-title new-name)))))
-
-(defn- rename-namespace-pages!
-  "Original names (unsanitized only)"
-  [repo conn config old-name new-name]
-  (let [pages (common-file-db/get-namespace-pages @conn old-name)
-        page (d/pull @conn '[*] [:block/name (common-util/page-name-sanity-lc old-name)])
-        pages (cons page pages)]
-    (doseq [{:block/keys [name title]} pages]
-      (let [old-page-title (or title name)
-            ;; only replace one time, for the case that the namespace is a sub-string of the sub-namespace page name
-            ;; Example: has pages [[work]] [[work/worklog]],
-            ;; we want to rename [[work/worklog]] to [[work1/worklog]] when rename [[work]] to [[work1]],
-            ;; but don't rename [[work/worklog]] to [[work1/work1log]]
-            new-page-title (common-util/replace-first-ignore-case old-page-title old-name new-name)]
-        (when (and old-page-title new-page-title)
-          (rename-page-aux repo conn config old-page-title new-page-title)
-          (println "Renamed " old-page-title " to " new-page-title))))))
-
-(defn- rename-nested-pages
-  "Unsanitized names only"
-  [repo conn config old-ns-name new-ns-name]
-  (let [nested-page-str (page-ref/->page-ref (common-util/page-name-sanity-lc old-ns-name))
-        ns-prefix-format-str (str page-ref/left-brackets "%s/")
-        ns-prefix       (common-util/format ns-prefix-format-str (common-util/page-name-sanity-lc old-ns-name))
-        nested-pages    (common-file-db/get-pages-by-name-partition @conn nested-page-str)
-        nested-pages-ns (common-file-db/get-pages-by-name-partition @conn ns-prefix)]
-    (when nested-pages
-      ;; rename page "[[obsidian]] is a tool" to "[[logseq]] is a tool"
-      (doseq [{:block/keys [name title]} nested-pages]
-        (let [old-page-title (or title name)
-              new-page-title (string/replace
-                              old-page-title
-                              (page-ref/->page-ref old-ns-name)
-                              (page-ref/->page-ref new-ns-name))]
-          (when (and old-page-title new-page-title)
-            (rename-page-aux repo conn config old-page-title new-page-title)
-            (println "Renamed " old-page-title " to " new-page-title)))))
-    (when nested-pages-ns
-      ;; rename page "[[obsidian/page1]] is a tool" to "[[logseq/page1]] is a tool"
-      (doseq [{:block/keys [name title]} nested-pages-ns]
-        (let [old-page-title (or title name)
-              new-page-title (string/replace
-                              old-page-title
-                              (common-util/format ns-prefix-format-str old-ns-name)
-                              (common-util/format ns-prefix-format-str new-ns-name))]
-          (when (and old-page-title new-page-title)
-            (rename-page-aux repo conn config old-page-title new-page-title)
-            (println "Renamed " old-page-title " to " new-page-title)))))))
-
-(defn rename!
-  [repo conn config page-uuid new-name & {:keys [persist-op?]
-                                          :or {persist-op? true}}]
-  (let [db @conn
-        page-e        (d/entity db [:block/uuid page-uuid])
-        old-name      (:block/title page-e)
-        new-name      (string/trim new-name)
-        old-page-name (common-util/page-name-sanity-lc old-name)
-        new-page-name (common-util/page-name-sanity-lc new-name)
-        new-page-e (d/entity db [:block/name new-page-name])
-        name-changed? (not= old-name new-name)]
-    (cond
-      (ldb/built-in? page-e)
-      :built-in-page
-
-      (string/blank? new-name)
-      :invalid-empty-name
-
-      (and page-e new-page-e
-           (or (file-entity-util/whiteboard? page-e)
-               (file-entity-util/whiteboard? new-page-e)))
-      :merge-whiteboard-pages
-
-      (and old-name new-name name-changed?)
-      (do
-        (cond
-          (= old-page-name new-page-name) ; case changed
-          (ldb/transact! conn
-                         [{:db/id (:db/id page-e)
-                           :block/title new-name}]
-                         {:page-id (:db/id page-e)
-                          :persist-op? persist-op?
-                          :outliner-op :rename-page})
-
-          (and (not= old-page-name new-page-name)
-               (d/entity @conn [:block/name new-page-name])) ; merge page
-          (based-merge-pages! repo conn config old-page-name new-page-name {:old-name old-name
-                                                                            :new-name new-name
-                                                                            :persist-op? persist-op?})
-
-          :else                          ; rename
-          (rename-namespace-pages! repo conn config old-name new-name))
-        (rename-nested-pages repo conn config old-name new-name)))))

+ 3 - 8
src/test/frontend/db/name_sanity_test.cljs

@@ -1,10 +1,9 @@
 (ns frontend.db.name-sanity-test
   (:require [cljs.test :refer [deftest testing is]]
             [clojure.string :as string]
-            [logseq.graph-parser.extract :as extract]
-            [frontend.worker.handler.page.file-based.rename :as worker-page-rename]
+            [frontend.common.file.util :as wfu]
             [frontend.util.fs :as fs-util]
-            [frontend.common.file.util :as wfu]))
+            [logseq.graph-parser.extract :as extract]))
 
 (defn- test-page-name
   "Check if page name can be preserved after escaping"
@@ -15,7 +14,7 @@
           url-single  (js/encodeURIComponent file-name)
           url-double  (js/encodeURIComponent url-single)
           file-name'  (js/decodeURIComponent url-single)
-          file-name'' ( js/decodeURIComponent (js/decodeURIComponent url-double))]
+          file-name'' (js/decodeURIComponent (js/decodeURIComponent url-double))]
       (is (= page-name page-name'))
       (is (not (fs-util/include-reserved-chars? file-name)))
       (is (not (contains? fs-util/windows-reserved-filebodies file-name)))
@@ -45,7 +44,3 @@
   (test-page-name "CON.")
   (test-page-name ".NET.")
   (mapv test-page-name fs-util/windows-reserved-filebodies))
-
-(deftest new-path-computation-tests
-  (is (= (#'worker-page-rename/compute-new-file-path "/data/app/dsal dsalfjk aldsaf.jkl" "ddd") "/data/app/ddd.jkl"))
-  (is (= (#'worker-page-rename/compute-new-file-path "c://data/a sdfpp/dsal dsalf% * _ dsaf.mnk" "c d / f") "c://data/a sdfpp/c d / f.mnk")))

+ 0 - 189
src/test/frontend/worker/handler/page/file_based/rename_test.cljs

@@ -1,189 +0,0 @@
-(ns frontend.worker.handler.page.file-based.rename-test
-  (:require [clojure.string :as string]
-            [clojure.test :refer [deftest is testing use-fixtures are]]
-            [datascript.core :as d]
-            [frontend.db :as db]
-            [frontend.handler.editor :as editor-handler]
-            [frontend.test.helper :as test-helper]
-            [frontend.util :as util]
-            [frontend.worker.handler.page.file-based.rename :as file-page-rename]))
-
-;; FIXME: merge properties from both pages
-
-(def repo test-helper/test-db-name)
-
-(def init-data (test-helper/initial-test-page-and-blocks))
-
-(def fbid (:block/uuid (second init-data)))
-
-(defn start-and-destroy-db
-  [f]
-  (test-helper/start-and-destroy-db
-   f
-   {:init-data (fn [conn] (d/transact! conn init-data))}))
-
-(use-fixtures :each start-and-destroy-db)
-
-(defn- page-rename [page-uuid new-name]
-  (file-page-rename/rename! repo (db/get-db repo false) {} page-uuid new-name))
-
-(deftest rename-test
-  (testing "Case change"
-    (let [page (db/get-page "test")]
-      (page-rename (:block/uuid page) "Test")
-      (is (= "Test" (:block/title (db/entity (:db/id page)))))))
-
-  (testing "Name changed"
-    (let [page (db/get-page "Test")]
-      (page-rename (:block/uuid page) "New name")
-      (is (= "New name" (:block/title (db/entity (:db/id page)))))))
-
-  (testing "Merge existing page"
-    (test-helper/create-page! "Existing page" {:redirect? false})
-    (let [page (db/get-page "new name")]
-      (page-rename (:block/uuid page) "Existing page"))
-    (let [e1 (db/get-page "new name")
-          e2 (db/get-page "existing page")]
-      ;; Old page deleted
-      (is (nil? e1))
-      ;; Blocks from both pages have been merged
-      (is (= (count (:block/_page e2)) (dec (count init-data)))))))
-
-(deftest merge-with-empty-page
-  (test-helper/create-page! "Existing page" {:redirect? false})
-  (let [page (db/get-page "test")]
-    (page-rename (:block/uuid page) "Existing page"))
-  (let [e1 (db/get-page "test")
-        e2 (db/get-page "existing page")]
-      ;; Old page deleted
-    (is (nil? e1))
-      ;; Blocks from both pages have been merged
-    (is (= (count (:block/_page e2)) (dec (count init-data))))))
-
-(deftest merge-existing-pages-should-update-ref-ids
-  (testing "Merge existing page"
-    (editor-handler/save-block! repo fbid "Block 1 [[Test]]")
-    (test-helper/create-page! "Existing page" {:redirect? false})
-    (let [page (db/get-page "test")]
-      (page-rename (:block/uuid page) "Existing page"))
-    (let [e1 (db/get-page "test")
-          e2 (db/get-page "existing page")]
-      ;; Old page deleted
-      (is (nil? e1))
-      ;; Blocks from both pages have been merged
-      (is (= (count (:block/_page e2)) (dec (count init-data))))
-      ;; Content updated
-      (is (= "Block 1 [[Existing page]]" (:block/title (db/entity [:block/uuid fbid])))))))
-
-(defn- replace-page-ref!
-  [content old-name new-name]
-  (let [[original-old-name original-new-name] (map string/trim [old-name new-name])
-        [old-ref new-ref] (map #(util/format "[[%s]]" %) [old-name new-name])
-        [old-name new-name] (map #(if (string/includes? % "/")
-                                    (string/replace % "/" ".")
-                                    %)
-                                 [original-old-name original-new-name])
-        old-org-ref (re-find
-                     (re-pattern
-                      (util/format
-                       "\\[\\[file:\\.*/.*%s\\.org\\]\\[(.*?)\\]\\]" old-name))
-                     content)]
-    (-> (if old-org-ref
-          (let [[old-full-ref old-label] old-org-ref
-                new-label (if (= old-label original-old-name)
-                            original-new-name
-                            old-label)
-                new-full-ref (-> (string/replace old-full-ref old-name new-name)
-                                 (string/replace (str "[" old-label "]")
-                                                 (str "[" new-label "]")))]
-            (string/replace content old-full-ref new-full-ref))
-          content)
-        (string/replace old-ref new-ref))))
-
-(defn- replace-old-page!
-  [content old-name new-name]
-  (when (and (string? content) (string? old-name) (string? new-name))
-    (-> content
-        (replace-page-ref! old-name new-name)
-        (file-page-rename/replace-tag-ref! old-name new-name))))
-
-(deftest test-replace-page-ref!
-  (are [x y] (= (let [[content old-name new-name] x]
-                  (replace-page-ref! content old-name new-name))
-                y)
-    ["bla [[foo]] bla" "foo" "bar"] "bla [[bar]] bla"
-
-    ["bla [[logseq/foo]] bla" "logseq/foo" "logseq/bar"] "bla [[logseq/bar]] bla"
-
-    ["bla [[file:./foo.org][foo]] bla" "foo" "bar"]
-    "bla [[file:./bar.org][bar]] bla"
-
-    ["bla [[file:./logseq.foo.org][logseq/foo]] bla" "logseq/foo" "logseq/bar"]
-    "bla [[file:./logseq.bar.org][logseq/bar]] bla"
-
-    ["bla [[file:../pages/logseq.foo.org][logseq/foo]] bla" "logseq/foo" "logseq/bar"]
-    "bla [[file:../pages/logseq.bar.org][logseq/bar]] bla"
-
-    ["bla [[file:./pages/logseq.foo.org][logseq/foo]] bla" "logseq/foo" "logseq/bar"]
-    "bla [[file:./pages/logseq.bar.org][logseq/bar]] bla"
-
-    ["bla [[file:./pages/logseq.foo.org][logseq/foo]] bla [[logseq/foo]]" "logseq/foo" "logseq/bar"]
-    "bla [[file:./pages/logseq.bar.org][logseq/bar]] bla [[logseq/bar]]"
-
-    ["bla [[file:./pages/logseq.foo.org][don't change this label]] bla [[logseq/foo]]" "logseq/foo" "logseq/bar"]
-    "bla [[file:./pages/logseq.bar.org][don't change this label]] bla [[logseq/bar]]"))
-
-(deftest test-replace-tag-ref!
-  (are [x y] (= (let [[content old-name new-name] x]
-                  (file-page-rename/replace-tag-ref! content old-name new-name))
-                y)
-    ["#foo" "foo" "bar"] "#bar"
-    ["#foo" "foo" "new bar"] "#[[new bar]]"
-
-    ["bla #foo bla" "foo" "bar"] "bla #bar bla"
-    ["bla #foo bla" "foo" "new bar"] "bla #[[new bar]] bla"
-
-    ["bla #foo" "foo" "bar"] "bla #bar"
-    ["bla #foo" "foo" "new bar"] "bla #[[new bar]]"
-
-    ["#foo #foobar" "foo" "bar"]
-    "#bar #foobar"
-
-    ["#foo #foobar bar#foo #foo" "foo" "bar"]
-    "#bar #foobar bar#foo #bar"
-
-    ["#foo #foobar bar#foo #foo,," "foo" "bar"]
-    "#bar #foobar bar#foo #bar,,"
-
-    ["#foo #foobar bar#foo #foo #foo ball" "foo" "bar"]
-    "#bar #foobar bar#foo #bar #bar ball"
-
-    ["#foo #foobar bar#foo #foo\t#foo ball" "foo" "bar"]
-    "#bar #foobar bar#foo #bar\t#bar ball"
-
-    ["#foo #foobar bar#foo #foo" "foo" "new bar"]
-    "#[[new bar]] #foobar bar#foo #[[new bar]]"
-
-    ["#logseq/foo #logseq/foobar bar#logseq/foo #logseq/foo" "logseq/foo" "logseq/bar"]
-    "#logseq/bar #logseq/foobar bar#logseq/foo #logseq/bar"
-
-    ;; #6451
-    ["#中文" "中文" "中文2"] "#中文2"
-    ["#2中文" "2中文" "中文234"] "#中文234"
-    ["#2中文2" "2中文2" "中文1999"] "#中文1999"
-    ["#2中文,SLKDF" "2中文" "中文1999"] "#2中文,SLKDF"
-    ["#2中文, SLKDF" "2中文" "中文1999"] "#中文1999, SLKDF"
-    ["#2中文看来减肥了" "2中文" "中文1999"] "#2中文看来减肥了"
-    ["两份健康 #2中文 看来减肥了" "2中文" "中文1999"] "两份健康 #中文1999 看来减肥了"
-    ["sdaflk  #2中文   看asdf了" "2中文" "中文1999"] "sdaflk  #中文1999   看asdf了"
-    ["sdaflk  #2中文" "2中文" "中文1999"] "sdaflk  #中文1999"))
-
-(deftest test-replace-old-page!
-  (are [x y] (= (let [[content old-name new-name] x]
-                  (replace-old-page! content old-name new-name))
-                y)
-    ["#foo bla [[foo]] bla #foo" "foo" "bar"]
-    "#bar bla [[bar]] bla #bar"
-
-    ["#logseq/foo bla [[logseq/foo]] bla [[file:./pages/logseq.foo.org][logseq/foo]] bla #logseq/foo" "logseq/foo" "logseq/bar"]
-    "#logseq/bar bla [[logseq/bar]] bla [[file:./pages/logseq.bar.org][logseq/bar]] bla #logseq/bar"))