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.
This commit is contained in:
Daniel Borchmann 2025-07-24 11:45:44 +02:00
parent cd90381ad0
commit 99f89be39c
No known key found for this signature in database
GPG Key ID: 50EA937BF472ADD1
2 changed files with 20 additions and 1 deletions

View File

@ -718,6 +718,7 @@ split horizontally again, but this extra work should not matter much."
org-password-manager-get-password-by-id org-password-manager-get-password-by-id
db/org-bookmark-open db/org-bookmark-open
db/org-bookmark-store-link db/org-bookmark-store-link
db/org-bookmark-export
db/org-lint-invalid-bookmark-link db/org-lint-invalid-bookmark-link
db/org-lint-possible-bookmark-link)) db/org-lint-possible-bookmark-link))
@ -1016,7 +1017,8 @@ split horizontally again, but this extra work should not matter much."
number)))) number))))
(org-link-set-parameters "bookmark" (org-link-set-parameters "bookmark"
:follow #'db/org-bookmark-open :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) (when (eq system-type 'windows-nt)
(org-link-set-parameters "onenote" :follow #'db/org-onenote-open) (org-link-set-parameters "onenote" :follow #'db/org-onenote-open)
(org-link-set-parameters "outlook" :follow #'db/org-outlook-open)) (org-link-set-parameters "outlook" :follow #'db/org-outlook-open))

View File

@ -2262,6 +2262,23 @@ PARAMS may contain the following values:
(org-link-store-props :link (concat "bookmark:" bookmark) (org-link-store-props :link (concat "bookmark:" bookmark)
:description 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) (defun db/org-lint-invalid-bookmark-link (ast)
"Org lint checker to verify bookmark links in AST point to known bookmarks." "Org lint checker to verify bookmark links in AST point to known bookmarks."
(bookmark-maybe-load-default-file) (bookmark-maybe-load-default-file)