Skip to content
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

Fix markdown-in-html not always splitting HTML tags into separate lines (#558) #560

Merged
merged 3 commits into from
Dec 26, 2023
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## python-markdown2 2.4.13 (not yet released)

- [pull #559] Allow cuddled tables (#557)
- [pull #560] Fix `markdown-in-html` not always splitting HTML tags into separate lines (#558)


## python-markdown2 2.4.12
Expand Down
12 changes: 8 additions & 4 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,13 +800,17 @@ def _hash_html_block_sub(self, match, raw=False):
m = self._html_markdown_attr_re.search(first_line)
if m:
lines = html.split('\n')
if len(lines) < 3: # if MD is on same line as HTML
lines = re.split(r'(<%s.*markdown=.*?>)' % tag, lines[0])[1:] + lines[1:]
first_line = lines[0]
lines = lines[:-1] + re.split(r'(</%s>.*?$)' % tag, lines[-1])[:-1]
# if MD is on same line as opening tag then split across two lines
lines = list(filter(None, (re.split(r'(<%s.*markdown=.*?>)' % tag, lines[0])))) + lines[1:]
# if MD on same line as closing tag, split across two lines
lines = lines[:-1] + list(filter(None, re.split(r'(</%s>.*?$)' % tag, lines[-1])))
# extract key sections of the match
first_line = lines[0]
middle = '\n'.join(lines[1:-1])
last_line = lines[-1]
# remove `markdown="1"` attr from tag
first_line = first_line[:m.start()] + first_line[m.end():]
# hash the HTML segments to protect them
f_key = _hash_text(first_line)
self.html_blocks[f_key] = first_line
l_key = _hash_text(last_line)
Expand Down
17 changes: 17 additions & 0 deletions test/tm-cases/markdown_in_html_on_same_line.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@
<p><strong>text</strong></p>

</p>

<p>

<p><strong>text</strong>
<strong>text</strong>
<strong>text</strong></p>

</p>

<p>

<p><strong>text</strong>
<strong>text</strong>
<strong>text</strong>
<strong>text</strong></p>

</p>
10 changes: 10 additions & 0 deletions test/tm-cases/markdown_in_html_on_same_line.text
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@
<p markdown="1">
**text**
</p>

<p markdown="1">**text**
**text**
**text**
</p>

<p markdown="1">**text**
**text**
**text**
**text**</p>
Loading