emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature request: Allow export to convert broken links to plain text


From: Ryan C. Thompson
Subject: Re: Feature request: Allow export to convert broken links to plain text
Date: Sun, 10 Sep 2023 00:13:47 -0400
User-agent: Mozilla Thunderbird


On 1/13/19 5:34 PM, Berry, Charles wrote:
Looks like your original idea to revise `org-export-data' might be best. 

IIUC, you need to add the link text to the SIGNAL-DATA in each of the places where `org-export-resolve-*-link' functions call `signal', then modify `org-export-data' to ignore the addition for `mark' and add it back for your new `mark-with-text' option.

HTH,

Chuck

Years later, I became annoyed enough by this to attempt to fix it again. Unfortunately, I looked into changing all the functions that signal org-link-broken, and not all of them can be modified in the way you described, at least not easily. Instead, I came up with a fairly clean alternative solution: define a new link type "maybe" using org-link-set-parameters with an :export function that pulls out the real link transcoder from the backend and calls it, but then implements my desired behavior if that transcoder throws an error. This allows you to prefix any link's path with "maybe:" to have it magically become plain text if it can't be resolved during export. Here's the implementation:


(org-link-set-parameters
 "maybe"
 :follow
 (lambda (path prefix)
   (condition-case err
       (org-link-open-from-string (format "[[%s]]" path))
     (error (message "Failed to open maybe link %S" path))))
 ;; This must be a lambda so it is self-contained
 :export
 (lambda (path desc backend &optional info)
   (when (symbolp backend)
     (setq backend (org-export-get-backend backend)))
   ;; Generate the non-maybe version of the link, and call the
   ;; backend's appropriate transcoder on it, but catch any error
   ;; thrown and just replace the link with its text instead.
   (let* ((real-link
           (with-temp-buffer
         (save-excursion (insert "[[" path "][" desc "]]"))
         (org-element-link-parser)))
          (real-link-transcoder (cdr (assoc 'link (org-export-get-all-transcoders backend)))))
     (condition-case err
         (funcall real-link-transcoder real-link desc info)
       (error
        (message "Skipping error during maybe link transcoding: %S" err)
        (or desc path))))))


Using the above code, the following org file can be successfully exported to HTML, with the broken/invalid links converted to just plain text.


* First heading
:PROPERTIES:
:CUSTOM_ID: heading1
:END:
- [[maybe:maybe:maybe:https://google.com][Maybe google]]
- [[maybe:#heading1][Link to first heading]]
- [[maybe:#heading2][Link to second heading]]
- [[maybe:#heading3][Link to third(?) heading]]
- [[maybe:blarg:notalink][This is an invalid link]]
* Second heading
:PROPERTIES:
:CUSTOM_ID: heading2
:END:


So, this isn't an ideal solution, since it requires me to prefix any potential offending links with "maybe:". But it's good enough for me.


Regards,

Ryan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]