Skip to content

[singlehtml] add docname to section anchor to make them unique #13739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Contributors
* Joel Wurtz -- cellspanning support in LaTeX
* John Waltman -- Texinfo builder
* Jon Dufresne -- modernisation
* Jorge Marques -- singlehtml unique section ids
* Josip Dzolonga -- coverage builder
* Juan Luis Cano Rodríguez -- new tutorial (2021)
* Julien Palard -- Colspan and rowspan in text builder
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ Bugs fixed
Patch by Bénédikt Tran.
* #13712: intersphinx: Don't add "v" prefix to non-numeric versions.
Patch by Szymon Karpinski.
* #13738: singlehtml builder: make section ids unique by appending the docname,
matching ``sphinx/environment/adapters/toctree.py``'s ``_resolve_toctree()``
format. E.g., ``id3`` becomes ``document-path/to/doc#id3``.

Testing
-------
2 changes: 1 addition & 1 deletion sphinx/builders/singlehtml.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def assemble_toc_secnumbers(self) -> dict[str, dict[str, tuple[int, ...]]]:
new_secnumbers: dict[str, tuple[int, ...]] = {}
for docname, secnums in self.env.toc_secnumbers.items():
for id, secnum in secnums.items():
alias = f'{docname}/{id}'
alias = f'{docname}{id}'
new_secnumbers[alias] = secnum

return {self.config.root_doc: new_secnumbers}
Expand Down
16 changes: 13 additions & 3 deletions sphinx/writers/html5.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sphinx.util.images import get_image_size

if TYPE_CHECKING:
from docutils.nodes import Element, Node, Text
from docutils.nodes import Element, Node, Text, section

from sphinx.builders import Builder
from sphinx.builders.html import StandaloneHTMLBuilder
Expand Down Expand Up @@ -395,10 +395,11 @@ def get_secnumber(self, node: Element) -> tuple[int, ...] | None:
if isinstance(node.parent, nodes.section):
if self.builder.name == 'singlehtml':
docname = self.docnames[-1]
anchorname = f'{docname}/#{node.parent["ids"][0]}'
# Remove document-
anchorname = node.parent['ids'][0][9:]
if anchorname not in self.builder.secnumbers:
# try first heading which has no anchor
anchorname = f'{docname}/'
anchorname = docname
else:
anchorname = '#' + node.parent['ids'][0]
if anchorname not in self.builder.secnumbers:
Expand Down Expand Up @@ -497,6 +498,15 @@ def depart_term(self, node: Element) -> None:

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

def visit_section(self, node: section) -> None:
if self.builder.name == 'singlehtml' and node['ids']:
docname = self.docnames[-1]
node['ids'][0] = 'document-' + docname + '#' + node['ids'][0]
super().visit_section(node)

def depart_section(self, node: section) -> None:
super().depart_section(node)

# overwritten
def visit_title(self, node: nodes.title) -> None:
if (
Expand Down
Loading