Skip to content

Commit

Permalink
(feat): support more ref links
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jethrokuan committed Jul 8, 2020
1 parent 9f7ed43 commit 907ffe8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions org-roam-buffer.el
Expand Up @@ -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)
Expand Down Expand Up @@ -123,18 +123,17 @@ 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))
(grouped-backlinks (--group-by (nth 0 it) key-backlinks)))
(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)))
Expand All @@ -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."
Expand Down
34 changes: 12 additions & 22 deletions org-roam.el
Expand Up @@ -559,26 +559,14 @@ it as FILE-PATH."
(let ((file-path (or file-path
(file-truename (buffer-file-name))))
links)
(require 'org-ref nil t) ; For parsing org-ref links
(org-element-map (org-element-parse-buffer) 'link
(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)
Expand All @@ -594,20 +582,22 @@ 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 (-contains? org-ref-cite-types))
(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)
Expand Down

0 comments on commit 907ffe8

Please sign in to comment.