Skip to content

Commit f4a94f7

Browse files
authored
fix: Properly fix duplicated headings for nested docstrings
PR #613: #613
1 parent e2123a9 commit f4a94f7

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

src/mkdocstrings/extension.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,24 +233,21 @@ class _PostProcessor(Treeprocessor):
233233
def run(self, root: Element) -> None:
234234
self._remove_duplicated_headings(root)
235235

236-
def _remove_duplicated_headings(self, parent: Element) -> bool:
236+
def _remove_duplicated_headings(self, parent: Element) -> None:
237237
carry_text = ""
238-
found = False
239238
for el in reversed(parent): # Reversed mainly for the ability to mutate during iteration.
240239
if el.tag == "div" and el.get("class") == "mkdocstrings":
241240
# Delete the duplicated headings along with their container, but keep the text (i.e. the actual HTML).
242241
carry_text = (el.text or "") + carry_text
243242
parent.remove(el)
244-
found = True
245-
elif carry_text:
246-
el.tail = (el.tail or "") + carry_text
247-
carry_text = ""
248-
elif self._remove_duplicated_headings(el):
249-
found = True
250-
break
243+
else:
244+
if carry_text:
245+
el.tail = (el.tail or "") + carry_text
246+
carry_text = ""
247+
self._remove_duplicated_headings(el)
248+
251249
if carry_text:
252250
parent.text = (parent.text or "") + carry_text
253-
return found
254251

255252

256253
class MkdocstringsExtension(Extension):

tests/fixtures/headings_many.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def heading_1():
2+
"""## Heading one"""
3+
4+
5+
def heading_2():
6+
"""### Heading two"""
7+
8+
9+
def heading_3():
10+
"""#### Heading three"""

tests/test_extension.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,23 @@ def test_use_options_yaml_key(ext_markdown: Markdown) -> None:
152152
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 2")
153153

154154

155-
@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"pymdownx.tabbed": {"alternate_style": True}}]}], indirect=["ext_markdown"])
155+
@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"admonition": {}}]}], indirect=["ext_markdown"])
156156
def test_removing_duplicated_headings(ext_markdown: Markdown) -> None:
157157
"""Assert duplicated headings are removed from the output."""
158158
output = ext_markdown.convert(
159159
dedent(
160160
"""
161-
=== "Tab A"
161+
::: tests.fixtures.headings_many.heading_1
162162
163-
::: tests.fixtures.headings
163+
!!! note
164164
165+
::: tests.fixtures.headings_many.heading_2
166+
167+
::: tests.fixtures.headings_many.heading_3
165168
""",
166169
),
167170
)
168-
assert output.count("Foo") == 1
169-
assert output.count("Bar") == 1
170-
assert output.count("Baz") == 1
171+
assert output.count(">Heading one<") == 1
172+
assert output.count(">Heading two<") == 1
173+
assert output.count(">Heading three<") == 1
174+
assert output.count('class="mkdocstrings') == 0

0 commit comments

Comments
 (0)