diff --git a/docs/changelog.md b/docs/changelog.md index 930199fe..7c96a6f4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -9,6 +9,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). See the [Contributing Guide](contributing.md) for details. +## [Unreleased] + +### Fixed + +* Fixed dropped content in `md_in_html` (#1526). + ## [3.8.0] - 2025-04-09 ### Changed diff --git a/markdown/extensions/md_in_html.py b/markdown/extensions/md_in_html.py index d1fbd7af..ba73c942 100644 --- a/markdown/extensions/md_in_html.py +++ b/markdown/extensions/md_in_html.py @@ -387,12 +387,16 @@ def run(self, parent: etree.Element, blocks: list[str]) -> bool: element = self.parser.md.htmlStash.rawHtmlBlocks[index] if isinstance(element, etree.Element): # We have a matched element. Process it. - blocks.pop(0) + block = blocks.pop(0) parent.append(element) self.parse_element_content(element) # Cleanup stash. Replace element with empty string to avoid confusing postprocessor. self.parser.md.htmlStash.rawHtmlBlocks.pop(index) self.parser.md.htmlStash.rawHtmlBlocks.insert(index, '') + content = block[m.end(0):] + # Ensure the rest of the content gets handled + if content: + blocks.insert(0, content) # Confirm the match to the `blockparser`. return True # No match found. diff --git a/tests/test_syntax/extensions/test_md_in_html.py b/tests/test_syntax/extensions/test_md_in_html.py index 1bdca393..5ef860d5 100644 --- a/tests/test_syntax/extensions/test_md_in_html.py +++ b/tests/test_syntax/extensions/test_md_in_html.py @@ -1517,6 +1517,27 @@ def test_md1_code_cdata(self): extensions=['md_in_html'] ) + def test_trailing_content_after_tag_in_md_block(self): + + # It should be noted that this is not the way `md_in_html` is intended to be used. + # What we are specifically testing is an edge case where content was previously lost. + # Lost content should not happen. + self.assertMarkdownRenders( + self.dedent( + """ +
+
AAAAA
+
+ """ + ), + '
\n' + '
\n' + '

AAAAA

\n' + '
\n' + '
', + extensions=['md_in_html'] + ) + def load_tests(loader, tests, pattern): """ Ensure `TestHTMLBlocks` doesn't get run twice by excluding it here. """