(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` - [#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 - [#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 - [#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) ## 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` - [#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 - [#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 - [#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 ### Bugfixes

View File

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