Skip to content

Commit

Permalink
fix: Fix removal of temporary div for headings forwarding
Browse files Browse the repository at this point in the history
Issue-50: #50
  • Loading branch information
pawamoy committed Jun 12, 2024
1 parent 3457ff0 commit c012c1d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/markdown_exec/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def run(self, root: Element) -> None: # noqa: D102 (ignore missing docstring)
if match:
counter = int(match.group(1))
markup: Markup = self.md.htmlStash.rawHtmlBlocks[counter] # type: ignore[assignment]
if markup in self.headings:
if headings := self.headings.get(markup):
div = Element("div", {"class": "markdown-exec"})
div.extend(self.headings[markup])
div.extend(headings)
el.append(div)


Expand All @@ -104,15 +104,20 @@ class RemoveHeadings(Treeprocessor):
name = "markdown_exec_remove_headings"

def run(self, root: Element) -> None: # noqa: D102
self._remove_duplicated_headings(root)

def _remove_duplicated_headings(self, parent: Element) -> None:
carry_text = ""
for el in reversed(root): # Reversed mainly for the ability to mutate during iteration.
for subel in reversed(el):
if subel.tag == "div" and subel.get("class") == "markdown-exec":
# Delete the duplicated headings along with their container, but keep the text (i.e. the actual HTML).
carry_text = (subel.text or "") + carry_text
el.remove(subel)
elif carry_text:
subel.tail = (subel.tail or "") + carry_text
for el in reversed(parent): # Reversed mainly for the ability to mutate during iteration.
if el.tag == "div" and el.get("class") == "markdown-exec":
# Delete the duplicated headings along with their container, but keep the text (i.e. the actual HTML).
carry_text = (el.text or "") + carry_text
parent.remove(el)
else:
if carry_text:
el.tail = (el.tail or "") + carry_text
carry_text = ""
if carry_text:
el.text = (el.text or "") + carry_text
self._remove_duplicated_headings(el)

if carry_text:
parent.text = (parent.text or "") + carry_text
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def md() -> Markdown:
for language in formatters
]
return Markdown(
extensions=["pymdownx.superfences"],
extensions=["pymdownx.superfences", "pymdownx.tabbed"],
extension_configs={"pymdownx.superfences": {"custom_fences": fences}},
)
30 changes: 30 additions & 0 deletions tests/test_headings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests for headings."""

from __future__ import annotations

from textwrap import dedent
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from markdown import Markdown


def test_headings_removal(md: Markdown) -> None:
"""Headings should leave no trace behind.
Parameters:
md: A Markdown instance (fixture).
"""
html = md.convert(
dedent(
"""
=== "File layout"
```tree
./
hello.md
```
""",
),
)
assert 'class="markdown-exec"' not in html

0 comments on commit c012c1d

Please # to comment.