Skip to content

Commit 55aeba0

Browse files
committed
allow users to provide a custom label for duplicate names
1 parent 99e5ccb commit 55aeba0

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

doc/usage/extensions/autosectionlabel.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ default. The ``autosectionlabel_prefix_document`` configuration variable can be
3030
used to make headings which appear multiple times but in different documents
3131
unique.
3232

33+
If a heading appears multiple times in a document, custom labels can be used to
34+
refer duplicate sections.
35+
36+
For example::
37+
38+
Section Title
39+
-------------
40+
41+
This is the text of the first section.
42+
43+
It refers to the section title, see :ref:`Section Title`.
44+
45+
.. _duplicate_section_title:
46+
47+
Section Title
48+
-------------
49+
50+
This is the text of the second section.
51+
52+
It refers to the corresponding section title, see :ref:`duplicate_section_title`.
53+
3354

3455
Configuration
3556
-------------

sphinx/ext/autosectionlabel.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def register_sections_as_label(app: Sphinx, document: Node) -> None:
4242
docname = app.env.current_document.docname
4343
title = cast('nodes.title', node[0])
4444
ref_name = getattr(title, 'rawsource', title.astext())
45+
if len(node['ids']) > 1 and node['ids'][-1].startswith('id'):
46+
continue
4547
if app.config.autosectionlabel_prefix_document:
4648
name = nodes.fully_normalize_name(docname + ':' + ref_name)
4749
else:

tests/roots/test-ext-autosectionlabel-prefix-document/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Introduce of Sphinx
99
Installation
1010
============
1111

12+
.. _custom-label:
13+
14+
Installation
15+
============
16+
1217
For Windows users
1318
-----------------
1419

@@ -30,6 +35,7 @@ References
3035

3136
* :ref:`index:Introduce of Sphinx`
3237
* :ref:`index:Installation`
38+
* :ref:`custom-label`
3339
* :ref:`index:For Windows users`
3440
* :ref:`index:For UNIX users`
3541
* :ref:`index:Linux`

tests/roots/test-ext-autosectionlabel/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Introduce of Sphinx
99
Installation
1010
============
1111

12+
.. _custom-label:
13+
14+
Installation
15+
============
16+
1217
For Windows users
1318
-----------------
1419

@@ -30,6 +35,7 @@ References
3035
* :ref:`test-ext-autosectionlabel`
3136
* :ref:`Introduce of Sphinx`
3237
* :ref:`Installation`
38+
* :ref:`custom-label`
3339
* :ref:`For Windows users`
3440
* :ref:`For UNIX users`
3541
* :ref:`Linux`

tests/test_extensions/test_ext_autosectionlabel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def test_autosectionlabel_html(app: SphinxTestApp) -> None:
2828
)
2929
assert re.search(html, content, re.DOTALL)
3030

31+
html = (
32+
'<li><p><a class="reference internal" href="#custom-label">'
33+
'<span class="std std-ref">Installation</span></a></p></li>'
34+
)
35+
assert re.search(html, content, re.DOTALL)
36+
3137
html = (
3238
'<li><p><a class="reference internal" href="#for-windows-users">'
3339
'<span class="std std-ref">For Windows users</span></a></p></li>'

0 commit comments

Comments
 (0)