Remember to cover the basics, that is, what you expected to happen and
what in fact did happen. You don't know how to make a good report? See

https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.


Emacs : GNU Emacs 29.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0)
of 2024-05-10
Package: Org mode version 9.6.15 (release_9.6.15 @ snap/emacs/current/usr/share/emacs/29.3/lisp/org)

Hi everyone,

1. Problem

I want to export an org file to HTML and include the org-info.js for the possibility of folding headings in the outputted HTML file. However, using numbered sections breaks this behavior. This problem has been addressed before but without a solution and without referring to numbered sections (https://lists.gnu.org/archive/html/emacs-orgmode/2020-04/msg00373.html).

Here is the minimal content of an org-file producing the problem using emacs -Q.

#+TITLE: Without Title there is Another Problem
#+OPTIONS: num:t
#+INFOJS_OPT: view:overview

* First Headline

This wants to be folded in HTML but it doesn't work

* Second Headline

Same here.

In the produced HTML file it is possible to fold the table of contents (either by clicking on it or by pressing f). However, it is not possible to fold the section headings. This bug happens with the original org-info.js (https://orgmode.org/org-info.js) as well as with the org-info-src.js script (https://orgmode.org/org-info-src.js).

After some investigation, I found that this problem can be solved by using #+OPTIONS: num:nil. Then, the folding works as expected. I pinned down the reason for this to be the id attribute of the div element that encloses the section content. When using unnumbered sections, the id attribute of the content (e.g., id="text-org123") corresponds to the headline's id attribute (e.g., id="org123"). When using numbered sections, the id attribute of the content is different (e.g., id="text-1") and the folding behavior breaks.

2. Proposed Solution

In order to use org-info.js when exporting with numbered sections, I found a working solution with the following patch applied to ox-html.el:

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 3b3aad748..a4a00b4ab 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -3643,23 +3643,24 @@ holding contextual information."
 (defun org-html-section (section contents info)
   "Transcode a SECTION element from Org to HTML.
 CONTENTS holds the contents of the section.  INFO is a plist
 holding contextual information."
   (let ((parent (org-element-lineage section 'headline)))
     ;; Before first headline: no container, just return CONTENTS.
     (if (not parent) contents
       ;; Get div's class and id references.
       (let* ((class-num (+ (org-export-get-relative-level parent info)
                           (1- (plist-get info :html-toplevel-hlevel))))
             (section-number
-             (and (org-export-numbered-headline-p parent info)
+             (and (not (plist-get info :html-use-infojs))
+                   (org-export-numbered-headline-p parent info)
                   (mapconcat
                    #'number-to-string
                    (org-export-get-headline-number parent info) "-"))))
         ;; Build return value.
        (format "<div class=\"outline-text-%d\" id=\"text-%s\">\n%s</div>\n"
                class-num
                (or (org-element-property :CUSTOM_ID parent)
                    section-number
                    (org-export-get-reference parent info))
                (or contents ""))))))

This function creates the div element for the section's content. When sections are numbered, a different id attribute is used since the variable section-number is not nil. This patch checks whether org-info.js is used by checking :html-use-infojs. If it is used, the section variable will be nil and the id attribute is set as if the sections are unnumbered. Consequently, the org-info.js folding behavior works as expected.

This is a proposed solution since I do not know whether this conflicts with some other settings. After all, why is the id attribute different when numbered sections are used?

All the best,

Felix Esser