Skip to content

Commit 8e3d165

Browse files
committed
[singlehtml]: append docname to section id
1 parent 8949df4 commit 8e3d165

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Contributors
7171
* Joel Wurtz -- cellspanning support in LaTeX
7272
* John Waltman -- Texinfo builder
7373
* Jon Dufresne -- modernisation
74+
* Jorge Marques -- singlehtml unique section ids
7475
* Josip Dzolonga -- coverage builder
7576
* Juan Luis Cano Rodríguez -- new tutorial (2021)
7677
* Julien Palard -- Colspan and rowspan in text builder

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ Bugs fixed
9797
Patch by Bénédikt Tran.
9898
* #13712: intersphinx: Don't add "v" prefix to non-numeric versions.
9999
Patch by Szymon Karpinski.
100+
* #13738: singlehtml builder: make section ids unique by appending the docname,
101+
matching ``sphinx/environment/adapters/toctree.py``'s ``_resolve_toctree()``
102+
format. E.g., ``id3`` becomes ``document-path/to/doc#id3``.
100103

101104
Testing
102105
-------

sphinx/writers/html5.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from sphinx.util.images import get_image_size
1818

1919
if TYPE_CHECKING:
20-
from docutils.nodes import Element, Node, Text
20+
from docutils.nodes import Element, Node, Text, section
2121

2222
from sphinx.builders import Builder
2323
from sphinx.builders.html import StandaloneHTMLBuilder
@@ -497,6 +497,18 @@ def depart_term(self, node: Element) -> None:
497497

498498
self.body.append('</dt>')
499499

500+
def visit_section(self, node: section) -> None:
501+
if (
502+
self.builder.name == 'singlehtml'
503+
and node['ids']
504+
):
505+
docname = self.docnames[-1]
506+
node['ids'][0] = 'document-' + docname + '#' + node['ids'][0]
507+
super().visit_section(node)
508+
509+
def depart_section(self, node: section) -> None:
510+
super().depart_section(node)
511+
500512
# overwritten
501513
def visit_title(self, node: nodes.title) -> None:
502514
if (

0 commit comments

Comments
 (0)