emacsorphanage / ox-pandoc

Another org-mode exporter via pandoc.
GNU General Public License v2.0
48 stars 13 forks source link

Custom links not handled in headlines #26

Open eatse21 opened 1 year ago

eatse21 commented 1 year ago

Hi, I currently have some custom org links in my documents successfully translated by a (org-link-set-parameters "custom" :export 'fn) function when exporting with pandoc. Recently I tried to put one in a headline, but nothing happened.

Consider this exemple org file:

#+title: Demo

* This [[id:mytarget][won't work]]
This [[id:mytarget2][will work]]  

With this configuration:

(defun handle-id-links (target-id description backend)
  (when (eq backend 'pandoc)
  (format "[[https://mydomain.com/%s][%s]]"
          target-id
          (or description target-id))))
(org-link-set-parameters "id" :export #'handle-id-links)

When exporting to html with pandoc for example, this comes out:

<body>
<h1 id="this-wont-work">This <a href="id:mytarget">won't work</a></h1>
<p>This <a href="https://mydomain.com/mytarget2">will work</a></p>
</body>

Actually, if I remove the link in the paragraph, and tell edebug to eval handle-id-links, I found that this function will never be called during export. Instead, it will be handled by org-org-headline in ox-org.el, finally calling org-element-link-interpreter which doesn’t do anything with the custom link.

I found a workaround by bypassing org-element-link-interpreter like this, but maybe this is something ox-pandoc could handle?

;;Workaround                                                                                                                                                       
(defun custom-link-interpreter (link contents)
  (let ((type (org-element-property :type link))
    (path (org-element-property :path link)))
    (when (string= type "id")
        (handle-id-links path contents 'pandoc))))
(advice-add #'org-element-link-interpreter :before-until #'custom-link-interpreter)