Skip to content

Commit

Permalink
better handle nested bold/italic
Browse files Browse the repository at this point in the history
based on #307
  • Loading branch information
kjk committed Jul 29, 2024
1 parent a2a9c4f commit 77f4768
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
14 changes: 13 additions & 1 deletion inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ func TestEmphasisMix(t *testing.T) {
"***triple emphasis___\n",
"<p>***triple emphasis___</p>\n",

"*italics **and bold** end*\n",
"<p><em>italics <strong>and bold</strong> end</em></p>\n",

"*italics **and bold***\n",
"<p><em>italics <strong>and bold</strong></em></p>\n",

"***bold** and italics*\n",
"<p><em><strong>bold</strong> and italics</em></p>\n",

"*start **bold** and italics*\n",
"<p><em>start <strong>bold</strong> and italics</em></p>\n",

"*__triple emphasis__*\n",
"<p><em><strong>triple emphasis</strong></em></p>\n",

Expand All @@ -183,7 +195,7 @@ func TestEmphasisMix(t *testing.T) {
"<p><strong>improper *nesting</strong> is* bad</p>\n",

"*improper **nesting* is** bad\n",
"<p>*improper <strong>nesting* is</strong> bad</p>\n",
"<p><em>improper **nesting</em> is** bad</p>\n",
}
doTestsInline(t, tests)
}
Expand Down
18 changes: 11 additions & 7 deletions parser/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,31 +1185,30 @@ func helperFindEmphChar(data []byte, c byte) int {
func helperEmphasis(p *Parser, data []byte, c byte) (int, ast.Node) {
i := 0

// skip one symbol if coming from emph3
// skip two symbol if coming from emph3, as it detected a double emphasis case
if len(data) > 1 && data[0] == c && data[1] == c {
i = 1
i = 2
}

for i < len(data) {
length := helperFindEmphChar(data[i:], c)
if length == 0 {
return 0, nil
}
i += length
if i >= len(data) {
return 0, nil
}

if i+1 < len(data) && data[i+1] == c {
i++
i += 2
continue
}

if data[i] == c && !IsSpace(data[i-1]) {

if p.extensions&NoIntraEmphasis != 0 {
rest := data[i+1:]
if !(len(rest) == 0 || IsSpace(rest[0]) || IsPunctuation2(rest)) {
if length == 0 {
return 0, nil
}
continue
}
}
Expand All @@ -1218,6 +1217,11 @@ func helperEmphasis(p *Parser, data []byte, c byte) (int, ast.Node) {
p.Inline(emph, data[:i])
return i + 1, emph
}

// We have to check this at the end, otherwise the scenario where we find repeated c's will get skipped
if length == 0 {
return 0, nil
}
}

return 0, nil
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ func IsPunctuation2(d []byte) bool {
return unicode.IsPunct(r)
}

// IsSpace returns true if c is a white-space charactr
// IsSpace returns true if c is a white-space character
func IsSpace(c byte) bool {
return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v'
}
Expand Down

0 comments on commit 77f4768

Please sign in to comment.