Skip to content

Commit 6456983

Browse files
jmlt2002GiteaBot
andcommitted
Fixes go-gitea#27605: inline math blocks can't be preceeded/followed by alphanumerical characters (go-gitea#30175)
- Inline math blocks couldn't be preceeded or succeeded by alphanumerical characters due to changes introduced in PR go-gitea#21171. Removed the condition that caused this (precedingCharacter condition) and added a new exit condition of the for-loop that checks if a specific '$' was escaped using '\' so that the math expression can be rendered as intended. - Additionally this PR fixes another bug where math blocks of the type '$xyz$abc$' where the dollar sign was not escaped by the user, generated an error (shown in the screenshots below) - Altered the tests to accomodate for the changes Former behaviour (from try.gitea.io): ![image](https://github.com/go-gitea/gitea/assets/114936010/8f0cbb21-321d-451c-b871-c67a8e1e9235) Fixed behaviour (from my local build): ![image](https://github.com/go-gitea/gitea/assets/114936010/5c22687c-6f11-4407-b5e7-c14b838bc20d) (Edit) Source code for the README.md file: ``` $x$ -$x$ $x$- a$xa$ $xa$a 1$xb$ $xb$1 $a a$b b$ a$b $a a$b b$ $a a\$b b$ ``` --------- Signed-off-by: João Tiago <[email protected]> Co-authored-by: Giteabot <[email protected]>
1 parent deaabf6 commit 6456983

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

modules/markup/markdown/markdown_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,17 +511,33 @@ func TestMathBlock(t *testing.T) {
511511
`\(a\) \(b\)`,
512512
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
513513
},
514+
{
515+
`$a$.`,
516+
`<p><code class="language-math is-loading">a</code>.</p>` + nl,
517+
},
518+
{
519+
`.$a$`,
520+
`<p>.$a$</p>` + nl,
521+
},
514522
{
515523
`$a a$b b$`,
516-
`<p><code class="language-math is-loading">a a$b b</code></p>` + nl,
524+
`<p>$a a$b b$</p>` + nl,
517525
},
518526
{
519527
`a a$b b`,
520528
`<p>a a$b b</p>` + nl,
521529
},
522530
{
523531
`a$b $a a$b b$`,
524-
`<p>a$b <code class="language-math is-loading">a a$b b</code></p>` + nl,
532+
`<p>a$b $a a$b b$</p>` + nl,
533+
},
534+
{
535+
"a$x$",
536+
`<p>a$x$</p>` + nl,
537+
},
538+
{
539+
"$x$a",
540+
`<p>$x$a</p>` + nl,
525541
},
526542
{
527543
"$$a$$",

modules/markup/markdown/math/inline_parser.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ func (parser *inlineParser) Trigger() []byte {
4141
return parser.start[0:1]
4242
}
4343

44+
func isPunctuation(b byte) bool {
45+
return b == '.' || b == '!' || b == '?' || b == ',' || b == ';' || b == ':'
46+
}
47+
4448
func isAlphanumeric(b byte) bool {
45-
// Github only cares about 0-9A-Za-z
46-
return (b >= '0' && b <= '9') || (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z')
49+
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
4750
}
4851

4952
// Parse parses the current line and returns a result of parsing.
@@ -56,7 +59,7 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
5659
}
5760

5861
precedingCharacter := block.PrecendingCharacter()
59-
if precedingCharacter < 256 && isAlphanumeric(byte(precedingCharacter)) {
62+
if precedingCharacter < 256 && (isAlphanumeric(byte(precedingCharacter)) || isPunctuation(byte(precedingCharacter))) {
6063
// need to exclude things like `a$` from being considered a start
6164
return nil
6265
}
@@ -75,14 +78,19 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
7578
ender += pos
7679

7780
// Now we want to check the character at the end of our parser section
78-
// that is ender + len(parser.end)
81+
// that is ender + len(parser.end) and check if char before ender is '\'
7982
pos = ender + len(parser.end)
8083
if len(line) <= pos {
8184
break
8285
}
83-
if !isAlphanumeric(line[pos]) {
86+
suceedingCharacter := line[pos]
87+
if !isPunctuation(suceedingCharacter) && !(suceedingCharacter == ' ') {
88+
return nil
89+
}
90+
if line[ender-1] != '\\' {
8491
break
8592
}
93+
8694
// move the pointer onwards
8795
ender += len(parser.end)
8896
}

0 commit comments

Comments
 (0)