Skip to content

Commit f2b9fd1

Browse files
Ensure md_in_html does not drop content
Fixes #1526. Co-authored-by: Dmitry Shachnev <[email protected]>
1 parent 513de8a commit f2b9fd1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Diff for: docs/changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
99
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1010
See the [Contributing Guide](contributing.md) for details.
1111

12+
## [Unreleased]
13+
14+
### Fixed
15+
16+
* Fixed dropped content in `md_in_html` (#1526).
17+
1218
## [3.8.0] - 2025-04-09
1319

1420
### Changed

Diff for: markdown/extensions/md_in_html.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,16 @@ def run(self, parent: etree.Element, blocks: list[str]) -> bool:
387387
element = self.parser.md.htmlStash.rawHtmlBlocks[index]
388388
if isinstance(element, etree.Element):
389389
# We have a matched element. Process it.
390-
blocks.pop(0)
390+
block = blocks.pop(0)
391391
parent.append(element)
392392
self.parse_element_content(element)
393393
# Cleanup stash. Replace element with empty string to avoid confusing postprocessor.
394394
self.parser.md.htmlStash.rawHtmlBlocks.pop(index)
395395
self.parser.md.htmlStash.rawHtmlBlocks.insert(index, '')
396+
content = block[m.end(0):]
397+
# Ensure the rest of the content gets handled
398+
if content:
399+
blocks.insert(0, content)
396400
# Confirm the match to the `blockparser`.
397401
return True
398402
# No match found.

Diff for: tests/test_syntax/extensions/test_md_in_html.py

+21
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,27 @@ def test_md1_code_cdata(self):
15171517
extensions=['md_in_html']
15181518
)
15191519

1520+
def test_trailing_content_after_tag_in_md_block(self):
1521+
1522+
# It should be noted that this is not the way `md_in_html` is intended to be used.
1523+
# What we are specifically testing is an edge case where content was previously lost.
1524+
# Lost content should not happen.
1525+
self.assertMarkdownRenders(
1526+
self.dedent(
1527+
"""
1528+
<div markdown>
1529+
<div class="circle"></div>AAAAA<div class="circle"></div>
1530+
</div>
1531+
"""
1532+
),
1533+
'<div>\n'
1534+
'<div class="circle"></div>\n'
1535+
'<p>AAAAA<div class="circle"></p>\n'
1536+
'</div>\n'
1537+
'</div>',
1538+
extensions=['md_in_html']
1539+
)
1540+
15201541

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

0 commit comments

Comments
 (0)