From d77f897400b5e668e613fba269eb5ab92a7ed492 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Wed, 8 Jul 2020 12:29:06 +0800 Subject: [PATCH] (feat): support more ref links (#900) This adds support for all sorts of ref links (http, https, and any custom link types). If the ref is not a file or org-ref citation link, the full link path is used. Closes #744. --- CHANGELOG.md | 1 + org-roam-buffer.el | 13 ++++++------- org-roam.el | 36 ++++++++++++++---------------------- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b19fa7c..e1133f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - [#851](https://github.com/org-roam/org-roam/pull/851) Add `'first-directory'` option for `org-roam-tag-sources` - [#863](https://github.com/org-roam/org-roam/pull/863) Display outline hierarchy in backlinks buffer - [#898](https://github.com/org-roam/org-roam/pull/898) Add `org-roam-random-note` to browse a random note. +- [#900](https://github.com/org-roam/org-roam/pull/900) Support and index all valid org links ### Bugfixes diff --git a/org-roam-buffer.el b/org-roam-buffer.el index 2b0be9c..0044e27 100644 --- a/org-roam-buffer.el +++ b/org-roam-buffer.el @@ -84,7 +84,7 @@ Has an effect if and only if `org-roam-buffer-position' is `top' or `bottom'." (defcustom org-roam-buffer-prepare-hook '(org-roam-buffer--insert-title org-roam-buffer--insert-backlinks - org-roam-buffer--insert-citelinks) + org-roam-buffer--insert-ref-links) "Hook run in the `org-roam-buffer' before it is displayed." :type 'hook :group 'org-roam) @@ -123,10 +123,9 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))" ,wrong-type)))))) (concat string (when (> l 1) "s")))) -(defun org-roam-buffer--insert-citelinks () - "Insert citation backlinks for the current buffer." - (when-let ((org-ref-p (require 'org-ref nil t)) ;; Ensure that org-ref is present - (ref (cdr (with-temp-buffer +(defun org-roam-buffer--insert-ref-links () + "Insert ref backlinks for the current buffer." + (when-let ((ref (cdr (with-temp-buffer (insert-buffer-substring org-roam-buffer--current) (org-roam--extract-ref))))) (if-let* ((key-backlinks (org-roam--get-backlinks ref)) @@ -134,7 +133,7 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))" (progn (insert (let ((l (length key-backlinks))) (format "\n\n* %d %s\n" - l (org-roam-buffer--pluralize "Cite backlink" l)))) + l (org-roam-buffer--pluralize "Ref Backlink" l)))) (dolist (group grouped-backlinks) (let ((file-from (car group)) (bls (cdr group))) @@ -150,7 +149,7 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))" 'file-from file-from 'file-from-point (plist-get props :point))) (insert "\n\n")))))) - (insert "\n\n* No cite backlinks!")))) + (insert "\n\n* No ref backlinks!")))) (defun org-roam-buffer--insert-backlinks () "Insert the org-roam-buffer backlinks string for the current buffer." diff --git a/org-roam.el b/org-roam.el index bec3435..8e56987 100644 --- a/org-roam.el +++ b/org-roam.el @@ -556,6 +556,7 @@ This is the format that emacsql expects when inserting into the database. FILE-FROM is typically the buffer file path, but this may not exist, for example in temp buffers. In cases where this occurs, we do know the file path, and pass it as FILE-PATH." + (require 'org-ref nil t) (let ((file-path (or file-path (file-truename (buffer-file-name)))) links) @@ -563,22 +564,9 @@ it as FILE-PATH." (lambda (link) (let* ((type (org-element-property :type link)) (path (org-element-property :path link)) - (start (org-element-property :begin link)) - (id-data (org-roam-id-find path)) - (link-type (cond ((and (string= type "file") - (org-roam--org-file-p path)) - "file") - ((and (string= type "id") - id-data) - "id") - ((and - (require 'org-ref nil t) - (-contains? org-ref-cite-types type)) - "cite") - (t nil)))) - (when link-type - (goto-char start) - (let* ((element (org-element-at-point)) + (start (org-element-property :begin link))) + (goto-char start) + (let* ((element (org-element-at-point)) (begin (or (org-element-property :content-begin element) (org-element-property :begin element))) (content (or (org-element-property :raw-value element) @@ -594,20 +582,24 @@ it as FILE-PATH." (org-roam--get-outline-path)) :content content :point begin)) - (names (pcase link-type + (names (pcase type ("file" (list (file-truename (expand-file-name path (file-name-directory file-path))))) ("id" - (list (car id-data))) - ("cite" - (org-ref-split-and-strip-string path))))) + (list (car (org-roam-id-find path)))) + ((pred (lambda (typ) + (and (boundp 'org-ref-cite-types) + (-contains? org-ref-cite-types typ)))) + (setq type "cite") + (org-ref-split-and-strip-string path)) + (_ (list (org-element-property :raw-link link)))))) (seq-do (lambda (name) (push (vector file-path name - link-type + type properties) links)) - names))))))) + names)))))) links)) (defun org-roam--extract-headlines (&optional file-path)