(feat): Add header level to backlinks buffer (#863)

adds the outline hierarchy to the backlinks buffer
This commit is contained in:
Jethro Kuan 2020-06-25 12:40:22 +08:00 committed by GitHub
parent c59d6c4f7c
commit 79c75ac174
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 6 deletions

View file

@ -11,6 +11,7 @@
- [#847](https://github.com/org-roam/org-roam/pull/847) Add GC threshold `org-roam-db-gc-threshold` to temporarily change the threshold on expensive operations.
- [#847](https://github.com/org-roam/org-roam/pull/847) Use sqlite3 transactions instead of storing the values to be inserted.
- [#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
### Bugfixes

View file

@ -169,13 +169,18 @@ For example: (setq org-roam-buffer-window-parameters '((no-other-window . t)))"
(org-roam--get-title-or-slug file-from)))
(dolist (backlink bls)
(pcase-let ((`(,file-from _ ,props) backlink))
(insert (propertize
(insert "*** "
(if-let ((outline (plist-get props :outline)))
(string-join outline " > ")
"Top")
"\n"
(propertize
(s-trim (s-replace "\n" " "
(plist-get props :content)))
'help-echo "mouse-1: visit backlinked note"
'file-from file-from
'file-from-point (plist-get props :point)))
(insert "\n\n"))))))
'file-from-point (plist-get props :point))
"\n\n"))))))
(insert "\n\n* No backlinks!")))
(defun org-roam-buffer-update ()

View file

@ -510,6 +510,42 @@ PATH should be the root from which to compute the relativity."
(concat dir link)))))
(buffer-string))))
(defun org-roam--get-outline-path ()
"Return the outline path to the current entry.
An outline path is a list of ancestors for current headline, as a
list of strings. Statistics cookies are removed and links are
kept.
When optional argument WITH-SELF is non-nil, the path also
includes the current headline."
(org-with-wide-buffer
(and (or (condition-case nil
(org-back-to-heading t)
(error nil))
(org-up-heading-safe))
(reverse (org-roam--get-outline-path-1)))))
(defun org-roam--get-outline-path-1 ()
"Return outline path to current headline.
Outline path is a list of strings, in reverse order. See
`org-roam--get-outline-path' for details.
Assume buffer is widened and point is on a headline."
(when org-complex-heading-regexp
(let ((heading (let ((case-fold-search nil))
(looking-at org-complex-heading-regexp)
(if (not (match-end 4)) ""
;; Remove statistics cookies.
(org-trim
(replace-regexp-in-string
"\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
(match-string-no-properties 4)))))))
(if (org-up-heading-safe)
(cons heading (org-roam--get-outline-path-1))
(list heading)))))
(defun org-roam--extract-links (&optional file-path)
"Extracts all link items within the current buffer.
Link items are of the form:
@ -553,7 +589,11 @@ it as FILE-PATH."
(content (string-trim content))
;; Expand all relative links to absolute links
(content (org-roam--expand-links content file-path)))
(let ((context (list :content content :point begin))
(let ((properties (list :outline (mapcar (lambda (path)
(org-roam--expand-links path file-path))
(org-roam--get-outline-path))
:content content
:point begin))
(names (pcase link-type
("file"
(list (file-truename (expand-file-name path (file-name-directory file-path)))))
@ -565,7 +605,7 @@ it as FILE-PATH."
(push (vector file-path
name
link-type
context)
properties)
links))
names)))))))
links))

View file

@ -45,7 +45,7 @@
(pcase (benchmark-run 1 (org-roam-db-build-cache t))
(`(,time ,gcs ,time-in-gc)
(message "Elapsed time: %fs (%fs in %d GCs)" time time-in-gc gcs)
(expect time :to-be-less-than 60))))
(expect time :to-be-less-than 70))))
(it "builds quickly without change"
(pcase (benchmark-run 1 (org-roam-db-build-cache))
(`(,time ,gcs ,time-in-gc)