Add support for block refs in page/block props

pull/8714/head
Gabriel Horner 2023-02-22 16:04:34 -05:00 committed by Gabriel Horner
parent bdc11a9dcd
commit 6506c369e8
2 changed files with 64 additions and 17 deletions

View File

@ -181,6 +181,20 @@
(remove string/blank?)
distinct)))
(defn- extract-block-refs
[nodes]
(let [ref-blocks (atom nil)]
(walk/postwalk
(fn [form]
(when-let [block (get-block-reference form)]
(swap! ref-blocks conj block))
form)
nodes)
(keep (fn [block]
(when-let [id (parse-uuid block)]
[:block/uuid id]))
@ref-blocks)))
(defn extract-properties
[properties user-config]
(when (seq properties)
@ -202,9 +216,10 @@
v' (text/parse-property k v mldoc-ast user-config)]
[k' v' mldoc-ast v])
(do (swap! *invalid-properties conj k)
nil)))))
nil)))))
(remove #(nil? (second %))))
page-refs (get-page-ref-names-from-properties properties user-config)
block-refs (extract-block-refs properties)
properties-text-values (->> (map (fn [[k _v _refs original-text]] [k original-text]) properties)
(into {}))
properties (map (fn [[k v _]] [k v]) properties)
@ -213,7 +228,8 @@
:properties-order (map first properties)
:properties-text-values properties-text-values
:invalid-properties @*invalid-properties
:page-refs page-refs})))
:page-refs page-refs
:block-refs block-refs})))
(defn- paragraph-timestamp-block?
[block]
@ -351,19 +367,9 @@
(defn- with-block-refs
[{:keys [title body] :as block}]
(let [ref-blocks (atom nil)]
(walk/postwalk
(fn [form]
(when-let [block (get-block-reference form)]
(swap! ref-blocks conj block))
form)
(concat title body))
(let [ref-blocks (keep (fn [block]
(when-let [id (parse-uuid block)]
[:block/uuid id]))
@ref-blocks)
refs (distinct (concat (:refs block) ref-blocks))]
(assoc block :refs refs))))
(let [ref-blocks (extract-block-refs (concat title body))
refs (distinct (concat (:refs block) ref-blocks))]
(assoc block :refs refs)))
(defn- block-keywordize
[block]
@ -532,8 +538,9 @@
:unordered true
:macros (extract-macros-from-ast body)
:body body}
block (with-page-block-refs block false supported-formats db date-formatter)]
(block-keywordize block))
block (with-page-block-refs block false supported-formats db date-formatter)
block' (update block :refs concat (:block-refs pre-block-properties))]
(block-keywordize block'))
(select-keys first-block [:block/format :block/page]))
blocks)
blocks)]
@ -584,6 +591,7 @@
block)
block (assoc block :body body)
block (with-page-block-refs block with-id? supported-formats db date-formatter)
block (update block :refs concat (:block-refs properties))
{:keys [created-at updated-at]} (:properties properties)
block (cond-> block
(and created-at (integer? created-at))

View File

@ -1,6 +1,10 @@
(ns logseq.graph-parser.block-test
(:require [logseq.graph-parser.block :as gp-block]
[logseq.graph-parser.mldoc :as gp-mldoc]
[logseq.graph-parser :as graph-parser]
[logseq.db :as ldb]
[logseq.graph-parser.util.block-ref :as block-ref]
[datascript.core :as d]
[cljs.test :refer [deftest are testing is]]))
(defn- extract-properties
@ -105,3 +109,38 @@
[["tags" "[[foo]], [[bar]]"] ["background-color" "#008000"]]
{:property-pages/enabled? true})))
"Only editable linkable built-in properties have page-refs in property values")))
(deftest refs-from-block-refs
(let [conn (ldb/start-conn)
id "63f528da-284a-45d1-ac9c-5d6a7435f6b4"
block (str "A block\nid:: " id)
block-ref-via-content (str "Link to " (block-ref/->block-ref id))
block-ref-via-block-properties (str "B block\nref:: " (block-ref/->block-ref id))
body (str "- " block "\n- " block-ref-via-content "\n- " block-ref-via-block-properties)
find-block-for-content (fn [content]
(->> (d/q '[:find (pull ?b [:block/content {:block/refs [:block/uuid]}])
:in $ ?content
:where [?b :block/content ?content]]
@conn
content)
(map first)
first))]
(graph-parser/parse-file conn "foo.md" body {})
(testing "Block refs in blocks"
(is (= [{:block/uuid (uuid id)}]
(:block/refs (find-block-for-content block-ref-via-content)))
"Block that links to a block via paragraph content has correct block ref")
(is (contains?
(set (:block/refs (find-block-for-content block-ref-via-block-properties)))
{:block/uuid (uuid id)})
"Block that links to a block via block properties has correct block ref"))
(testing "Block refs in pre-block"
(let [block-ref-via-page-properties (str "page-ref:: " (block-ref/->block-ref id))]
(graph-parser/parse-file conn "foo2.md" block-ref-via-page-properties {})
(is (contains?
(set (:block/refs (find-block-for-content block-ref-via-page-properties)))
{:block/uuid (uuid id)})
"Block that links to a block via page properties has correct block ref")))))