Last active
July 29, 2021 20:47
-
-
Save rasendubi/0519d4033b7b838bba86459e26660935 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'cl) | |
(require 'el-patch) | |
(defvar rasen/org-roam-graph-exclude-node (lambda (id node type) nil) | |
"Function to exclude nodes from org-roam-graph.") | |
;; exclude all id nodes that are not known to org-roam (e.g., excluded) | |
;; (setq rasen/org-roam-graph-exclude-node | |
;; (lambda (id node type) | |
;; (and (string-equal type "id") | |
;; (not node)))) | |
;; exclude nodes from a directory | |
;; (setq rasen/org-roam-graph-exclude-node | |
;; (lambda (id node type) | |
;; (and (string-equal type "id") | |
;; node | |
;; (string-prefix-p (expand-file-name "private/" org-roam-directory) | |
;; (org-roam-node-file node))))) | |
;; exclude nodes based on `org-roam-graph-exclude-matcher' | |
;; (setq org-roam-graph-exclude-matcher '("private" "dailies")) | |
;; (setq rasen/org-roam-graph-exclude-node | |
;; (lambda (id node type) | |
;; (and (string-equal type "id") | |
;; node | |
;; (let ((path (org-roam-node-file node))) | |
;; (some (lambda (matcher) (string-match matcher path)) | |
;; org-roam-graph-exclude-matcher))))) | |
(defun rasen/org-roam-graph--filter-edges (edges &optional nodes-table) | |
(let ((nodes-table (or nodes-table (org-roam--nodes-table)))) | |
(seq-filter (pcase-lambda (`(,source ,dest ,type)) | |
(let ((source-node (gethash source nodes-table)) | |
(dest-node (gethash dest nodes-table))) | |
(not (or (funcall rasen/org-roam-graph-exclude-node source source-node "id") | |
(funcall rasen/org-roam-graph-exclude-node dest dest-node type))))) | |
edges))) | |
(el-patch-defun org-roam-graph--dot (&optional edges all-nodes) | |
"Build the graphviz given the EDGES of the graph. | |
If ALL-NODES, include also nodes without edges." | |
(let ((org-roam-directory-temp org-roam-directory) | |
(nodes-table (org-roam--nodes-table)) | |
(seen-nodes (list)) | |
(edges (el-patch-let (($orig (or edges (org-roam-db-query [:select :distinct [source dest type] :from links])))) | |
(el-patch-swap | |
$orig | |
(rasen/org-roam-graph--filter-edges $orig))))) | |
(with-temp-buffer | |
(setq-local org-roam-directory org-roam-directory-temp) | |
(insert "digraph \"org-roam\" {\n") | |
(dolist (option org-roam-graph-extra-config) | |
(insert (org-roam-graph--dot-option option) ";\n")) | |
(insert (format " edge [%s];\n" | |
(mapconcat (lambda (var) | |
(org-roam-graph--dot-option var nil "\"")) | |
org-roam-graph-edge-extra-config | |
","))) | |
(pcase-dolist (`(,source ,dest ,type) edges) | |
(unless (member type org-roam-graph-link-hidden-types) | |
(pcase-dolist (`(,node ,node-type) `((,source "id") | |
(,dest ,type))) | |
(unless (member node seen-nodes) | |
(insert (org-roam-graph--format-node | |
(or (gethash node nodes-table) node) node-type)) | |
(push node seen-nodes))) | |
(insert (format " \"%s\" -> \"%s\";\n" | |
(xml-escape-string source) | |
(xml-escape-string dest))))) | |
(when all-nodes | |
(maphash (lambda (id node) | |
(unless (el-patch-let (($orig (member id seen-nodes))) | |
(el-patch-swap | |
$orig | |
(or $orig | |
(funcall rasen/org-roam-graph-exclude-node id node "id")))) | |
(insert (org-roam-graph--format-node node "id")))) | |
nodes-table)) | |
(insert "}") | |
(buffer-string)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment