Hello, Org-mode maintainers,

I found a bug in ox-html's footnote reference handling that generates
duplicate HTML IDs when a footnote is referenced multiple times.  When a
footnote is referenced more than once, ox-html should generate unique
IDs for each reference:

- First reference: id="fnr.LABEL"
- Second reference: id="fnr.LABEL.1"
- Third reference: id="fnr.LABEL.2"
- etc.

However, the current implementation generates duplicate IDs for the
second and subsequent references. Try these code:

--------------------------------->8<-----------------------------------
#+title: test
#+options: html-style:nil html-postamble:nil html-preamble:nil

Hello world[fn:1][fn:1][fn:1]


[fn:1] test1
--------------------------------->8<-----------------------------------

You'll get something like this:

<p>
Hello world<sup><a id="fnr.1" class="footref" href="#fn.1"
role="doc-backlink">1</a></sup><sup>, </sup><sup><a id="fnr.1.1"
class="footref" href="#fn.1" role="doc-backlink">1</a></sup><sup>,
</sup><sup><a id="fnr.1.1" class="footref" href="#fn.1"
role="doc-backlink">1</a></sup>
</p>

All subsequent references get the same ".1" suffix, violating the HTML
spec (IDs must be unique).

The bug is in `org-html-footnote-reference'. The code uses
`org-export-get-ordinal' to compute the numeric suffix, but
`org-export-get-ordinal' returns the *footnote number* (which footnote
it is), not the *reference count* (how many times it has been
referenced).

Instead of using `org-export-get-ordinal', count the previous references
directly by traversing the parse tree. The attached patch implements
this fix.

While this patch prevents the duplicate ID issue, jumping back from a
footnote definition to the exact originating reference is still not
supported when a footnote is referenced multiple times. Achieving that
would likely require JavaScript, so I have left it out of scope for
ox-html.

Most users probably won't reference the same footnote many times, and
the backlink defaults to the first reference anyway, so this isn't a
critical bug.  just duplicate IDs that violate the HTML spec.

Please check the patch file.

Regards.

Attachment: 0001-ox-html-Fix-duplicate-IDs-in-multiple-footnote-refer.patch
Description: Binary data

Reply via email to