From b0eee438c19f89e5dc76bb7afdd2fd22b4ef7b22 Mon Sep 17 00:00:00 2001 From: Protesilaos Stavrou Date: Wed, 14 Feb 2024 09:46:48 +0200 Subject: [PATCH] Make Org export correctly add the search to the anchor This is relevant when the 'denote:' link has an optional search extension to jump to a location (same as with the standard Org 'file:' link type). Denote uses this facility with the command 'denote-org-extras-link-to-heading', which is part of the new optional extension 'denote-org-extras.el'. Thanks to @fingerknight for noting that the search extension for not part of the exported link anchor. This was done in issue 250: . --- README.org | 4 ++-- denote.el | 32 +++++++++++++++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/README.org b/README.org index cafc0a4..8ae3353 100644 --- a/README.org +++ b/README.org @@ -5054,8 +5054,8 @@ Denote is meant to be a collective effort. Every bit of help matters. Niall Dooley, Paul van Gelder, Peter Prevos, Peter Smith, Suhail Singh, Shreyas Ragavan, Stefan Thesing, Summer Emacs, Sven Seebeck, Taoufik, TJ Stankus, Viktor Haag, Wade Mealing, Yi Liu, Ypot, - atanasj, babusri, doolio, drcxd, hpgisler, pRot0ta1p, rbenit68, - relict007, sienic, sundar bp. + atanasj, babusri, doolio, drcxd, fingerknight, hpgisler, pRot0ta1p, + rbenit68, relict007, sienic, sundar bp. Special thanks to Peter Povinec who helped refine the file-naming scheme, which is the cornerstone of this project. diff --git a/denote.el b/denote.el index 4e57a25..e148bc5 100644 --- a/denote.el +++ b/denote.el @@ -4047,10 +4047,9 @@ This command is meant to be used from a Dired buffer." (declare-function org-link-open-as-file "ol" (path arg)) -(defun denote-link--ol-resolve-link-to-target (link &optional path-id) - "Resolve LINK into the appropriate target. -With optional PATH-ID return a cons cell consisting of the path -and the identifier." +(defun denote-link--ol-resolve-link-to-target (link &optional full-data) + "Resolve LINK to target file, with or without additioanl search terms. +With optional FULL-DATA return a list in the form of (path id search)." (let* ((search (and (string-match "::\\(.*\\)\\'" link) (match-string 1 link))) (id (if (and search (not (string-empty-p search))) @@ -4058,8 +4057,8 @@ and the identifier." link)) (path (denote-get-path-by-id id))) (cond - (path-id - (cons (format "%s" path) (format "%s" id))) + (full-data + (list path id search)) ((and search (not (string-empty-p search))) (concat path "::" search)) (path)))) @@ -4141,17 +4140,24 @@ Also see the user option `denote-org-store-link-to-heading'." "Export a `denote:' link from Org files. The LINK, DESCRIPTION, and FORMAT are handled by the export backend." - (let* ((path-id (denote-link--ol-resolve-link-to-target link :path-id)) - (path (file-relative-name (car path-id))) - (p (file-name-sans-extension path)) - (id (cdr path-id)) - (desc (or description (concat "denote:" id)))) + (let* ((path-id (denote-link--ol-resolve-link-to-target link :full-data)) + (path (file-relative-name (nth 0 path-id))) + (id (nth 1 path-id)) + (search (nth 2 path-id)) + (anchor (file-name-sans-extension path)) + (desc (cond + (description) + (search (format "denote:%s::%s" id search)) + (t (concat "denote:" id))))) (cond - ((eq format 'html) (format "%s" p desc)) + ((eq format 'html) + (if search + (format "%s" anchor search desc) + (format "%s" anchor desc))) ((eq format 'latex) (format "\\href{%s}{%s}" (replace-regexp-in-string "[\\{}$%&_#~^]" "\\\\\\&" path) desc)) ((eq format 'texinfo) (format "@uref{%s,%s}" path desc)) ((eq format 'ascii) (format "[%s] " desc path)) ; NOTE 2022-06-16: May be tweaked further - ((eq format 'md) (format "[%s](%s.md)" desc p)) + ((eq format 'md) (format "[%s](%s.md)" desc anchor)) (t path)))) ;; The `eval-after-load' part with the quoted lambda is adapted from