(feat): graph: add cite links to graph (#439)

This commit is contained in:
Tim Quelch
2020-04-12 16:38:58 +10:00
committed by GitHub
parent 0e79bbb75a
commit 580a320c66
3 changed files with 41 additions and 5 deletions

View File

@ -12,6 +12,7 @@
* [#380][gh-380] Allow `org-roam-buffer-position` to also be `top` or `bottom` * [#380][gh-380] Allow `org-roam-buffer-position` to also be `top` or `bottom`
* [#385][gh-385] Add `org-roam-graph-node-extra-config` to configure Graphviz nodes * [#385][gh-385] Add `org-roam-graph-node-extra-config` to configure Graphviz nodes
* [#435][gh-435] Add `org-roam-graph-edge-extra-config` to configure Graphviz edges * [#435][gh-435] Add `org-roam-graph-edge-extra-config` to configure Graphviz edges
* [#439][gh-439] Add support for `org-ref` citations to display as edges in graph. Add `org-roam-graph-edge-cites-extra-config` to configure these edges
## 1.0.0 (23-03-2020) ## 1.0.0 (23-03-2020)

View File

@ -253,8 +253,14 @@ This is equivalent to removing the node from the graph."
If the file does not have any connections, nil is returned." If the file does not have any connections, nil is returned."
(let* ((query "WITH RECURSIVE (let* ((query "WITH RECURSIVE
links_of(file, link) AS links_of(file, link) AS
(SELECT \"from\", \"to\" FROM links UNION (WITH roamlinks AS (SELECT * FROM links WHERE \"type\" = '\"roam\"'),
SELECT \"to\", \"from\" FROM links), citelinks AS (SELECT * FROM links
JOIN refs ON links.\"to\" = refs.\"ref\"
AND links.\"type\" = '\"cite\"')
SELECT \"from\", \"to\" FROM roamlinks UNION
SELECT \"to\", \"from\" FROM roamlinks UNION
SELECT \"file\", \"from\" FROM citelinks UNION
SELECT \"from\", \"file\" FROM citelinks),
connected_component(file) AS connected_component(file) AS
(SELECT link FROM links_of WHERE file = $s1 (SELECT link FROM links_of WHERE file = $s1
UNION UNION
@ -268,8 +274,14 @@ If the file does not have any connections, nil is returned."
including the file itself. If the file does not have any connections, nil is returned." including the file itself. If the file does not have any connections, nil is returned."
(let* ((query "WITH RECURSIVE (let* ((query "WITH RECURSIVE
links_of(file, link) AS links_of(file, link) AS
(SELECT \"from\", \"to\" FROM links UNION (WITH roamlinks AS (SELECT * FROM links WHERE \"type\" = '\"roam\"'),
SELECT \"to\", \"from\" FROM links), citelinks AS (SELECT * FROM links
JOIN refs ON links.\"to\" = refs.\"ref\"
AND links.\"type\" = '\"cite\"')
SELECT \"from\", \"to\" FROM roamlinks UNION
SELECT \"to\", \"from\" FROM roamlinks UNION
SELECT \"file\", \"from\" FROM citelinks UNION
SELECT \"from\", \"file\" FROM citelinks),
-- Links are traversed in a breadth-first search. In order to calculate the -- Links are traversed in a breadth-first search. In order to calculate the
-- distance of nodes and to avoid following cyclic links, the visited nodes -- distance of nodes and to avoid following cyclic links, the visited nodes
-- are tracked in 'trace'. -- are tracked in 'trace'.

View File

@ -74,6 +74,13 @@ Example:
:type '(alist) :type '(alist)
:group 'org-roam) :group 'org-roam)
(defcustom org-roam-graph-edge-cites-extra-config '(("color" . "red"))
"Extra options for graphviz edges for citation links.
Example:
'((\"dir\" . \"back\"))"
:type '(alist)
:group 'org-roam)
(defcustom org-roam-graph-max-title-length 100 (defcustom org-roam-graph-max-title-length 100
"Maximum length of titles in graph nodes." "Maximum length of titles in graph nodes."
:type 'number :type 'number
@ -139,7 +146,13 @@ into a digraph."
`[:with selected :as [:select [file] :from ,node-query] `[:with selected :as [:select [file] :from ,node-query]
:select :distinct [to from] :from links :select :distinct [to from] :from links
:where (and (in to selected) (in from selected))]) :where (and (in to selected) (in from selected))])
(edges (org-roam-db-query edges-query))) (edges-cites-query
`[:with selected :as [:select [file] :from ,node-query]
:select :distinct [file from]
:from links :inner :join refs :on (and (= links:to refs:ref) (= links:type "cite"))
:where (and (in file selected) (in from selected))])
(edges (org-roam-db-query edges-query))
(edges-cites (org-roam-db-query edges-cites-query)))
(insert "digraph \"org-roam\" {\n") (insert "digraph \"org-roam\" {\n")
(dolist (option org-roam-graph-extra-config) (dolist (option org-roam-graph-extra-config)
@ -179,6 +192,16 @@ into a digraph."
(insert (format " \"%s\" -> \"%s\";\n" (insert (format " \"%s\" -> \"%s\";\n"
(xml-escape-string (car edge)) (xml-escape-string (car edge))
(xml-escape-string (cadr edge))))) (xml-escape-string (cadr edge)))))
(insert (format " edge [%s];\n"
(->> org-roam-graph-edge-cites-extra-config
(mapcar (lambda (n)
(concat (car n) "=" (cdr n))))
(s-join ","))))
(dolist (edge edges-cites)
(insert (format " \"%s\" -> \"%s\";\n"
(xml-escape-string (car edge))
(xml-escape-string (cadr edge)))))
(insert "}") (insert "}")
(buffer-string)))) (buffer-string))))