Skip to content

Commit 1c69fdc

Browse files
authored
Improve math rendering (#36124)
Fix #36108 Fix #36107
1 parent ed698d1 commit 1c69fdc

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

modules/markup/markdown/markdown_math_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func TestMathRender(t *testing.T) {
3030
"$ a $",
3131
`<p><code class="language-math">a</code></p>` + nl,
3232
},
33+
{
34+
"$a$$b$",
35+
`<p><code class="language-math">a</code><code class="language-math">b</code></p>` + nl,
36+
},
3337
{
3438
"$a$ $b$",
3539
`<p><code class="language-math">a</code> <code class="language-math">b</code></p>` + nl,
@@ -59,7 +63,7 @@ func TestMathRender(t *testing.T) {
5963
`<p>a$b $a a$b b$</p>` + nl,
6064
},
6165
{
62-
"a$x$",
66+
"a$x$", // Pattern: "word$other$" The real world example is: "Price is between US$1 and US$2.", so don't parse this.
6367
`<p>a$x$</p>` + nl,
6468
},
6569
{
@@ -70,6 +74,10 @@ func TestMathRender(t *testing.T) {
7074
"$a$ ($b$) [$c$] {$d$}",
7175
`<p><code class="language-math">a</code> (<code class="language-math">b</code>) [$c$] {$d$}</p>` + nl,
7276
},
77+
{
78+
"[$a$](link)",
79+
`<p><a href="/link" rel="nofollow"><code class="language-math">a</code></a></p>` + nl,
80+
},
7381
{
7482
"$$a$$",
7583
`<p><code class="language-math">a</code></p>` + nl,

modules/markup/markdown/math/inline_parser.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func isAlphanumeric(b byte) bool {
5454
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
5555
}
5656

57+
func isInMarkdownLinkText(block text.Reader, lineAfter []byte) bool {
58+
return block.PrecendingCharacter() == '[' && bytes.HasPrefix(lineAfter, []byte("]("))
59+
}
60+
5761
// Parse parses the current line and returns a result of parsing.
5862
func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
5963
line, _ := block.PeekLine()
@@ -115,7 +119,9 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
115119
}
116120
// check valid ending character
117121
isValidEndingChar := isPunctuation(succeedingCharacter) || isParenthesesClose(succeedingCharacter) ||
118-
succeedingCharacter == ' ' || succeedingCharacter == '\n' || succeedingCharacter == 0
122+
succeedingCharacter == ' ' || succeedingCharacter == '\n' || succeedingCharacter == 0 ||
123+
succeedingCharacter == '$' ||
124+
isInMarkdownLinkText(block, line[i+len(stopMark):])
119125
if checkSurrounding && !isValidEndingChar {
120126
break
121127
}

0 commit comments

Comments
 (0)