Skip to content

Commit

Permalink
Merge pull request #560 from Crozzers/always-split-markdown-in-html
Browse files Browse the repository at this point in the history
Fix `markdown-in-html` not always splitting HTML tags into separate lines (#558)
  • Loading branch information
nicholasserra committed Dec 26, 2023
2 parents 844942a + 0203031 commit b94feac
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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>

0 comments on commit b94feac

Please sign in to comment.