From 90bfcca9ba3ca79b21d911b027ad00cff352f7b4 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 15 Apr 2025 07:46:43 -0600 Subject: [PATCH 1/5] Ensure `md_in_html` does not drop content --- docs/changelog.md | 6 ++++++ markdown/extensions/md_in_html.py | 6 +++++- .../test_syntax/extensions/test_md_in_html.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 930199fe..3fc02035 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. +## [3.8.1] - xxxx-xx-xx + +### 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..5c89a5b2 100644 --- a/tests/test_syntax/extensions/test_md_in_html.py +++ b/tests/test_syntax/extensions/test_md_in_html.py @@ -1517,6 +1517,24 @@ def test_md1_code_cdata(self): extensions=['md_in_html'] ) + def test_trialing_content_after_tag_in_md_block(self): + + 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. """ From 19df5b63bb30a5134d475bbb2e8af2b18876bdfb Mon Sep 17 00:00:00 2001 From: Isaac Muse Date: Tue, 15 Apr 2025 09:00:07 -0600 Subject: [PATCH 2/5] Update tests/test_syntax/extensions/test_md_in_html.py Co-authored-by: Dmitry Shachnev --- tests/test_syntax/extensions/test_md_in_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_syntax/extensions/test_md_in_html.py b/tests/test_syntax/extensions/test_md_in_html.py index 5c89a5b2..ebd0b40c 100644 --- a/tests/test_syntax/extensions/test_md_in_html.py +++ b/tests/test_syntax/extensions/test_md_in_html.py @@ -1517,7 +1517,7 @@ def test_md1_code_cdata(self): extensions=['md_in_html'] ) - def test_trialing_content_after_tag_in_md_block(self): + def test_trailing_content_after_tag_in_md_block(self): self.assertMarkdownRenders( self.dedent( From 87b82a93f4a29ee8c36ed6c652b8b1ff6644dbf8 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Tue, 15 Apr 2025 09:04:11 -0600 Subject: [PATCH 3/5] Add note on the intention of the test --- tests/test_syntax/extensions/test_md_in_html.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_syntax/extensions/test_md_in_html.py b/tests/test_syntax/extensions/test_md_in_html.py index ebd0b40c..5ef860d5 100644 --- a/tests/test_syntax/extensions/test_md_in_html.py +++ b/tests/test_syntax/extensions/test_md_in_html.py @@ -1519,6 +1519,9 @@ def test_md1_code_cdata(self): 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( """ From 4aab1808d5e6f2e1a78e649adf2834a65c0f9de2 Mon Sep 17 00:00:00 2001 From: facelessuser Date: Wed, 16 Apr 2025 07:00:27 -0600 Subject: [PATCH 4/5] Remove date placeholder --- docs/changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 3fc02035..3a947af5 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -9,7 +9,7 @@ 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. -## [3.8.1] - xxxx-xx-xx +## [3.8.1] ### Fixed From d6cb42c1c616d32ae0669b73e341613ff0420e1d Mon Sep 17 00:00:00 2001 From: facelessuser Date: Wed, 16 Apr 2025 07:04:30 -0600 Subject: [PATCH 5/5] Change version number to "Unreleased" --- docs/changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 3a947af5..7c96a6f4 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -9,7 +9,7 @@ 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. -## [3.8.1] +## [Unreleased] ### Fixed