From 99f89be39c973630d00e10b481cd52572054f155 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Thu, 24 Jul 2025 11:45:44 +0200 Subject: [PATCH] Resolve Org bookmark links on export Replace bookmark links on export with their actual targets, as the bookmarks itself are of little use in the final document. --- init.el | 4 +++- site-lisp/db-org.el | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/init.el b/init.el index 27ae2b7..5dfbaa0 100644 --- a/init.el +++ b/init.el @@ -718,6 +718,7 @@ split horizontally again, but this extra work should not matter much." org-password-manager-get-password-by-id db/org-bookmark-open db/org-bookmark-store-link + db/org-bookmark-export db/org-lint-invalid-bookmark-link db/org-lint-possible-bookmark-link)) @@ -1016,7 +1017,8 @@ split horizontally again, but this extra work should not matter much." number)))) (org-link-set-parameters "bookmark" :follow #'db/org-bookmark-open - :store #'db/org-bookmark-store-link) + :store #'db/org-bookmark-store-link + :export #'db/org-bookmark-export) (when (eq system-type 'windows-nt) (org-link-set-parameters "onenote" :follow #'db/org-onenote-open) (org-link-set-parameters "outlook" :follow #'db/org-outlook-open)) diff --git a/site-lisp/db-org.el b/site-lisp/db-org.el index 079d5b3..8144273 100644 --- a/site-lisp/db-org.el +++ b/site-lisp/db-org.el @@ -2262,6 +2262,23 @@ PARAMS may contain the following values: (org-link-store-props :link (concat "bookmark:" bookmark) :description bookmark)))) +(defun db/org-bookmark-export (path description backend) + "Export Org bookmark links by resolving them to their target path. + +PATH denotes the bookmark name, while DESCRIPTION is the (optional) +description of that link. BACKEND denotes the target format for export." + (condition-case err + (let* ((bmk-target (or (bookmark-prop-get path 'location) + (bookmark-prop-get path 'filename) + (user-error "Cannot resolve bookmark for export: %s" + path)))) + ;; TODO: the following might be quite heavy, as it invokes the Org parser again. Maybe + ;; exporting links can be done more easily? + (org-export-string-as (org-link-make-string bmk-target description) + backend)) + (error (error "Error of type “%s” while exporting bookmark “%s”: “%s”" + (car err) path (cadr err))))) + (defun db/org-lint-invalid-bookmark-link (ast) "Org lint checker to verify bookmark links in AST point to known bookmarks." (bookmark-maybe-load-default-file)