Skip to content

Commit

Permalink
Merge pull request #547 from Crozzers/markdown-in-html-same-line
Browse files Browse the repository at this point in the history
Update `markdown-in-html` extra to handle markdown on same line as HTML (#546)
  • Loading branch information
nicholasserra committed Dec 10, 2023
2 parents f7b4535 + f5d0f40 commit f817f0b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,7 @@

## python-markdown2 2.4.12 (not yet released)

- [pull #547] Update `markdown-in-html` extra to handle markdown on same line as HTML (#546)
- [pull #550] Fix tables with trailing whitespace not being recognized (#549)


Expand Down
11 changes: 11 additions & 0 deletions lib/markdown2.py
Expand Up @@ -783,8 +783,15 @@ def _detab(self, text):
def _hash_html_block_sub(self, match, raw=False):
if isinstance(match, str):
html = match
tag = None
else:
html = match.group(1)
try:
tag = match.group(2)
except IndexError:
tag = None

tag = tag or re.match(r'^<(\S).*?>', html).group(1)

if raw and self.safe_mode:
html = self._sanitize_html(html)
Expand All @@ -793,6 +800,10 @@ 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]
middle = '\n'.join(lines[1:-1])
last_line = lines[-1]
first_line = first_line[:m.start()] + first_line[m.end():]
Expand Down
23 changes: 23 additions & 0 deletions test/tm-cases/markdown_in_html_on_same_line.html
@@ -0,0 +1,23 @@
<p>

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

</p>

<p>

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

</p>

<p>

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

</p>

<p>

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

</p>
1 change: 1 addition & 0 deletions test/tm-cases/markdown_in_html_on_same_line.opts
@@ -0,0 +1 @@
{"extras": ["markdown-in-html"]}
12 changes: 12 additions & 0 deletions test/tm-cases/markdown_in_html_on_same_line.text
@@ -0,0 +1,12 @@
<p markdown="1">**text**</p>


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

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

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

0 comments on commit f817f0b

Please sign in to comment.