From 1edf43a7d2092c0b5fbcd8562d7a6a9de136a687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Tiago?= Date: Thu, 28 Mar 2024 18:41:55 +0000 Subject: [PATCH 1/4] Fix #27605: inline math blocks can't be preceeded/followed by alphanumerical characters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) and added a new if statement that checks if a specific '$' was escaped using '\'. Signed-off-by: João Tiago --- modules/markup/markdown/markdown_test.go | 4 ++-- modules/markup/markdown/math/inline_parser.go | 16 +++------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index ebac3fbe9e8e0..cddfb0c85166d 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 a$b 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$`, - `

a$b a a$b b

` + nl, + `

ab a ab b

` + nl, }, { "$$a$$", diff --git a/modules/markup/markdown/math/inline_parser.go b/modules/markup/markdown/math/inline_parser.go index 0ac25c2b2ac86..42c4996668bc5 100644 --- a/modules/markup/markdown/math/inline_parser.go +++ b/modules/markup/markdown/math/inline_parser.go @@ -41,11 +41,6 @@ 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() @@ -55,12 +50,6 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser. return nil } - precedingCharacter := block.PrecendingCharacter() - if precedingCharacter < 256 && isAlphanumeric(byte(precedingCharacter)) { - // need to exclude things like `a$` from being considered a start - return nil - } - // move the opener marker point at the start of the text opener := len(parser.start) @@ -75,14 +64,15 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser. ender += pos // Now we want to check the character at the end of our parser section - // that is ender + len(parser.end) + // that is ender + len(parser.end) and check if char before ender is '\' pos = ender + len(parser.end) if len(line) <= pos { break } - if !isAlphanumeric(line[pos]) { + if line[ender-1] != '\\' { break } + // move the pointer onwards ender += len(parser.end) } From e375bf384081b52edd2e29799143fdf09fb14c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Tiago?= Date: Fri, 29 Mar 2024 15:45:01 +0000 Subject: [PATCH 2/4] Add 2 test cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added one test case to check that math blocks render correctly when preceeded by an alphanumerical character, and another one for when it is succeeded by an alphanumerical character. Signed-off-by: João Tiago --- modules/markup/markdown/markdown_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index cddfb0c85166d..808c7c4e69b57 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -519,6 +519,14 @@ func TestMathBlock(t *testing.T) { `a$b $a a$b b$`, `

ab a ab b

` + nl, }, + { + "a$x$", + `

ax

` + nl, + }, + { + "$x$a", + `

xa

` + nl, + }, { "$$a$$", `
a
` + nl, 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 3/4] 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 } From eb7546e34255884ce08fdf5e76184b0ab0d4c7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Tiago?= Date: Mon, 1 Apr 2024 19:20:02 +0100 Subject: [PATCH 4/4] Allow math expressions to be suceeded only by punctuation. 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 | 14 +++++++++++--- modules/markup/markdown/math/inline_parser.go | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index bfc44728d4fa3..f69a26644f383 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -507,9 +507,17 @@ func TestMathBlock(t *testing.T) { `\(a\) \(b\)`, `

a b

` + nl, }, + { + `$a$.`, + `

a.

` + nl, + }, + { + `.$a$`, + `

.$a$

` + nl, + }, { `$a a$b b$`, - `

$a ab b

` + nl, + `

$a a$b b$

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

a$b $a ab b

` + nl, + `

a$b $a a$b b$

` + nl, }, { "a$x$", - `

ax

` + nl, + `

a$x$

` + nl, }, { "$x$a", diff --git a/modules/markup/markdown/math/inline_parser.go b/modules/markup/markdown/math/inline_parser.go index 09fcd45ffdbd1..862234e69bb74 100644 --- a/modules/markup/markdown/math/inline_parser.go +++ b/modules/markup/markdown/math/inline_parser.go @@ -41,9 +41,12 @@ func (parser *inlineParser) Trigger() []byte { return parser.start[0:1] } +func isPunctuation(b byte) bool { + return b == '.' || b == '!' || b == '?' || b == ',' || b == ';' || b == ':' +} + 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') + return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9') } // Parse parses the current line and returns a result of parsing. @@ -55,6 +58,12 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser. return nil } + precedingCharacter := block.PrecendingCharacter() + if precedingCharacter < 256 && (isAlphanumeric(byte(precedingCharacter)) || isPunctuation(byte(precedingCharacter))) { + // need to exclude things like `a$` from being considered a start + return nil + } + // move the opener marker point at the start of the text opener := len(parser.start) @@ -74,7 +83,8 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser. if len(line) <= pos { break } - if isAlphanumeric(line[pos]) { + suceedingCharacter := line[pos] + if !isPunctuation(suceedingCharacter) && !(suceedingCharacter == ' ') { return nil } if line[ender-1] != '\\' {