Skip to content

Commit

Permalink
Fix #27605: inline math blocks can't be preceeded/followed by alphanu…
Browse files Browse the repository at this point in the history
…merical characters

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 <[email protected]>
  • Loading branch information
jmlt2002 committed Mar 29, 2024
1 parent e40fc75 commit 1edf43a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
4 changes: 2 additions & 2 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,15 +509,15 @@ func TestMathBlock(t *testing.T) {
},
{
`$a a$b b$`,
`<p><code class="language-math is-loading">a a$b b</code></p>` + nl,
`<p><code class="language-math is-loading">a a</code>b b$</p>` + nl,
},
{
`a a$b b`,
`<p>a a$b b</p>` + nl,
},
{
`a$b $a a$b b$`,
`<p>a$b <code class="language-math is-loading">a a$b b</code></p>` + nl,
`<p>a<code class="language-math is-loading">b </code>a a<code class="language-math is-loading">b b</code></p>` + nl,
},
{
"$$a$$",
Expand Down
16 changes: 3 additions & 13 deletions modules/markup/markdown/math/inline_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand All @@ -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)
}
Expand Down

0 comments on commit 1edf43a

Please sign in to comment.