summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Lester <[email protected]>2021-08-01 20:58:17 -0400
committerBrian Lester <[email protected]>2021-08-01 20:58:17 -0400
commit82c5906d7bebfb65c8906802d939c8198aab87b8 (patch)
tree064f23c943b3b85e5889b3a9859fc881a8181dbb
parent197a3c7060945d422ccb1c3504bfda93791ea380 (diff)
Convert cite links with an associated node into id links
Currently the `org-roam-ui--send-graphdata` function gets all `id` and `cite` links from the database. They are all sent over the websocket, but nothing is done to handle the cite links, they don't appear in the graph. These are not handled because instead of being an (`id` `id` `type`) tuple, they are (`id` `ref` `type`). This change uses a join in the DB to get the `id` of the node that `ref` is associated with. A map function applied to the list of links converts any cite link whose `ref` has an associated node into an (`id` `id` `type`) tuple (changing the `type` from `"cite"` to `"id"`). This new link is correctly handled and appears in the graph.
-rw-r--r--org-roam-ui.el18
1 files changed, 16 insertions, 2 deletions
diff --git a/org-roam-ui.el b/org-roam-ui.el
index aca7e40..aae4446 100644
--- a/org-roam-ui.el
+++ b/org-roam-ui.el
@@ -153,9 +153,23 @@ This serves the web-build and API over HTTP."
(defun org-roam-ui--send-graphdata ()
"Get roam data, make JSON, send through websocket to org-roam-ui."
(let* ((nodes-columns [id file title level])
- (links-columns [source dest type])
+ (links-columns [links:source links:dest links:type refs:node-id])
(nodes-db-rows (org-roam-db-query `[:select ,nodes-columns :from nodes]))
- (links-db-rows (org-roam-db-query `[:select ,links-columns :from links :where (or (= type "id") (= type "cite"))]))
+ ;; Left outer join on refs means any id link (or cite link without a
+ ;; corresponding node) will have 'nil for the `refs:node-id' value. Any
+ ;; cite link where a node has that `:ROAM_REFS:' will have a value.
+ (links-db-rows (org-roam-db-query `[:select ,links-columns
+ :from links
+ :left :outer :join refs :on (= links:dest refs:ref)
+ :where (or (= links:type "id") (= links:type "cite"))]))
+ ;; Convert any cite links that have nodes with associated refs to a
+ ;; standard id link while removing the 'nil `refs:node-id' from all
+ ;; other links
+ (links-db-rows (seq-map (lambda (l)
+ (pcase-let ((`(,source ,dest ,type ,node-id) l))
+ (if node-id
+ (list source node-id "id")
+ (list source dest type)))) links-db-rows))
(response `((nodes . ,(mapcar (apply-partially #'org-roam-ui-sql-to-alist (append nodes-columns nil)) nodes-db-rows))
(links . ,(mapcar (apply-partially #'org-roam-ui-sql-to-alist '(source target type)) links-db-rows)))))
(websocket-send-text oru-ws (json-encode `((type . "graphdata") (data . ,response))))))