Skip to content

Commit a4ec889

Browse files
committed
fix: some built-in tags shouldn't be recognized as inline tags
related to logseq/db-test#555
1 parent a3b2ac9 commit a4ec889

File tree

9 files changed

+138
-101
lines changed

9 files changed

+138
-101
lines changed

deps/db/src/logseq/db.cljs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@
129129
;; notify ui
130130
(when-let [f @*transact-invalid-callback]
131131
(f tx-report errors))
132-
(throw (ex-info "DB write failed with invalid data" {:tx-data tx-data}))))
132+
(throw (ex-info "DB write failed with invalid data" {:tx-data tx-data
133+
:pipeline-tx-data (:tx-data tx-report)}))))
133134
tx-report)
134135
(d/transact! conn tx-data tx-meta)))
135136
(catch :default e

deps/db/src/logseq/db/frontend/class.cljs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,24 @@
117117
#{:logseq.class/Journal :logseq.class/Whiteboard
118118
:logseq.class/Pdf-annotation}))
119119

120+
(def block-kind-tags
121+
#{:logseq.class/Cards :logseq.class/Code-block
122+
:logseq.class/Math-block :logseq.class/Quote-block
123+
:logseq.class/Query :logseq.class/Pdf-annotation
124+
:logseq.class/Template})
125+
126+
(def disallowed-inline-tags
127+
"Classes that should be removed from inline tags"
128+
(set/union page-classes
129+
private-tags
130+
block-kind-tags))
131+
120132
(def extends-hidden-tags
121133
"Built-in classes that are hidden when choosing extends"
122134
(set/union
123135
private-tags
124-
#{:logseq.class/Cards :logseq.class/Code-block
125-
:logseq.class/Math-block :logseq.class/Pdf-annotation
126-
:logseq.class/Query :logseq.class/Quote-block
127-
:logseq.class/Template}))
136+
block-kind-tags))
137+
128138
(def hidden-tags
129139
"Built-in classes that are hidden in a few contexts like property values"
130140
#{:logseq.class/Page :logseq.class/Root :logseq.class/Asset})

deps/outliner/src/logseq/outliner/core.cljs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,26 @@
237237
[:db/retract eid :block/tags :logseq.class/Page]])))
238238
tags))
239239

240-
(defn- remove-inline-page-classes
240+
(defn- inline-tag-disallowed?
241+
[db t]
242+
;; both disallowed tags and built-in pages shouldn't be used as inline tags
243+
(let [disallowed-idents (into db-class/disallowed-inline-tags
244+
#{:logseq.property/query :logseq.property/asset})]
245+
(and (map? t)
246+
(or
247+
(contains?
248+
disallowed-idents
249+
(or (:db/ident t)
250+
(when-let [id (:block/uuid t)]
251+
(:db/ident (d/entity db [:block/uuid id])))))
252+
(contains?
253+
sqlite-create-graph/built-in-pages-names
254+
(or (:block/title t)
255+
(when-let [id (:block/uuid t)]
256+
(:block/title (d/entity db [:block/uuid id])))))))))
257+
258+
(defn- remove-disallowed-inline-classes
241259
[db {:block/keys [tags] :as block}]
242-
;; Notice: should check `page?` for block from the current db
243260
(if (or (ldb/page? (d/entity db (:db/id block))) (:block/name block))
244261
block
245262
(let [tags' (cond
@@ -253,28 +270,26 @@
253270
:else
254271
tags)
255272
block (assoc block :block/tags tags')
256-
page-class? (fn [t]
257-
(and (map? t) (contains? db-class/page-classes
258-
(or (:db/ident t)
259-
(when-let [id (:block/uuid t)]
260-
(:db/ident (d/entity db [:block/uuid id])))))))
261-
page-classes (filter page-class? tags')]
262-
(if (seq page-classes)
273+
disallowed-tag? (fn [tag] (inline-tag-disallowed? db tag))
274+
disallowed-tags (filter disallowed-tag? tags')]
275+
(if (seq disallowed-tags)
263276
(-> block
264277
(update :block/tags
265278
(fn [tags]
266-
(->> (remove page-class? tags)
279+
(->> (remove disallowed-tag? tags)
267280
(remove nil?))))
268281
(update :block/refs
269-
(fn [refs] (->> (remove page-class? refs)
282+
(fn [refs] (->> (remove disallowed-tag? refs)
270283
(remove nil?))))
271284
(update :block/title (fn [title]
272285
(reduce
273-
(fn [title page-class]
274-
(-> (string/replace title (str "#" (page-ref/->page-ref (:block/uuid page-class))) "")
286+
(fn [title tag]
287+
(-> (string/replace title
288+
(str "#" (page-ref/->page-ref (:block/uuid tag)))
289+
(str "#" (:block/title tag)))
275290
string/trim))
276291
title
277-
page-classes))))
292+
disallowed-tags))))
278293
block))))
279294

280295
(extend-type Entity
@@ -289,7 +304,7 @@
289304
this)
290305
data' (if db-based?
291306
(->> (dissoc data :block/properties)
292-
(remove-inline-page-classes db))
307+
(remove-disallowed-inline-classes db))
293308
data)
294309
collapse-or-expand? (= outliner-op :collapse-expand-blocks)
295310
m* (cond->
@@ -586,7 +601,7 @@
586601
orders (get-block-orders blocks target-block sibling? keep-block-order?)]
587602
(map-indexed (fn [idx {:block/keys [parent] :as block}]
588603
(when-let [uuid' (get uuids (:block/uuid block))]
589-
(let [block (if db-based? (remove-inline-page-classes db block) block)
604+
(let [block (if db-based? (remove-disallowed-inline-classes db block) block)
590605
top-level? (= (:block/level block) 1)
591606
parent (compute-block-parent block parent target-block top-level? sibling? get-new-id outliner-op replace-empty-target? idx)
592607

src/main/frontend/components/block.cljs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,11 @@
20122012
["Tag" _]
20132013
(when-let [s (gp-block/get-tag item)]
20142014
(let [s (text/page-ref-un-brackets! s)]
2015-
(page-cp (assoc config :tag? true) {:block/name s})))
2015+
(if (config/db-based-graph?)
2016+
(if (common-util/uuid-string? s)
2017+
(page-cp (assoc config :tag? true) {:block/name s})
2018+
[:span (str "#" s)])
2019+
(page-cp (assoc config :tag? true) {:block/name s}))))
20162020

20172021
["Emphasis" [[kind] data]]
20182022
(emphasis-cp config kind data)

src/main/frontend/handler/db_based/editor.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
(-> refs
6565
remove-non-existed-refs!
6666
(use-cached-refs! block))))))
67-
title' (db-content/title-ref->id-ref (:block/title block) (:block/refs block))
67+
title' (db-content/title-ref->id-ref (or (get block :block/title) title) (:block/refs block))
6868
result (-> block
6969
(merge (if level {:block/level level} {}))
7070
(assoc :block/title title'))]

src/main/frontend/handler/editor.cljs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,7 @@
14831483
(string/trim value)))
14841484
(save-block-aux! db-block value opts))))
14851485
(catch :default error
1486+
(js/console.error error)
14861487
(log/error :save-block-failed error)))))))
14871488

14881489
(defn- clean-content!

src/main/frontend/worker/db_worker.cljs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -492,18 +492,18 @@
492492
(def-thread-api :thread-api/block-refs-check
493493
[repo id {:keys [unlinked?]}]
494494
(m/sp
495-
(when-let [conn (worker-state/get-datascript-conn repo)]
496-
(let [db @conn
497-
block (d/entity db id)]
498-
(if unlinked?
499-
(let [title (string/lower-case (:block/title block))
500-
result (m/? (search-blocks repo title {:limit 100}))]
501-
(boolean (some (fn [b]
502-
(let [block (d/entity db (:db/id b))]
503-
(and (not= id (:db/id block))
504-
(not ((set (map :db/id (:block/refs block))) id))
505-
(string/includes? (string/lower-case (:block/title block)) title)))) result)))
506-
(some? (first (common-initial-data/get-block-refs db (:db/id block)))))))))
495+
(when-let [conn (worker-state/get-datascript-conn repo)]
496+
(let [db @conn
497+
block (d/entity db id)]
498+
(if unlinked?
499+
(let [title (string/lower-case (:block/title block))
500+
result (m/? (search-blocks repo title {:limit 100}))]
501+
(boolean (some (fn [b]
502+
(let [block (d/entity db (:db/id b))]
503+
(and (not= id (:db/id block))
504+
(not ((set (map :db/id (:block/refs block))) id))
505+
(string/includes? (string/lower-case (:block/title block)) title)))) result)))
506+
(some? (first (common-initial-data/get-block-refs db (:db/id block)))))))))
507507

508508
(def-thread-api :thread-api/get-block-parents
509509
[repo id depth]
@@ -642,7 +642,7 @@
642642
:notification
643643
(do
644644
(log/error ::apply-outliner-ops-failed e)
645-
(shared-service/broadcast-to-clients! :notification [(:message payload) (:type payload)]))
645+
(shared-service/broadcast-to-clients! :notification [(:message payload) (:type payload) (:clear? payload) (:uid payload) (:timeout payload)]))
646646
(throw e)))))))
647647

648648
(def-thread-api :thread-api/file-writes-finished?
@@ -881,14 +881,14 @@
881881
[repo start-opts]
882882
(js/Promise.
883883
(m/sp
884-
(c.m/<? (init-sqlite-module!))
885-
(when-not (:import-type start-opts)
886-
(c.m/<? (start-db! repo start-opts))
887-
(assert (some? (worker-state/get-datascript-conn repo))))
884+
(c.m/<? (init-sqlite-module!))
885+
(when-not (:import-type start-opts)
886+
(c.m/<? (start-db! repo start-opts))
887+
(assert (some? (worker-state/get-datascript-conn repo))))
888888
;; Don't wait for rtc started because the app will be slow to be ready
889889
;; for users.
890-
(when @worker-state/*rtc-ws-url
891-
(rtc.core/new-task--rtc-start true)))))
890+
(when @worker-state/*rtc-ws-url
891+
(rtc.core/new-task--rtc-start true)))))
892892

893893
(def broadcast-data-types
894894
(set (map

src/test/frontend/modules/outliner/core_test.cljs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns frontend.modules.outliner.core-test
2-
(:require [cljs.test :refer [deftest is use-fixtures testing] :as test]
2+
(:require [cljs.test :refer [deftest is testing use-fixtures] :as test]
33
[clojure.set :as set]
44
[clojure.test.check.generators :as gen]
55
[clojure.walk :as walk]
@@ -12,10 +12,7 @@
1212
[frontend.test.fixtures :as fixtures]
1313
[frontend.test.helper :as test-helper :refer [load-test-files]]
1414
[frontend.worker.db-listener :as worker-db-listener]
15-
[logseq.common.util :as common-util]
1615
[logseq.db :as ldb]
17-
[logseq.db.frontend.class :as db-class]
18-
[logseq.db.test.helper :as db-test]
1916
[logseq.graph-parser.block :as gp-block]
2017
[logseq.outliner.core :as outliner-core]
2118
[logseq.outliner.transaction :as outliner-tx]))
@@ -523,61 +520,6 @@
523520
(state/get-date-formatter)
524521
block)))
525522

526-
(deftest save-inline-tag
527-
(let [conn (db-test/create-conn-with-blocks
528-
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
529-
block (db-test/find-block-by-content @conn "test")
530-
_ (outliner-core/save-block! "logseq_db_test" conn
531-
"MMM do, yyyy"
532-
{:block/uuid (:block/uuid block)
533-
:block/refs '({:block/name "audio", :block/title "audio", :block/uuid #uuid "6852be3e-6e80-4245-b72c-0d586f1fd007", :block/created-at 1750253118663, :block/updated-at 1750253118663, :block/tags [:logseq.class/Page]}),
534-
:block/tags '({:block/name "audio", :block/title "audio", :block/uuid #uuid "6852be3e-6e80-4245-b72c-0d586f1fd007", :block/created-at 1750253118663, :block/updated-at 1750253118663, :block/tags [:logseq.class/Tag]}),
535-
:block/title "test #[[6852be3e-6e80-4245-b72c-0d586f1fd007]]",
536-
:db/id (:db/id block)})
537-
audio-tag (ldb/get-page @conn "audio")]
538-
(is (some? (:db/ident audio-tag)) "#audio doesn't have db/ident")
539-
(is (= [:logseq.class/Tag] (map :db/ident (:block/tags audio-tag)))
540-
"#audio has wrong tags")))
541-
542-
(deftest do-not-save-inline-page-tag-when-save-block
543-
(testing "Inline page class shouldn't be saved when save block"
544-
(let [conn (db-test/create-conn-with-blocks
545-
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
546-
block (db-test/find-block-by-content @conn "test")
547-
block' (d/entity @conn (:db/id block))]
548-
(doseq [class-ident db-class/page-classes]
549-
(let [class (d/entity @conn class-ident)]
550-
(outliner-core/save-block! "logseq_db_test" conn
551-
"MMM do, yyyy"
552-
{:block/uuid (:block/uuid block)
553-
:block/tags [(select-keys class [:block/name :block/title :block/uuid :db/ident])],
554-
:block/title (common-util/format "test #[[%s]]" (str (:block/uuid class))),
555-
:db/id (:db/id block)})
556-
(is (= "test" (:block/title block')))
557-
(is (empty? (:block/tags block'))))))))
558-
559-
(deftest do-not-save-inline-page-tag-when-insert-blocks
560-
(testing "Inline page class shouldn't be saved when insert blocks"
561-
(let [conn (db-test/create-conn-with-blocks
562-
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
563-
block (db-test/find-block-by-content @conn "test")]
564-
(doseq [class-ident db-class/page-classes]
565-
(let [class (d/entity @conn class-ident)
566-
new-block-id (random-uuid)
567-
_ (outliner-tx/transact!
568-
(transact-opts)
569-
(outliner-core/insert-blocks! "logseq_db_test" conn
570-
[{:block/uuid new-block-id
571-
:block/tags [(select-keys class [:block/name :block/title :block/uuid :db/ident])],
572-
:block/title (common-util/format "test #[[%s]]" (str (:block/uuid class))),
573-
:block/page (:db/id (:block/page block))}]
574-
block
575-
{:sibling? false
576-
:keep-uuid? true}))
577-
block' (d/entity @conn [:block/uuid new-block-id])]
578-
(is (= "test" (:block/title block')))
579-
(is (empty? (:block/tags block'))))))))
580-
581523
(deftest save-test
582524
(load-test-files [{:file/path "pages/page1.md"
583525
:file/content "alias:: foo, bar
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
(ns frontend.modules.outliner.inline-tag-test
2+
(:require [cljs.test :refer [use-fixtures deftest is testing] :as test]
3+
[datascript.core :as d]
4+
[frontend.test.helper :as test-helper]
5+
[logseq.common.util :as common-util]
6+
[logseq.db :as ldb]
7+
[logseq.db.frontend.class :as db-class]
8+
[logseq.db.test.helper :as db-test]
9+
[logseq.outliner.core :as outliner-core]))
10+
11+
(use-fixtures :each test-helper/db-based-start-and-destroy-db)
12+
13+
(deftest save-inline-tag
14+
(let [conn (db-test/create-conn-with-blocks
15+
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
16+
block (db-test/find-block-by-content @conn "test")
17+
_ (outliner-core/save-block! "logseq_db_test" conn
18+
"MMM do, yyyy"
19+
{:block/uuid (:block/uuid block)
20+
:block/refs '({:block/name "audio", :block/title "audio", :block/uuid #uuid "6852be3e-6e80-4245-b72c-0d586f1fd007", :block/created-at 1750253118663, :block/updated-at 1750253118663, :block/tags [:logseq.class/Page]}),
21+
:block/tags '({:block/name "audio", :block/title "audio", :block/uuid #uuid "6852be3e-6e80-4245-b72c-0d586f1fd007", :block/created-at 1750253118663, :block/updated-at 1750253118663, :block/tags [:logseq.class/Tag]}),
22+
:block/title "test #[[6852be3e-6e80-4245-b72c-0d586f1fd007]]",
23+
:db/id (:db/id block)})
24+
audio-tag (ldb/get-page @conn "audio")]
25+
(is (some? (:db/ident audio-tag)) "#audio doesn't have db/ident")
26+
(is (= [:logseq.class/Tag] (map :db/ident (:block/tags audio-tag)))
27+
"#audio has wrong tags")))
28+
29+
(deftest disallowed-inline-tags-when-save-block
30+
(testing "Disallowed inline tags shouldn't be recognized when save block"
31+
(let [conn (db-test/create-conn-with-blocks
32+
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
33+
block (db-test/find-block-by-content @conn "test")]
34+
(doseq [class-ident db-class/page-classes]
35+
(let [class (d/entity @conn class-ident)]
36+
(outliner-core/save-block! "logseq_db_test" conn
37+
"MMM do, yyyy"
38+
{:block/uuid (:block/uuid block)
39+
:block/tags [(select-keys class [:block/name :block/title :block/uuid :db/ident])],
40+
:block/title (common-util/format "test #[[%s]]" (str (:block/uuid class))),
41+
:db/id (:db/id block)})
42+
(let [block' (d/entity @conn (:db/id block))]
43+
(is (= (str "test #" (:block/title class)) (:block/title block')))
44+
(is (empty? (:block/tags block')))))))))
45+
46+
(deftest disallowed-inline-tags-when-insert-blocks
47+
(testing "Disallowed inline tags shouldn't be recognized when insert blocks"
48+
(let [conn (db-test/create-conn-with-blocks
49+
[{:page {:block/title "page1"} :blocks [{:block/title "test"}]}])
50+
block (db-test/find-block-by-content @conn "test")]
51+
(doseq [class-ident db-class/page-classes]
52+
(let [class (d/entity @conn class-ident)
53+
new-block-id (random-uuid)
54+
_ (outliner-core/insert-blocks! "logseq_db_test" conn
55+
[{:block/uuid new-block-id
56+
:block/tags [(select-keys class [:block/name :block/title :block/uuid :db/ident])],
57+
:block/title (common-util/format "test #[[%s]]" (str (:block/uuid class))),
58+
:block/page (:db/id (:block/page block))}]
59+
block
60+
{:sibling? false
61+
:keep-uuid? true})
62+
block' (d/entity @conn [:block/uuid new-block-id])]
63+
(is (= (str "test #" (:block/title class)) (:block/title block')))
64+
(is (empty? (:block/tags block'))))))))

0 commit comments

Comments
 (0)