emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Ryan C. Thompson" <rct@thompsonclan.org>
To: "Berry, Charles" <ccberry@ucsd.edu>
Cc: Org-Mode List <emacs-orgmode@gnu.org>
Subject: Re: Feature request: Allow export to convert broken links to plain text
Date: Sun, 10 Sep 2023 00:13:47 -0400	[thread overview]
Message-ID: <ce86d05c-ca3c-41c0-a6f8-aa57f7755c22@thompsonclan.org> (raw)
In-Reply-To: <6C73C452-CA9E-45A0-8529-61BDCDA0B580@ucsd.edu>

[-- Attachment #1: Type: text/plain, Size: 2919 bytes --]


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

[-- Attachment #2: Type: text/html, Size: 4178 bytes --]

  reply	other threads:[~2023-09-10  4:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-12 20:38 Feature request: Allow export to convert broken links to plain text Ryan Thompson
2019-01-12 20:42 ` Ryan Thompson
2019-01-13 17:50 ` Berry, Charles
2019-01-13 21:33   ` Ryan Thompson
2019-01-13 22:34     ` Berry, Charles
2023-09-10  4:13       ` Ryan C. Thompson [this message]
2023-09-11 17:12         ` Berry, Charles
  -- strict thread matches above, loose matches on Subject: below --
2023-04-13 22:55 Janek Fischer
2023-04-15  6:50 ` Hanno Perrey
2023-04-17 13:56 ` Ihor Radchenko
2019-01-05 20:57 Ryan Thompson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ce86d05c-ca3c-41c0-a6f8-aa57f7755c22@thompsonclan.org \
    --to=rct@thompsonclan.org \
    --cc=ccberry@ucsd.edu \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).