Skip to content
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

Remove duplicated headings for docstrings nested in tabs/admonitions #610

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/mkdocstrings/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,26 @@ def _process_block(

class _PostProcessor(Treeprocessor):
def run(self, root: Element) -> None:
self.remove_duplicated_headings_from_parent(root)

def remove_duplicated_headings_from_parent(self, parent: Element) -> bool:
percevalw marked this conversation as resolved.
Show resolved Hide resolved
carry_text = ""
for el in reversed(root): # Reversed mainly for the ability to mutate during iteration.
found = False
for el in reversed(parent):
percevalw marked this conversation as resolved.
Show resolved Hide resolved
if el.tag == "div" and el.get("class") == "mkdocstrings":
# Delete the duplicated headings along with their container, but keep the text (i.e. the actual HTML).
carry_text = (el.text or "") + carry_text
root.remove(el)
parent.remove(el)
found = True
elif carry_text:
el.tail = (el.tail or "") + carry_text
carry_text = ""
elif self.remove_duplicated_headings_from_parent(el):
percevalw marked this conversation as resolved.
Show resolved Hide resolved
found = True
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a wrong behavior: "If duplicated headings were found in a child element, stop scanning the rest of the document upwards". Why?

Copy link
Member

@pawamoy pawamoy Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there's no specific "why" and the assumption is that duplicated headings appear in a single child element. Do you recommend that we try and remove them from the whole subtree?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this was my assumption, that transformations would only nest the duplicated heading block without repeating it. Breaking early should improve the performance if the tree is large, but I have not idea by how much or if this was necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results from the child element are being propagated to the parent loop and it gets interrupted.

if carry_text:
root.text = (root.text or "") + carry_text
parent.text = (parent.text or "") + carry_text
return found


class MkdocstringsExtension(Extension):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,23 @@ def test_use_options_yaml_key(ext_markdown: Markdown) -> None:
"""Check that using the 'options' YAML key works as expected."""
assert "h1" in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 1")
assert "h1" not in ext_markdown.convert("::: tests.fixtures.headings\n options:\n heading_level: 2")


@pytest.mark.parametrize("ext_markdown", [{"markdown_extensions": [{"pymdownx.tabbed": {"alternate_style": True}}]}], indirect=["ext_markdown"])
def test_tab_headings(ext_markdown: Markdown) -> None:
percevalw marked this conversation as resolved.
Show resolved Hide resolved
"""Assert footnotes don't get added to subsequent docstrings."""
percevalw marked this conversation as resolved.
Show resolved Hide resolved
output = ext_markdown.convert(
dedent(
"""
Top.[^aaa]

percevalw marked this conversation as resolved.
Show resolved Hide resolved
=== "Tab A"

::: tests.fixtures.headings

""",
),
)
assert output.count("Foo") == 1
assert output.count("Bar") == 1
assert output.count("Baz") == 1