Skip to content

Commit 609faad

Browse files
committed
Issue #365 Bold/Italic nesting fix
The logic for the current regex for strong/em and em/strong was sound, but the way it was implemented caused some unintended side effects. Whether it is a quirk with regex in general or just with Python’s re engine, I am not sure. Put basically `(\*|_){3}` causes issues with nested bold/italic. So, allowing the group to be defined, and then using the group number to specify the remaining sequential chars is a better way that works more reliably `(\*|_)\2{2}. Test from issue #365 was also added to check for this case in the future.
1 parent f0357b2 commit 609faad

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

markdown/inlinepatterns.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def build_inlinepatterns(md_instance, **kwargs):
101101
ESCAPE_RE = r'\\(.)' # \<
102102
EMPHASIS_RE = r'(\*)([^\*]+)\2' # *emphasis*
103103
STRONG_RE = r'(\*{2}|_{2})(.+?)\2' # **strong**
104-
EM_STRONG_RE = r'(\*|_){3}(.+?)\2(.*?)\2{2}' # ***strongem*** or ***em*strong**
105-
STRONG_EM_RE = r'(\*|_){3}(.+?)\2{2}(.*?)\2' # ***strong**em*
104+
EM_STRONG_RE = r'(\*|_)\2{2}(.+?)\2(.*?)\2{2}' # ***strongem*** or ***em*strong**
105+
STRONG_EM_RE = r'(\*|_)\2{2}(.+?)\2{2}(.*?)\2' # ***strong**em*
106106
SMART_EMPHASIS_RE = r'(?<!\w)(_)(?!_)(.+?)(?<!_)\2(?!\w)' # _smart_emphasis_
107107
EMPHASIS_2_RE = r'(_)(.+?)\2' # _emphasis_
108108
LINK_RE = NOIMG + BRK + \

tests/misc/nested-patterns.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
<strong><a href="http://example.com"><em>link</em></a></strong>
66
<strong><a href="http://example.com"><em>link</em></a></strong>
77
<a href="http://example.com"><strong><em>link</em></strong></a></p>
8-
<p><strong><em>I am <strong><em>italic</em> and</strong> bold</em> I am <code>just</code> bold</strong></p>
8+
<p><strong><em>I am <strong><em>italic</em> and</strong> bold</em> I am <code>just</code> bold</strong></p>
9+
<p>Example <strong><em>bold italic</em></strong> on the same line <strong><em>bold italic</em></strong>.</p>
10+
<p>Example <strong><em>bold italic</em></strong> on the same line <strong><em>bold italic</em></strong>.</p>

tests/misc/nested-patterns.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ __[*link*](http://example.com)__
77
[***link***](http://example.com)
88

99
***I am ___italic_ and__ bold* I am `just` bold**
10+
11+
Example __*bold italic*__ on the same line __*bold italic*__.
12+
13+
Example **_bold italic_** on the same line **_bold italic_**.

0 commit comments

Comments
 (0)