fix: avoid :block/refs self cycle

This commit is contained in:
Tienson Qin
2024-08-06 15:21:13 +08:00
parent d151ec755f
commit 72552d6224
4 changed files with 17 additions and 14 deletions

View File

@@ -362,7 +362,7 @@
(assoc page :block/type type))))))
(defn- with-page-refs-and-tags
[{:keys [title body tags refs marker priority] :as block} db date-formatter]
[{:keys [title body tags refs marker priority] :as block} db date-formatter parse-block]
(let [db-based? (ldb/db-based-graph? db)
refs (->> (concat tags refs (when-not db-based? [marker priority]))
(remove string/blank?)
@@ -419,7 +419,9 @@
(remove nil?)
(map (fn [ref]
(if-let [entity (ldb/get-case-page db (:block/title ref))]
(select-keys entity [:block/uuid :block/title :block/name])
(if (= (:db/id parse-block) (:db/id entity))
ref
(select-keys entity [:block/uuid :block/title :block/name]))
ref))))
tags (ref->map-fn *structured-tags true)]
(assoc block
@@ -484,9 +486,9 @@
(map (fn [page] (page-name->map page db true date-formatter)) page-refs)))
(defn- with-page-block-refs
[block db date-formatter]
[block db date-formatter & {:keys [parse-block]}]
(some-> block
(with-page-refs-and-tags db date-formatter)
(with-page-refs-and-tags db date-formatter parse-block)
with-block-refs
(update :refs (fn [col] (remove nil? col)))))
@@ -558,7 +560,7 @@
properties))
(defn- construct-block
[block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter]}]
[block properties timestamps body encoded-content format pos-meta {:keys [block-pattern db date-formatter parse-block]}]
(let [id (get-custom-id-or-new-id properties)
ref-pages-in-properties (->> (:page-refs properties)
(remove string/blank?))
@@ -596,7 +598,7 @@
block)
block (-> block
(assoc :body body)
(with-page-block-refs db date-formatter)
(with-page-block-refs db date-formatter {:parse-block parse-block})
(update :tags (fn [tags] (map #(assoc % :block/format format) tags)))
(update :refs (fn [refs] (map #(if (map? %) (assoc % :block/format format) %) refs))))
block (update block :refs concat (:block-refs properties))
@@ -651,7 +653,7 @@
`blocks`: mldoc ast.
`content`: markdown or org-mode text.
`format`: content's format, it could be either :markdown or :org-mode.
`options`: Options supported are :user-config, :block-pattern,
`options`: Options supported are :user-config, :block-pattern, parse-block,
:extract-macros, :date-formatter, :page-name, :db-graph-mode? and :db"
[blocks content format {:keys [user-config db-graph-mode?] :as options}]
{:pre [(seq blocks) (string? content) (contains? #{:markdown :org} format)]}

View File

@@ -172,8 +172,7 @@
[id])
property-refs content-refs)
;; Remove self-ref to avoid recursive bugs
(remove #(and (:db/ident block)
(= % (:db/ident block))))
(remove #(or (= (:db/id block) %) (= (:db/id block) (:db/id (d/entity db %)))))
;; Remove alias ref to avoid recursive display bugs
(remove #(contains? (set (map :db/id (:block/alias block))) %))
(remove nil?))))

View File

@@ -16,11 +16,12 @@
(defn extract-blocks
"Wrapper around logseq.graph-parser.block/extract-blocks that adds in system state
and handles unexpected failure."
[blocks content format {:keys [page-name]}]
[blocks content format {:keys [page-name parse-block]}]
(let [repo (state/get-current-repo)]
(try
(gp-block/extract-blocks blocks content format
{:user-config (state/get-config)
:parse-block parse-block
:block-pattern (config/get-block-pattern format)
:db (db/get-db repo)
:date-formatter (state/get-date-formatter)
@@ -74,7 +75,7 @@ and handles unexpected failure."
format (or format :markdown)
parse-config (mldoc/get-default-config format)
ast (format/to-edn title format parse-config)
blocks (extract-blocks ast title format {})
blocks (extract-blocks ast title format {:parse-block block})
new-block (first blocks)
block (cond->
(merge block new-block)

View File

@@ -30,8 +30,9 @@
(nil? x))) refs))
(defn- use-cached-refs!
[refs]
(let [cached-refs @(:editor/block-refs @state/state)
[refs block]
(let [refs (remove #(= (:block/uuid block) (:block/uuid %)) refs)
cached-refs @(:editor/block-refs @state/state)
title->ref (zipmap (map :block/title cached-refs) cached-refs)]
(map (fn [x]
(if-let [ref (and (map? x) (title->ref (:block/title x)))]
@@ -102,7 +103,7 @@
(fn [refs]
(-> refs
remove-non-existed-refs!
use-cached-refs!)))))
(use-cached-refs! block))))))
result (-> block
(merge (if level {:block/level level} {}))
(replace-page-refs-with-ids))]