From 88f41f3072458f175e942b3195626232db8c4d57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Tiago?= Date: Sun, 31 Mar 2024 17:01:15 +0100 Subject: [PATCH] Allow math expressions to be preceeded but not suceeded by alphanumerical characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Tiago --- modules/markup/markdown/markdown_test.go | 6 +++--- modules/markup/markdown/math/inline_parser.go | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 808c7c4e69b57..bfc44728d4fa3 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -509,7 +509,7 @@ func TestMathBlock(t *testing.T) { }, { `$a a$b b$`, - `

a ab b$

` + nl, + `

$a ab b

` + nl, }, { `a a$b b`, @@ -517,7 +517,7 @@ func TestMathBlock(t *testing.T) { }, { `a$b $a a$b b$`, - `

ab a ab b

` + nl, + `

a$b $a ab b

` + nl, }, { "a$x$", @@ -525,7 +525,7 @@ func TestMathBlock(t *testing.T) { }, { "$x$a", - `

xa

` + nl, + `

$x$a

` + nl, }, { "$$a$$", diff --git a/modules/markup/markdown/math/inline_parser.go b/modules/markup/markdown/math/inline_parser.go index 42c4996668bc5..09fcd45ffdbd1 100644 --- a/modules/markup/markdown/math/inline_parser.go +++ b/modules/markup/markdown/math/inline_parser.go @@ -41,6 +41,11 @@ func (parser *inlineParser) Trigger() []byte { return parser.start[0:1] } +func isAlphanumeric(b byte) bool { + // Github only cares about 0-9A-Za-z + return (b >= '0' && b <= '9') || (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') +} + // Parse parses the current line and returns a result of parsing. func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node { line, _ := block.PeekLine() @@ -69,6 +74,9 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser. if len(line) <= pos { break } + if isAlphanumeric(line[pos]) { + return nil + } if line[ender-1] != '\\' { break }