branch: externals/org
commit 3ea1682731fa061115bc690e76ce7d1d2955f810
Author: Derek Chen-Becker <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    Generate images in TOC for HTML export
    
    * etc/ORG-NEWS: Add a news entry for the change in link rendering behavior.
    * lisp/ox.el (org-export-toc-entry-backend): Extract the lambda for link
    formatting into a new `org-export-toc-default-link-transcoder' defun for
    use in other exporters.
    * lisp/ox-html.el (org-html--format-toc-headline): Format image links for
    the TOC using `org-html-link', but all other link types maintain formatting
    using the new `org-export-toc-default-link-transcoder'.
    * testing/lisp/test-ox-html.el (org-html/test-toc-images,
    org-html/test-toc-links): Create new unit tests to exercise the conditional
    formatting of TOC links for image and non-image links.
    
    Prior to this, image links would be rendered in headers, but would be
    converted into a plain text filename in the TOC.
    
    Link: 
https://list.orgmode.org/orgmode/[email protected]/
    Reported-by: Perry Smith <[email protected]>
---
 etc/ORG-NEWS                 |  7 +++++++
 lisp/ox-html.el              |  6 +++++-
 lisp/ox.el                   | 13 ++++++++-----
 testing/lisp/test-ox-html.el | 19 +++++++++++++++++++
 4 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index a8c68de7f6..5a72b69ea4 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -221,6 +221,13 @@ cookie =[N%]=.
 
 ** Miscellaneous
 
+*** ~ox-html~ properly formats image links in the table of contents
+
+If a heading has an image link (as determined by
+`org-html-inline-image-p') then the image is rendered in the exported
+table of contents, otherwise the link is rendered as plain text (the
+previous behavior for all links).
+
 *** Some internal function names in =org-colview= library have been changed
 
 As a result of internal refactoring in =lisp/org-colview.el= some of
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 5f3666ba1e..3d4f79de57 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2589,7 +2589,11 @@ INFO is a plist used as a communication channel."
                        (org-element-property :priority headline)))
         (text (org-export-data-with-backend
                (org-export-get-alt-title headline info)
-               (org-export-toc-entry-backend 'html)
+               (org-export-toc-entry-backend 'html
+                  `(link . ,(lambda (l c i)
+                              (if (org-html-inline-image-p l i)
+                                  (org-html-link l c i)
+                                (org-export-toc-default-link-transcoder l c 
i)))))
                info))
         (tags (and (eq (plist-get info :with-tags) t)
                    (org-export-get-tags headline info))))
diff --git a/lisp/ox.el b/lisp/ox.el
index c6f4dcbd71..e0782c1e62 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -5687,6 +5687,13 @@ required on headlines excluded from table of contents."
              (> (org-export-get-relative-level headline info)
                 toc-depth)))))
 
+(defun org-export-toc-default-link-transcoder (link contents info)
+  "Format a link by converting to regular text based on LINK, CONTENTS, and 
INFO."
+  (or contents
+      (org-export-data
+       (org-element-property :raw-link link)
+       info)))
+
 (defun org-export-toc-entry-backend (parent &rest transcoders)
   "Return an export backend appropriate for table of contents entries.
 
@@ -5705,11 +5712,7 @@ transcoding it."
    :transcoders
    (append transcoders
           `((footnote-reference . ,#'ignore)
-            (link . ,(lambda (l c i)
-                       (or c
-                           (org-export-data
-                            (org-element-property :raw-link l)
-                            i))))
+            (link . ,#'org-export-toc-default-link-transcoder)
             (radio-target . ,(lambda (_r c _) c))
             (target . ,#'ignore)))))
 
diff --git a/testing/lisp/test-ox-html.el b/testing/lisp/test-ox-html.el
index 11da941aba..e60db23c8b 100644
--- a/testing/lisp/test-ox-html.el
+++ b/testing/lisp/test-ox-html.el
@@ -1112,6 +1112,25 @@ entirely."
         (expected 
"\n<ul>\n<li>\n<ul>\n<li>1\n<ul>\n<li>1.1</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>2</li>\n</ul>\n"))
     (should (string= (org-html--toc-text toc-entries nil) expected))))
 
+(ert-deftest org-html/test-toc-images ()
+  "Test the generation of image links in the TOC."
+  (org-test-with-temp-text "* [[file:test.svg]] Test\n\nA test"
+    (let ((export-buffer "*Test HTML Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'html export-buffer)
+      (with-current-buffer export-buffer
+        (should (= 1 (how-many "<li>.*<img src=\"test.svg\" .*</li>")))))))
+
+(ert-deftest org-html/test-toc-links ()
+  "Non-image links in the TOC should not result in TOC links."
+  (org-test-with-temp-text "* [[https://orgmode.org][org]] Test\n\nA test"
+    (let ((export-buffer "*Test HTML Export*")
+          (org-export-show-temporary-export-buffer nil))
+      (org-export-to-buffer 'html export-buffer)
+      (with-current-buffer export-buffer
+        (should (= 0 (how-many "<li>.*orgmode.org.*</li>")))))))
+
+
 ;;; Rendering priorities
 
 (ert-deftest ox-html/test-priority ()

Reply via email to