Replace special [[id]] with [[page name]] when reading block content

This commit is contained in:
Tienson Qin
2023-06-15 22:58:03 +08:00
parent 06e8bae3c5
commit 8746a373ae
8 changed files with 73 additions and 50 deletions

View File

@@ -495,3 +495,5 @@
(defn get-block-hidden-properties
[]
(:block-hidden-properties (state/get-config)))
(defonce page-ref-special-chars "`~`^")

View File

@@ -1,19 +1,29 @@
(ns frontend.db.datascript.entity-plus
"Add map ops such as assoc/dissoc to datascript Entity"
(:require [datascript.impl.entity :as entity :refer [Entity]]
[datascript.core :as d]
[cljs.core]))
(:require [cljs.core]
[datascript.impl.entity :as entity :refer [Entity]]
[frontend.state :as state]
[frontend.config :as config]
[frontend.db.utils :as db-utils]))
(def lookup-entity @#'entity/lookup-entity)
(defn lookup-kv-then-entity
([e k] (lookup-kv-then-entity e k nil))
([^Entity e k default-value]
(or (get (.-kv e) k)
(lookup-entity e k default-value))))
(if (and (= k :block/content) (config/db-based-graph? (state/get-current-repo)))
(let [result (lookup-entity e k default-value)
refs (:block/refs e)]
(or
(if (seq refs)
(db-utils/special-id->page result refs)
result)
default-value))
(or (get (.-kv e) k)
(lookup-entity e k default-value)))))
(extend-type Entity
cljs.core/IEncodeJS
(-clj->js [this] nil) ; avoid `clj->js` overhead when entity was passed to rum components
(-clj->js [_this] nil) ; avoid `clj->js` overhead when entity was passed to rum components
IAssociative
(-assoc [this k v]

View File

@@ -876,6 +876,7 @@ independent of format as format specific heading characters are stripped"
(cons (dissoc blocks-tree :block/_parent) (mapcat flatten-tree children))
[blocks-tree]))
;; TODO: performance enhance
(defn get-block-and-children
[repo block-uuid]
(some-> (d/q
@@ -887,7 +888,8 @@ independent of format as format specific heading characters are stripped"
block-uuid
block-attrs)
first
flatten-tree))
flatten-tree
(->> (map #(db-utils/update-block-content % (:db/id %))))))
(defn get-file-page
([file-path]
@@ -1248,7 +1250,7 @@ independent of format as format specific heading characters are stripped"
(->> (d/datoms db :aevt :block/content)
(filter filter-fn)
(map :e))
result (d/pull-many db block-attrs ids)]
result (db-utils/pull-many db block-attrs ids)]
(remove (fn [block] (= page-id (:db/id (:block/page block)))) result)))}
nil)
react

View File

@@ -5,7 +5,8 @@
[datascript.transit :as dt]
[frontend.db.conn :as conn]
[frontend.config :as config]
[logseq.graph-parser.util :as gp-util]))
[logseq.graph-parser.util :as gp-util]
[clojure.string :as string]))
;; transit serialization
@@ -60,6 +61,27 @@
(d/entity db id-or-lookup-ref))
(d/entity db id-or-lookup-ref)))))
(defn special-id->page
"Convert special id backs to page name."
[content refs]
(reduce
(fn [content ref]
(if (:block/name ref)
(string/replace content (str config/page-ref-special-chars (:block/uuid ref)) (:block/name ref))
content))
content
refs))
(defn update-block-content
"Replace `[[internal-id]]` with `[[page name]]`"
[item eid]
(if (config/db-based-graph? (state/get-current-repo))
(if-let [content (:block/content item)]
(let [refs (:block/refs (entity eid))]
(assoc item :block/content (special-id->page content refs)))
item)
item))
(defn pull
([eid]
(pull (state/get-current-repo) '[*] eid))
@@ -67,12 +89,8 @@
(pull (state/get-current-repo) selector eid))
([repo selector eid]
(when-let [db (conn/get-db repo)]
(try
(d/pull db
selector
eid)
(catch :default _e
nil)))))
(let [result (d/pull db selector eid)]
(update-block-content result eid)))))
(defn pull-many
([eids]
@@ -81,10 +99,9 @@
(pull-many (state/get-current-repo) selector eids))
([repo selector eids]
(when-let [db (conn/get-db repo)]
(try
(d/pull-many db selector eids)
(catch :default e
(js/console.error e))))))
(let [selector (if (some #{:db/id} selector) selector (conj selector :db/id))]
(->> (d/pull-many db selector eids)
(map #(update-block-content % (:db/id %))))))))
(defn transact!
([tx-data]

View File

@@ -9,7 +9,6 @@
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser.util.page-ref :as page-ref]))
(def page-ref-special-chars "`~`^")
(defn- remove-non-existed-refs!
[refs]
(remove (fn [x] (or
@@ -20,7 +19,7 @@
(defn- replace-tag-ref
[content page-name id]
(let [id' (str page-ref-special-chars id)
(let [id' (str config/page-ref-special-chars id)
[page wrapped-id] (if (string/includes? page-name " ")
(map page-ref/->page-ref [page-name id'])
[page-name id'])
@@ -37,7 +36,7 @@
(defn- replace-page-ref
[content page-name id]
(let [id' (str page-ref-special-chars id)
(let [id' (str config/page-ref-special-chars id)
[page wrapped-id] (map page-ref/->page-ref [page-name id'])]
(util/replace-ignore-case content page wrapped-id)))

View File

@@ -55,18 +55,12 @@
;;; internal utils
(defn- get-blocks-contents
[repo root-block-uuid]
[repo root-block-uuid & {:keys [init-level]
:or {init-level 1}}]
(->
(db/get-block-and-children repo root-block-uuid)
(outliner-tree/blocks->vec-tree (str root-block-uuid))
(outliner-file/tree->file-content {:init-level 1})))
(defn get-page-content
[page]
(-> page
db/get-page
:block/file
:file/content))
(outliner-file/tree->file-content {:init-level init-level})))
(defn root-block-uuids->content
[repo root-block-uuids]
@@ -94,11 +88,17 @@
(mapv remove-block-ast-pos
(gp-mldoc/->edn content (gp-mldoc/default-config format)))))))
(defn get-page-content
([page-name]
(get-page-content (state/get-current-repo) page-name))
([repo page-name]
(when-let [page-uuid (:block/uuid (db/entity [:block/name (util/page-name-sanity-lc page-name)]))]
(get-blocks-contents repo page-uuid :init-level 0))))
(defn- page-name->ast
[page-name]
(let [content (get-page-content page-name)
format :markdown]
(when content
(when-let [content (get-page-content page-name)]
(let [format :markdown]
(removev Properties-block-ast?
(mapv remove-block-ast-pos
(gp-mldoc/->edn content (gp-mldoc/default-config format)))))))

View File

@@ -4,22 +4,14 @@
(def keys-of-deleted-entity 1)
(defn safe-pull
[db selector eid]
(try
(d/pull db selector eid)
(catch :default e
(js/console.error e)
nil)))
(defn get-entity-from-db-after-or-before
"Get the entity from db after if possible; otherwise get entity from db before
Useful for fetching deleted elements"
[db-before db-after db-id]
(let [r (safe-pull db-after '[*] db-id)]
(let [r (d/pull db-after '[*] db-id)]
(if (= keys-of-deleted-entity (count r))
;; block has been deleted
(safe-pull db-before '[*] db-id)
(d/pull db-before '[*] db-id)
r)))
(defn get-blocks-and-pages

View File

@@ -16,7 +16,8 @@
[goog.object :as gobj]
[promesa.core :as p]
[clojure.set :as set]
[frontend.modules.datascript-report.core :as db-report]))
[frontend.modules.datascript-report.core :as db-report]
[datascript.core :as d]))
(defn get-engine
[repo]
@@ -286,15 +287,15 @@
(let [tar-db (:db-after tx-report)]
;; Reverse query the corresponding page id of the modified `:file/content`)
(when-let [page-id (->> (:e datom)
(db-report/safe-pull tar-db '[:block/_file])
(d/pull tar-db '[:block/_file])
(:block/_file)
(first)
(:db/id))]
;; Fetch page entity according to what page->index requested
(db-report/safe-pull tar-db '[:db/id :block/uuid
:block/original-name
{:block/file [:file/content]}]
page-id)))))
(d/pull tar-db '[:db/id :block/uuid
:block/original-name
{:block/file [:file/content]}]
page-id)))))
(remove nil?)))))
;; TODO merge with logic in `invoke-hooks` when feature and test is sufficient