impl 010-logseq-cli-show-linked-references.md (2)

This commit is contained in:
rcmerci
2026-01-20 22:35:46 +08:00
parent 5d6e46e46d
commit 33b8a3207d
4 changed files with 118 additions and 40 deletions

View File

@@ -40,7 +40,7 @@
[:db/id
:block/uuid
:block/title
:block/marker
{:logseq.property/status [:db/ident :block/name :block/title]}
:block/order
{:block/parent [:db/id]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}])
@@ -49,7 +49,7 @@
[:db/id
:block/uuid
:block/title
:block/marker
{:logseq.property/status [:db/ident :block/name :block/title]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}
{:block/page [:db/id :block/name :block/title :block/uuid]}
{:block/parent [:db/id
@@ -86,13 +86,32 @@
(when (seq labels)
(string/join " " (map #(str "#" %) labels)))))
(defn- status-from-ident
[ident]
(let [name* (name ident)
parts (string/split name* #"\.")
status (or (last parts) name*)]
(string/upper-case status)))
(defn- status-label
[node]
(let [status (:logseq.property/status node)]
(cond
(string? status) (when (seq status) status)
(keyword? status) (status-from-ident status)
(map? status) (or (:block/title status)
(:block/name status)
(when-let [ident (:db/ident status)]
(status-from-ident ident)))
:else nil)))
(defn- block-label
[node]
(let [title (:block/title node)
marker (:block/marker node)
status (status-label node)
uuid->label (:uuid->label node)
base (cond
(and title (seq marker)) (str marker " " title)
(and title (seq status)) (str status " " title)
title title
(:block/name node) (:block/name node)
(:block/uuid node) (some-> (:block/uuid node) str))
@@ -121,15 +140,22 @@
ref-ids))
[])]
(let [blocks (vec (remove nil? pulled))
page-lookup-key (fn [value]
(cond
(map? value) (or (:db/id value)
(when-let [uuid (:block/uuid value)]
[:block/uuid uuid]))
(number? value) value
(uuid? value) [:block/uuid value]
(and (string? value) (common-util/uuid-string? value)) [:block/uuid (uuid value)]
:else nil))
page-id-from (fn [block]
(let [page (:block/page block)
parent (:block/parent block)
parent-page (:block/page parent)]
(or (when (map? page) (:db/id page))
(when (number? page) page)
(when (map? parent-page) (:db/id parent-page))
(when (number? parent-page) parent-page)
(when (map? parent) (:db/id parent)))))
(or (page-lookup-key page)
(page-lookup-key parent-page)
(page-lookup-key parent))))
page-ids (->> blocks
(keep page-id-from)
distinct
@@ -257,7 +283,8 @@
(cond
(some? id)
(p/let [entity (transport/invoke config :thread-api/pull false
[repo [:db/id :block/name :block/uuid :block/title :block/marker
[repo [:db/id :block/name :block/uuid :block/title
{:logseq.property/status [:db/ident :block/name :block/title]}
{:block/page [:db/id :block/title]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}] id])]
(if-let [page-id (get-in entity [:block/page :db/id])]
@@ -274,14 +301,16 @@
(if-not (common-util/uuid-string? uuid-str)
(p/rejected (ex-info "block must be a uuid" {:code :invalid-block}))
(p/let [entity (transport/invoke config :thread-api/pull false
[repo [:db/id :block/name :block/uuid :block/title :block/marker
[repo [:db/id :block/name :block/uuid :block/title
{:logseq.property/status [:db/ident :block/name :block/title]}
{:block/page [:db/id :block/title]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}]
[:block/uuid (uuid uuid-str)]])
entity (if (:db/id entity)
entity
(transport/invoke config :thread-api/pull false
[repo [:db/id :block/name :block/uuid :block/title :block/marker
[repo [:db/id :block/name :block/uuid :block/title
{:logseq.property/status [:db/ident :block/name :block/title]}
{:block/page [:db/id :block/title]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}]
[:block/uuid uuid-str]]))]
@@ -297,7 +326,8 @@
(seq page-name)
(p/let [page-entity (transport/invoke config :thread-api/pull false
[repo [:db/id :block/uuid :block/title :block/marker
[repo [:db/id :block/uuid :block/title
{:logseq.property/status [:db/ident :block/name :block/title]}
{:block/tags [:db/id :block/name :block/title :block/uuid]}]
[:block/name page-name]])]
(if-let [page-id (:db/id page-entity)]

View File

@@ -204,27 +204,30 @@
"175 └── cccc")
(tree->text tree-data))))))
(deftest test-tree->text-prefixes-marker
(testing "show tree text prefixes markers before block titles"
(deftest test-tree->text-prefixes-status
(testing "show tree text prefixes status before block titles"
(let [tree->text #'show-command/tree->text
tree-data {:root {:db/id 1
:block/title "Root"
:block/marker "TODO"
:logseq.property/status {:db/ident :logseq.property/status.todo
:block/title "TODO"}
:block/children [{:db/id 2
:block/title "Child"
:block/marker "CANCELED"}]}}]
:logseq.property/status {:db/ident :logseq.property/status.canceled
:block/title "CANCELED"}}]}}]
(is (= (str "1 TODO Root\n"
"2 └── CANCELED Child")
(tree->text tree-data))))))
(deftest test-tree->text-marker-multiline-alignment
(testing "show tree text keeps multiline alignment when marker prefix is present"
(deftest test-tree->text-status-multiline-alignment
(testing "show tree text keeps multiline alignment when status prefix is present"
(let [tree->text #'show-command/tree->text
tree-data {:root {:db/id 1
:block/title "Root"
:block/children [{:db/id 22
:block/title "line1\nline2"
:block/marker "TODO"}]}}]
:logseq.property/status {:db/ident :logseq.property/status.todo
:block/title "TODO"}}]}}]
(is (= (str "1 Root\n"
"22 └── TODO line1\n"
" line2")
@@ -238,7 +241,8 @@
:linked-references {:count 2
:blocks [{:db/id 10
:block/title "Ref A"
:block/marker "TODO"
:logseq.property/status {:db/ident :logseq.property/status.todo
:block/title "TODO"}
:block/page {:db/id 100
:block/title "Page A"}}
{:db/id 11

View File

@@ -125,6 +125,50 @@
(is false (str "unexpected error: " e))
(done)))))))
(deftest test-cli-show-linked-references-json
(async done
(let [data-dir (node-helper/create-tmp-dir "db-worker-linked-refs")]
(-> (p/let [cfg-path (node-path/join (node-helper/create-tmp-dir "cli") "cli.edn")
_ (fs/writeFileSync cfg-path "{:output-format :json}")
_ (run-cli ["graph" "create" "--repo" "linked-refs-graph"] data-dir cfg-path)
_ (run-cli ["--repo" "linked-refs-graph" "add" "page" "--page" "TargetPage"] data-dir cfg-path)
_ (run-cli ["--repo" "linked-refs-graph" "add" "page" "--page" "SourcePage"] data-dir cfg-path)
target-show-before (run-cli ["--repo" "linked-refs-graph" "show" "--page-name" "TargetPage" "--format" "json"] data-dir cfg-path)
target-before-payload (parse-json-output target-show-before)
target-uuid (or (get-in target-before-payload [:data :root :block/uuid])
(get-in target-before-payload [:data :root :uuid]))
ref-content (str "See [[" target-uuid "]]")
_ (run-cli ["--repo" "linked-refs-graph" "add" "block" "--target-page-name" "SourcePage" "--content" ref-content] data-dir cfg-path)
source-show (run-cli ["--repo" "linked-refs-graph" "show" "--page-name" "SourcePage" "--format" "json"] data-dir cfg-path)
source-payload (parse-json-output source-show)
ref-node (find-block-by-title (get-in source-payload [:data :root]) ref-content)
ref-uuid (or (:block/uuid ref-node) (:uuid ref-node))
target-show (run-cli ["--repo" "linked-refs-graph" "show" "--page-name" "TargetPage" "--format" "json"] data-dir cfg-path)
target-payload (parse-json-output target-show)
linked-refs (get-in target-payload [:data :linked-references])
linked-blocks (:blocks linked-refs)
linked-uuids (set (map (fn [block]
(or (:block/uuid block) (:uuid block)))
linked-blocks))
linked-page-titles (set (keep (fn [block]
(or (get-in block [:block/page :block/title])
(get-in block [:block/page :block/name])
(get-in block [:page :title])
(get-in block [:page :name])))
linked-blocks))
stop-result (run-cli ["server" "stop" "--repo" "linked-refs-graph"] data-dir cfg-path)
stop-payload (parse-json-output stop-result)]
(is (some? target-uuid))
(is (= "ok" (:status target-payload)))
(is (some? ref-uuid))
(is (contains? linked-uuids ref-uuid))
(is (contains? linked-page-titles "SourcePage"))
(is (= "ok" (:status stop-payload)))
(done))
(p/catch (fn [e]
(is false (str "unexpected error: " e))
(done)))))))
(deftest test-cli-move-block
(async done
(let [data-dir (node-helper/create-tmp-dir "db-worker-move")]