(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.
This commit is contained in:
Jethro Kuan
2020-07-08 12:29:06 +08:00
committed by GitHub
parent 9f7ed4353c
commit d77f897400
3 changed files with 21 additions and 29 deletions

View File

@ -13,6 +13,7 @@
- [#851](https://github.com/org-roam/org-roam/pull/851) Add `'first-directory'` option for `org-roam-tag-sources` - [#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 - [#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. - [#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 ### Bugfixes

View File

@ -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 (defcustom org-roam-buffer-prepare-hook '(org-roam-buffer--insert-title
org-roam-buffer--insert-backlinks 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." "Hook run in the `org-roam-buffer' before it is displayed."
:type 'hook :type 'hook
:group 'org-roam) :group 'org-roam)
@ -123,10 +123,9 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
,wrong-type)))))) ,wrong-type))))))
(concat string (when (> l 1) "s")))) (concat string (when (> l 1) "s"))))
(defun org-roam-buffer--insert-citelinks () (defun org-roam-buffer--insert-ref-links ()
"Insert citation backlinks for the current buffer." "Insert ref backlinks for the current buffer."
(when-let ((org-ref-p (require 'org-ref nil t)) ;; Ensure that org-ref is present (when-let ((ref (cdr (with-temp-buffer
(ref (cdr (with-temp-buffer
(insert-buffer-substring org-roam-buffer--current) (insert-buffer-substring org-roam-buffer--current)
(org-roam--extract-ref))))) (org-roam--extract-ref)))))
(if-let* ((key-backlinks (org-roam--get-backlinks 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 (progn
(insert (let ((l (length key-backlinks))) (insert (let ((l (length key-backlinks)))
(format "\n\n* %d %s\n" (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) (dolist (group grouped-backlinks)
(let ((file-from (car group)) (let ((file-from (car group))
(bls (cdr 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 file-from
'file-from-point (plist-get props :point))) 'file-from-point (plist-get props :point)))
(insert "\n\n")))))) (insert "\n\n"))))))
(insert "\n\n* No cite backlinks!")))) (insert "\n\n* No ref backlinks!"))))
(defun org-roam-buffer--insert-backlinks () (defun org-roam-buffer--insert-backlinks ()
"Insert the org-roam-buffer backlinks string for the current buffer." "Insert the org-roam-buffer backlinks string for the current buffer."

View File

@ -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 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 in temp buffers. In cases where this occurs, we do know the file path, and pass
it as FILE-PATH." it as FILE-PATH."
(require 'org-ref nil t)
(let ((file-path (or file-path (let ((file-path (or file-path
(file-truename (buffer-file-name)))) (file-truename (buffer-file-name))))
links) links)
@ -563,22 +564,9 @@ it as FILE-PATH."
(lambda (link) (lambda (link)
(let* ((type (org-element-property :type link)) (let* ((type (org-element-property :type link))
(path (org-element-property :path link)) (path (org-element-property :path link))
(start (org-element-property :begin link)) (start (org-element-property :begin link)))
(id-data (org-roam-id-find path)) (goto-char start)
(link-type (cond ((and (string= type "file") (let* ((element (org-element-at-point))
(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))
(begin (or (org-element-property :content-begin element) (begin (or (org-element-property :content-begin element)
(org-element-property :begin element))) (org-element-property :begin element)))
(content (or (org-element-property :raw-value element) (content (or (org-element-property :raw-value element)
@ -594,20 +582,24 @@ it as FILE-PATH."
(org-roam--get-outline-path)) (org-roam--get-outline-path))
:content content :content content
:point begin)) :point begin))
(names (pcase link-type (names (pcase type
("file" ("file"
(list (file-truename (expand-file-name path (file-name-directory file-path))))) (list (file-truename (expand-file-name path (file-name-directory file-path)))))
("id" ("id"
(list (car id-data))) (list (car (org-roam-id-find path))))
("cite" ((pred (lambda (typ)
(org-ref-split-and-strip-string path))))) (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) (seq-do (lambda (name)
(push (vector file-path (push (vector file-path
name name
link-type type
properties) properties)
links)) links))
names))))))) names))))))
links)) links))
(defun org-roam--extract-headlines (&optional file-path) (defun org-roam--extract-headlines (&optional file-path)