Skip to content

Ensure md_in_html does not drop content #1527

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion markdown/extensions/md_in_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_syntax/extensions/test_md_in_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
<div markdown>
<div class="circle"></div>AAAAA<div class="circle"></div>
</div>
"""
),
'<div>\n'
'<div class="circle"></div>\n'
'<p>AAAAA<div class="circle"></p>\n'
'</div>\n'
'</div>',
extensions=['md_in_html']
)


def load_tests(loader, tests, pattern):
""" Ensure `TestHTMLBlocks` doesn't get run twice by excluding it here. """
Expand Down