diff options
Diffstat (limited to 'org-roam-ui.el')
-rw-r--r-- | org-roam-ui.el | 79 |
1 files changed, 55 insertions, 24 deletions
diff --git a/org-roam-ui.el b/org-roam-ui.el index 61b8ea6..e1431ff 100644 --- a/org-roam-ui.el +++ b/org-roam-ui.el @@ -35,8 +35,8 @@ (require 'json) (require 'simple-httpd) (require 'org-roam) -(require 'org-roam-dailies) (require 'websocket) +(require 'org-roam-dailies) (defgroup org-roam-ui nil "UI in Org-roam." @@ -145,6 +145,13 @@ Take ID as string as sole argument." :group 'org-roam-ui :type 'hook) +(defcustom org-roam-ui-latex-macros nil + "Alist of LaTeX macros to be passed to org-roam-ui. +Format as, i.e. with double backslashes for a single backslash: +'((\"\\macro\".\"\\something{#1}\"))" + :group 'org-roam-ui + :type 'alist) + ;; Internal vars (defvar org-roam-ui--ws-current-node nil @@ -269,32 +276,33 @@ TODO: Be able to delete individual nodes." (org-roam-ui-follow-mode -1) (message "Connection with org-roam-ui closed.")) -(defun org-roam-ui--send-text (id ws) - "Send the text from org-node ID through the websocket WS." +(defun org-roam-ui--get-text (id) + "Retrieve the text from org-node ID." (let* ((node (org-roam-populate (org-roam-node-create :id id))) - (file (org-roam-node-file node)) - (text)) + (file (org-roam-node-file node))) (org-roam-with-temp-buffer file - (setq text - (buffer-substring-no-properties (buffer-end -1) (buffer-end 1))) - text) + (when (> (org-roam-node-level node) 0) + ;; Heading nodes have level 1 and greater. + (goto-char (org-roam-node-point node)) + (org-narrow-to-element)) + (buffer-substring-no-properties (buffer-end -1) (buffer-end 1))))) + +(defun org-roam-ui--send-text (id ws) + "Send the text from org-node ID through the websocket WS." + (let ((text (org-roam-ui--get-text id))) (websocket-send-text ws (json-encode `((type . "orgText") (data . ,text)))))) -(defservlet* file/:file text/plain () - "Servlet for accessing file contents of org-roam files. - -Just sends the complete content of org-roam files rather than the specific -node, as it's much faster to do that on the UI side." - (insert-file-contents-literally (org-link-decode file)) +(defservlet* node/:id text/plain () + "Servlet for accessing node content." + (insert (org-roam-ui--get-text (org-link-decode id))) (httpd-send-header t "text/plain" 200 :Access-Control-Allow-Origin "*")) - (defservlet* img/:file text/plain () "Servlet for accessing images found in org-roam files." (progn @@ -306,6 +314,7 @@ node, as it's much faster to do that on the UI side." TODO: Make this only send the changes to the graph data, not the complete graph." (when (org-roam-buffer-p) + (org-roam-ui--send-variables org-roam-ui-ws-socket) (org-roam-ui--send-graphdata))) (defun org-roam-ui--check-orb-keywords () @@ -565,19 +574,29 @@ from all other links." (defun org-roam-ui--send-variables (ws) "Send miscellaneous org-roam variables through the websocket WS." - (when (boundp 'org-roam-dailies-directory) - (let ((daily-dir (if (file-name-absolute-p org-roam-dailies-directory) - (expand-file-name org-roam-dailies-directory) - (expand-file-name - org-roam-dailies-directory - org-roam-directory)))) - (websocket-send-text ws + (let ((daily-dir (if (boundp 'org-roam-dailies-directory) + (if (file-name-absolute-p org-roam-dailies-directory) + (expand-file-name org-roam-dailies-directory) + (expand-file-name + org-roam-dailies-directory + org-roam-directory)) + "/dailies")) + (attach-dir (if (boundp 'org-attach-id-dir) + org-attach-id-dir + (expand-file-name ".attach/" org-directory))) + (sub-dirs (org-roam-ui-find-subdirectories))) + (websocket-send-text org-roam-ui-ws-socket (json-encode `((type . "variables") (data . - (("dailyDir" . + (("subDirs". + ,sub-dirs) + ("dailyDir" . ,daily-dir) - ("roamDir" . ,org-roam-directory))))))))) + ("attachDir" . + ,attach-dir) + ("roamDir" . ,org-roam-directory) + ("katexMacros" . ,org-roam-ui-latex-macros)))))))) (defun org-roam-ui-sql-to-alist (column-names rows) "Convert sql result to alist for json encoding. @@ -610,6 +629,18 @@ ROWS is the sql result, while COLUMN-NAMES is the columns to use." `(violet . ,(face-foreground font-lock-constant-face)) `(magenta . ,(face-foreground font-lock-preprocessor-face)))) +(defun org-roam-ui-find-subdirectories () + "Find all the subdirectories in the org-roam directory. +TODO: Exclude org-attach dirs." + (seq-filter + (lambda (file) (and (file-directory-p file) (org-roam-ui-allowed-directory-p file))) + (directory-files-recursively org-roam-directory + ".*" t #'org-roam-ui-allowed-directory-p))) + +(defun org-roam-ui-allowed-directory-p (dir) + "Check whether a DIR should be listed as a filterable dir. +Hides . directories." + (not (string-match-p "\\(\/\\|\\\\\\)\\..*?" dir))) ;;;; interactive commands |