(feat): add org-roam-buffer-preview-function (#1388)

Instead of storing preview content in the database, now provide a
function to fetch these on the fly. This paves the way for future
improvements (e.g. showing more lines)
This commit is contained in:
Jethro Kuan
2021-01-17 02:52:39 +08:00
committed by GitHub
parent 06e5814898
commit 1b3a0abd36
2 changed files with 22 additions and 12 deletions

View File

@@ -36,6 +36,8 @@
(require 's)
(require 'f)
(require 'ol)
(require 'org-element)
(require 'org-roam-macs)
(defvar org-roam-directory)
(defvar org-link-frame-setup)
@@ -98,6 +100,13 @@ Has an effect if and only if `org-roam-buffer-position' is `top' or `bottom'."
:type 'hook
:group 'org-roam)
(defcustom org-roam-buffer-preview-function #'org-roam-buffer--preview
"Function to obtain preview contents for a given link.
The function takes in two arguments, the FILE containing the
link, and the POINT of the link."
:type 'function
:group 'org-roam)
(defcustom org-roam-buffer-window-parameters nil
"Additional window parameters for the `org-roam-buffer' side window.
For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
@@ -124,6 +133,16 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
'font-lock-face
'org-document-title)))
(defun org-roam-buffer--preview (file point)
"Get preview content for FILE at POINT."
(org-roam--with-temp-buffer file
(goto-char point)
(let ((elem (org-element-at-point)))
(or (org-element-property :raw-value elem)
(when-let ((begin (org-element-property :begin elem))
(end (org-element-property :end elem)))
(string-trim (buffer-substring-no-properties begin end)))))))
(defun org-roam-buffer--pluralize (string number)
"Conditionally pluralize STRING if NUMBER is above 1."
(let ((l (pcase number
@@ -171,7 +190,7 @@ ORIG-PATH is the path where the CONTENT originated."
"file")))
(dolist (backlink bls)
(pcase-let ((`(,file-from _ ,props) backlink))
(insert (if-let ((content (plist-get props :content)))
(insert (if-let ((content (funcall org-roam-buffer-preview-function file-from (plist-get props :point))))
(propertize (org-roam-buffer-expand-links content file-from)
'help-echo "mouse-1: visit backlinked note"
'file-from file-from
@@ -208,7 +227,7 @@ ORIG-PATH is the path where the CONTENT originated."
(org-roam-buffer-expand-links file-from))
"Top")
"\n"
(if-let ((content (plist-get prop :content)))
(if-let ((content (funcall org-roam-buffer-preview-function file-from (plist-get prop :point))))
(propertize
(s-trim (s-replace "\n" " " (org-roam-buffer-expand-links content file-from)))
'help-echo "mouse-1: visit backlinked note"