loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fixes #27605: inline math blocks can't be preceeded/followed by alphanumerical characters (#30175)

- Inline math blocks couldn't be preceeded or succeeded by
alphanumerical characters due to changes introduced in PR #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 <joao.leal.tintas@tecnico.ulisboa.pt>
Co-authored-by: Giteabot <teabot@gitea.io>
(cherry picked from commit e006451ab1509f8d6d43c5974387c05b26517392)

authored by

João Tiago
Giteabot
and committed by
Gergely Nagy
2adc3a45 63904e2f

+31 -7
+18 -2
modules/markup/markdown/markdown_test.go
··· 506 506 `<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl, 507 507 }, 508 508 { 509 + `$a$.`, 510 + `<p><code class="language-math is-loading">a</code>.</p>` + nl, 511 + }, 512 + { 513 + `.$a$`, 514 + `<p>.$a$</p>` + nl, 515 + }, 516 + { 509 517 `$a a$b b$`, 510 - `<p><code class="language-math is-loading">a a$b b</code></p>` + nl, 518 + `<p>$a a$b b$</p>` + nl, 511 519 }, 512 520 { 513 521 `a a$b b`, ··· 515 523 }, 516 524 { 517 525 `a$b $a a$b b$`, 518 - `<p>a$b <code class="language-math is-loading">a a$b b</code></p>` + nl, 526 + `<p>a$b $a a$b b$</p>` + nl, 527 + }, 528 + { 529 + "a$x$", 530 + `<p>a$x$</p>` + nl, 531 + }, 532 + { 533 + "$x$a", 534 + `<p>$x$a</p>` + nl, 519 535 }, 520 536 { 521 537 "$$a$$",
+13 -5
modules/markup/markdown/math/inline_parser.go
··· 41 41 return parser.start[0:1] 42 42 } 43 43 44 + func isPunctuation(b byte) bool { 45 + return b == '.' || b == '!' || b == '?' || b == ',' || b == ';' || b == ':' 46 + } 47 + 44 48 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') 47 50 } 48 51 49 52 // Parse parses the current line and returns a result of parsing. ··· 56 59 } 57 60 58 61 precedingCharacter := block.PrecendingCharacter() 59 - if precedingCharacter < 256 && isAlphanumeric(byte(precedingCharacter)) { 62 + if precedingCharacter < 256 && (isAlphanumeric(byte(precedingCharacter)) || isPunctuation(byte(precedingCharacter))) { 60 63 // need to exclude things like `a$` from being considered a start 61 64 return nil 62 65 } ··· 75 78 ender += pos 76 79 77 80 // 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 '\' 79 82 pos = ender + len(parser.end) 80 83 if len(line) <= pos { 81 84 break 82 85 } 83 - if !isAlphanumeric(line[pos]) { 86 + suceedingCharacter := line[pos] 87 + if !isPunctuation(suceedingCharacter) && !(suceedingCharacter == ' ') { 88 + return nil 89 + } 90 + if line[ender-1] != '\\' { 84 91 break 85 92 } 93 + 86 94 // move the pointer onwards 87 95 ender += len(parser.end) 88 96 }