(fix): fix refs not showing in backlinks buffer (#1140)

This changes ref extraction to parse the ref using the org plain-links
syntax. This works for arbitrary `cite` links, and web links.

Link storage and ref lookup is now based on the path (e.g.
`//google.com`) or `author_year`.
This commit is contained in:
Jethro Kuan
2020-09-27 02:20:23 +08:00
committed by GitHub
parent fadb515a87
commit 273d0dffa6
3 changed files with 36 additions and 65 deletions

View File

@ -126,10 +126,10 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
(defun org-roam-buffer--insert-ref-links () (defun org-roam-buffer--insert-ref-links ()
"Insert ref backlinks for the current buffer." "Insert ref backlinks for the current buffer."
(when-let ((ref (cdr (with-temp-buffer (when-let ((path (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 path))
(grouped-backlinks (--group-by (nth 0 it) key-backlinks))) (grouped-backlinks (--group-by (nth 0 it) key-backlinks)))
(progn (progn
(insert (let ((l (length key-backlinks))) (insert (let ((l (length key-backlinks)))

View File

@ -63,19 +63,6 @@ If FILE, set `org-roam-temp-file-name' to file and insert its contents."
(setq-local org-roam-file-name ,file)) (setq-local org-roam-file-name ,file))
,@body))))) ,@body)))))
(defmacro org-roam--with-file (file &rest body)
"Execute BODY within a FILE.
Closes the file if the file is not yet visited."
(declare (indent 1) (debug t))
`(let* ((existing-buf (find-buffer-visiting ,file))
(buf (or existing-buf
(find-file-noselect file)))
res)
(with-current-buffer buf
(setq res ,@body))
(when existing-buf (kill-buffer existing-buf))
res))
(defun org-roam-message (format-string &rest args) (defun org-roam-message (format-string &rest args)
"Pass FORMAT-STRING and ARGS to `message' when `org-roam-verbose' is t." "Pass FORMAT-STRING and ARGS to `message' when `org-roam-verbose' is t."
(when org-roam-verbose (when org-roam-verbose

View File

@ -611,7 +611,7 @@ it as FILE-PATH."
(org-element-map (org-element-parse-buffer) 'link (org-element-map (org-element-parse-buffer) 'link
(lambda (link) (lambda (link)
(goto-char (org-element-property :begin link)) (goto-char (org-element-property :begin link))
(let* ((type (org-element-property :type link)) (let* ((type (org-roam--collate-types (org-element-property :type link)))
(path (org-element-property :path link)) (path (org-element-property :path link))
(element (org-element-at-point)) (element (org-element-at-point))
(begin (or (org-element-property :content-begin element) (begin (or (org-element-property :content-begin element)
@ -631,11 +631,8 @@ it as FILE-PATH."
(names (pcase type (names (pcase type
("id" ("id"
(list (car (org-roam-id-find path)))) (list (car (org-roam-id-find path))))
((pred (lambda (typ) ("cite" (list path))
(and (boundp 'org-ref-cite-types) ("website" (list path))
(-contains? org-ref-cite-types typ))))
(setq type "cite")
(org-ref-split-and-strip-string path))
("fuzzy" (list path)) ("fuzzy" (list path))
("roam" (list path)) ("roam" (list path))
(_ (if (or (file-remote-p path) (_ (if (or (file-remote-p path)
@ -780,47 +777,32 @@ Tags are obtained via:
`((booleanp (list symbolp)) `((booleanp (list symbolp))
,wrong-type)))))) ,wrong-type))))))
(defun org-roam--cite-prefix (ref) (defun org-roam--collate-types (type)
"Return the citation prefix of REF, or nil otherwise. "Collate TYPE into a parent type.
The prefixes are defined in `org-ref-cite-types`. Packages like `org-ref' introduce many different link prefixes,
Examples: but we collate them under the same parent type to clean up
(org-roam--cite-prefix \"cite:foo\") -> \"cite:\" backlinks."
(org-roam--cite-prefix \"https://google.com\") -> nil" (cond ((and (boundp 'org-ref-cite-types)
(when (require 'org-ref nil t) (member type org-ref-cite-types))
(seq-find "cite")
(lambda (prefix) (s-prefix? prefix ref)) ((member type '("http" "https"))
(-map (lambda (type) (concat type ":")) "website")
org-ref-cite-types)))) (t type)))
(defun org-roam--ref-type (ref)
"Determine the type of the REF from the prefix."
(let* ((cite-prefix (org-roam--cite-prefix ref))
(is-website (seq-some
(lambda (prefix) (s-prefix? prefix ref))
'("http" "https")))
(type (cond (cite-prefix "cite")
(is-website "website")
(t "file"))))
type))
(defun org-roam--extract-ref () (defun org-roam--extract-ref ()
"Extract the ref from current buffer and return the type and the key of the ref." "Extract the ref from current buffer and return the type and the key of the ref."
(pcase (cdr (assoc "ROAM_KEY" (let (type path)
(org-roam--extract-global-props '("ROAM_KEY")))) (pcase (cdr (assoc "ROAM_KEY"
('nil nil) (org-roam--extract-global-props '("ROAM_KEY"))))
((pred string-empty-p) ('nil nil)
(user-error "Org property #+roam_key cannot be empty")) ((pred string-empty-p)
(ref (user-error "Org property #+roam_key cannot be empty"))
(let* ((type (org-roam--ref-type ref)) (ref
(key (cond ((string= "cite" type) (when (string-match org-link-plain-re ref)
(s-chop-prefix (org-roam--cite-prefix ref) ref)) (setq type (org-roam--collate-types (match-string 1 ref))
(t ref)))) path (match-string 2 ref)))))
(cons type key))))) (when (and type path)
(cons type path))))
(defun org-roam--ref-type-p (type)
"Return t if the ref from current buffer is TYPE."
(let ((current (car (org-roam--extract-ref))))
(eq current type)))
;;;; Title/Path/Slug conversion ;;;; Title/Path/Slug conversion
(defun org-roam--path-to-slug (path) (defun org-roam--path-to-slug (path)
@ -1111,10 +1093,12 @@ When KEEP-BUFFER-P is non-nil, keep the buffers navigated by Org-roam open."
(let ((file (or (org-roam-id-get-file id) (let ((file (or (org-roam-id-get-file id)
(unless strict (org-id-find-id-file id))))) (unless strict (org-id-find-id-file id)))))
(when file (when file
(if keep-buffer-p (let ((existing-buf (find-buffer-visiting file))
(org-id-find-id-in-file id file markerp) (res (org-id-find-id-in-file id file markerp)))
(org-roam--with-file file (when (and (not keep-buffer-p)
(org-id-find-id-in-file id file markerp)))))) existing-buf)
(kill-buffer existing-buf))
res))))
(defun org-roam-id-open (id-or-marker &optional strict) (defun org-roam-id-open (id-or-marker &optional strict)
"Go to the entry with ID-OR-MARKER. "Go to the entry with ID-OR-MARKER.