From 073bbeb90ae8f6c4d78a1b52eaed18e906e55fbc Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Tue, 26 May 2026 17:12:28 +0800 Subject: [PATCH] fix: keep canonical page title in search results --- src/main/frontend/worker/search.cljs | 14 ++++++++--- src/test/frontend/worker/search_test.cljs | 30 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/frontend/worker/search.cljs b/src/main/frontend/worker/search.cljs index 8c130aac69..59e69e8ae2 100644 --- a/src/main/frontend/worker/search.cljs +++ b/src/main/frontend/worker/search.cljs @@ -108,8 +108,6 @@ DROP TRIGGER IF EXISTS blocks_au; (defn- throw-upsert-blocks-error! [item] - (js/console.error "Upsert blocks wrong data: ") - (js/console.dir item) (throw (ex-info "Search upsert-blocks wrong data: " (bean/->clj item)))) @@ -537,6 +535,10 @@ DROP TRIGGER IF EXISTS blocks_au; (string/join " ")) title))) +(defn- block-result-title + [block] + (db-content/recur-replace-uuid-in-block-title block)) + (defn- matched-alias [q block] (when-not (string/blank? q) @@ -670,9 +672,13 @@ DROP TRIGGER IF EXISTS blocks_au; (let [alias-source (some-> (first (:block/_alias block)) (select-keys [:block/uuid :block/title])) alias-match (matched-alias q block) + page-or-object-result? (page-or-object? block) + result-title (if page-or-object-result? (block-result-title block) title) display-title (if (:enable-snippet? option) - (ensure-highlighted-snippet snippet title q) - (or snippet title)) + (ensure-highlighted-snippet snippet result-title q) + (if page-or-object-result? + result-title + (or snippet title))) block-page (or (:block/uuid (:block/page block)) (when (and page (common-util/uuid-string? page)) diff --git a/src/test/frontend/worker/search_test.cljs b/src/test/frontend/worker/search_test.cljs index d7cdbb383b..5e1188ea91 100644 --- a/src/test/frontend/worker/search_test.cljs +++ b/src/test/frontend/worker/search_test.cljs @@ -618,6 +618,36 @@ (is (= "Artificial Intelligence" (:block/title result))) (is (not (contains? result :alias)))))))) +(deftest search-result-replaces-page-title-uuid-refs-without-alias-title + (testing "page result titles resolve uuid refs but do not display alias titles from the search index" + (let [page-id #uuid "00000000-0000-0000-0000-000000000246" + ref-id #uuid "00000000-0000-0000-0000-000000000247" + page {:db/id 1 + :block/uuid page-id + :block/title (str "Artificial [[" ref-id "]]") + :block/refs [{:block/uuid ref-id + :block/title "Machine Learning"}] + :block/alias [{:db/id 2 + :block/uuid #uuid "00000000-0000-0000-0000-000000000248" + :block/title "ai"}]}] + (with-redefs [d/entity (fn [_db [_attr id]] + (when (= id page-id) + page)) + ldb/page? (fn [entity] (= (:db/id entity) (:db/id page))) + ldb/object? (constantly false) + ldb/built-in? (constantly false) + ldb/hidden? (constantly false)] + (let [result (#'search/search-result->block-result + (atom :db) + "Artificial" + nil + {:enable-snippet? false} + {:id (str page-id) + :page (str page-id) + :title "Artificial [[Machine Learning]] ai"})] + (is (= "Artificial [[Machine Learning]]" (:block/title result))) + (is (not (contains? result :alias)))))))) + (deftest search-result-snippet-uses-canonical-page-title (testing "page snippets do not display alias titles from the search index" (let [page-id #uuid "00000000-0000-0000-0000-000000000242"