(fix): preserve existing description in a roam: link on replace (#1327)

Co-authored-by: Jethro Kuan <jethrokuan95@gmail.com>
This commit is contained in:
Kisaragi Hiu
2020-12-15 20:47:51 +09:00
committed by GitHub
parent f2976fa3be
commit d87dd011aa
2 changed files with 69 additions and 52 deletions

View File

@ -10,6 +10,7 @@
- [#1281](https://github.com/org-roam/org-roam/pull/1281) fixed idle-timer not instantiated on `org-roam-mode`
- [#1308](https://github.com/org-roam/org-roam/pull/1308) fixed file renames corrupting database
- [#1325](https://github.com/org-roam/org-roam/pull/1325) make titles and tags extracted unique per note
- [#1327](https://github.com/org-roam/org-roam/pull/1327) preserve existing link description during automatic replacement
## 1.2.3 (13-11-2020)
@ -70,7 +71,7 @@ This change requires you to set `org-roam-directory` to the resolved path of a f
- [#974](https://github.com/org-roam/org-roam/pull/974) Protect region targeted by `org-roam-insert`
- [#994](https://github.com/org-roam/org-roam/pull/994) Simplify org-roam-store-link
- [#1062](https://github.com/org-roam/org-roam/pull/1062) Variable `org-roam-completions-everywhere` allows for completions everywhere from word at point
- [#910](https://github.com/org-roam/org-roam/pull/910), [#1105](https://github.com/org-roam/org-roam/pull/1105) Support fuzzy links of the form [[roam:Title]], [[roam:*Headline]] and [[roam:Title*Headline]]
- [#910](https://github.com/org-roam/org-roam/pull/910), [#1105](https://github.com/org-roam/org-roam/pull/1105) Support fuzzy links of the form `[[roam:Title]]`, `[[roam:*Headline]]` and `[[roam:Title*Headline]]`
### Bugfixes

View File

@ -71,9 +71,12 @@ noabbrev Absolute path, no abbreviation of home directory."
(org-link-set-parameters "roam"
:follow #'org-roam-link-follow-link)
(defun org-roam-link-follow-link (path)
"Navigates to location specified by PATH."
(pcase-let ((`(,link-type ,loc ,desc ,mkr) (org-roam-link--get-location path)))
(defun org-roam-link-follow-link (_path)
"Navigates to location in Org-roam link.
This function is called by Org when following links of the type
`roam'. While the path is passed, assume that the cursor is on
the link."
(pcase-let ((`(,link-type ,loc ,desc ,mkr) (org-roam-link--get-location)))
(when (and org-roam-link-auto-replace loc desc)
(org-roam-link--replace-link link-type loc desc))
(pcase link-type
@ -184,16 +187,31 @@ star-idx is the index of the asterisk, if any."
(t 'title+headline))))
(list type title headline star-index))))
(defun org-roam-link--get-location (link)
"Return the location of Org-roam fuzzy LINK.
(defun org-roam-link--get-location ()
"Return the location of the Org-roam fuzzy link at point.
The location is returned as a list containing (link-type loc desc marker).
nil is returned if there is no matching location.
link-type is either \"file\" or \"id\".
loc is the target location: e.g. a file path, or an id.
marker is a marker to the headline, if applicable."
(let (mkr link-type desc loc)
(pcase-let ((`(,type ,title ,headline _) (org-roam-link--split-path link)))
marker is a marker to the headline, if applicable.
desc is either the the description of the link under point, or
the target of LINK (title or heading content)."
(let ((context (org-element-context))
mkr link-type desc loc)
(pcase (org-element-lineage context '(link) t)
(`nil (error "Not at an Org link"))
(link
(if (not (string-equal "roam" (org-element-property :type link)))
(error "Not at Org-roam link")
(setq desc (and (org-element-property :contents-begin link)
(org-element-property :contents-end link)
(buffer-substring-no-properties
(org-element-property :contents-begin link)
(org-element-property :contents-end link))))
(pcase-let ((`(,type ,title ,headline _) (org-roam-link--split-path
(org-element-property :path link))))
(pcase type
('title+headline
(let ((file (org-roam-link--get-file-from-title title)))
@ -202,25 +220,26 @@ marker is a marker to the headline, if applicable."
(setq mkr (org-roam-link--get-id-from-headline headline file))
(pcase mkr
(`(,marker . ,target-id)
(progn
(setq mkr marker
loc target-id
link-type "id"
desc headline))
(_ (org-roam-message "cannot find matching id"))))))
desc (or desc headline)
link-type "id")))
(_ (org-roam-message "Cannot find matching id"))))))
('title
(setq loc (org-roam-link--get-file-from-title title)
desc title
link-type "file"))
link-type "file"
desc (or desc title)))
('headline
(setq mkr (org-roam-link--get-id-from-headline headline))
(pcase mkr
(`(,marker . ,target-id)
(setq mkr marker
loc target-id
desc headline
link-type "id"))
(_ (org-roam-message "Cannot find matching headline")))))
(list link-type loc desc mkr))))
link-type "id"
desc (or desc headline)))
(_ (org-roam-message "Cannot find matching headline")))))))))
(list link-type loc desc mkr)))
;;; Conversion Functions
(defun org-roam-link--replace-link (link-type loc &optional desc)
@ -241,14 +260,11 @@ DESC is the link description."
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-link-bracket-re nil t)
(let ((context (org-element-context)))
(pcase (org-element-lineage context '(link) t)
(`nil nil)
(link
(when (string-equal "roam" (org-element-property :type link))
(pcase-let ((`(,link-type ,loc ,desc _) (org-roam-link--get-location (org-element-property :path link))))
(condition-case nil
(pcase-let ((`(,link-type ,loc ,desc _) (org-roam-link--get-location)))
(when (and link-type loc)
(org-roam-link--replace-link link-type loc desc))))))))))
(org-roam-link--replace-link link-type loc desc)))
(error nil)))))
(defun org-roam-link--replace-link-on-save ()
"Hook to replace all roam links on save."