Skip to content

Commit 4bde78e

Browse files
committed
[singlehtml]: append docname to section id
1 parent fe728f4 commit 4bde78e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

AUTHORS.rst

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

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ Bugs fixed
116116
* #10785: Autodoc: Allow type aliases defined in the project to be properly
117117
cross-referenced when used as type annotations. This makes it possible
118118
for objects documented as ``:py:data:`` to be hyperlinked in function signatures.
119+
* #13738: singlehtml builder: make section ids unique by appending the docname,
120+
matching ``sphinx/environment/adapters/toctree.py``'s ``_resolve_toctree()``
121+
format. E.g., ``id3`` becomes ``document-path/to/doc#id3``.
119122

120123
Testing
121124
-------

sphinx/writers/html5.py

Lines changed: 10 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,15 @@ def depart_term(self, node: Element) -> None:
497497

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

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

0 commit comments

Comments
 (0)