@@ -6,20 +6,26 @@ package math
66import (
77 "bytes"
88
9+ giteaUtil "code.gitea.io/gitea/modules/util"
10+
911 "github.com/yuin/goldmark/ast"
1012 "github.com/yuin/goldmark/parser"
1113 "github.com/yuin/goldmark/text"
1214 "github.com/yuin/goldmark/util"
1315)
1416
1517type blockParser struct {
16- parseDollars bool
18+ parseDollars bool
19+ endBytesDollars []byte
20+ endBytesBracket []byte
1721}
1822
1923// NewBlockParser creates a new math BlockParser
2024func NewBlockParser (parseDollarBlocks bool ) parser.BlockParser {
2125 return & blockParser {
22- parseDollars : parseDollarBlocks ,
26+ parseDollars : parseDollarBlocks ,
27+ endBytesDollars : []byte {'$' , '$' },
28+ endBytesBracket : []byte {'\\' , ']' },
2329 }
2430}
2531
@@ -47,10 +53,7 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
4753 node := NewBlock (dollars , pos )
4854
4955 // Now we need to check if the ending block is on the segment...
50- endBytes := []byte {'\\' , ']' }
51- if dollars {
52- endBytes = []byte {'$' , '$' }
53- }
56+ endBytes := giteaUtil .Iif (dollars , b .endBytesDollars , b .endBytesBracket )
5457 idx := bytes .Index (line [pos + 2 :], endBytes )
5558 if idx >= 0 {
5659 // for case $$ ... $$ any other text
@@ -63,6 +66,7 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
6366 segment .Stop = segment .Start + idx
6467 node .Lines ().Append (segment )
6568 node .Closed = true
69+ node .Inline = true
6670 return node , parser .Close | parser .NoChildren
6771 }
6872
@@ -79,27 +83,19 @@ func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Cont
7983 }
8084
8185 line , segment := reader .PeekLine ()
82- w , pos := util .IndentWidth (line , 0 )
86+ w , pos := util .IndentWidth (line , reader . LineOffset () )
8387 if w < 4 {
84- if block .Dollars {
85- i := pos
86- for ; i < len (line ) && line [i ] == '$' ; i ++ {
87- }
88- length := i - pos
89- if length >= 2 && util .IsBlank (line [i :]) {
90- reader .Advance (segment .Stop - segment .Start - segment .Padding )
91- block .Closed = true
88+ endBytes := giteaUtil .Iif (block .Dollars , b .endBytesDollars , b .endBytesBracket )
89+ if bytes .HasPrefix (line [pos :], endBytes ) && util .IsBlank (line [pos + len (endBytes ):]) {
90+ if util .IsBlank (line [pos + len (endBytes ):]) {
91+ newline := giteaUtil .Iif (line [len (line )- 1 ] != '\n' , 0 , 1 )
92+ reader .Advance (segment .Stop - segment .Start - newline + segment .Padding )
9293 return parser .Close
9394 }
94- } else if len (line [pos :]) > 1 && line [pos ] == '\\' && line [pos + 1 ] == ']' && util .IsBlank (line [pos + 2 :]) {
95- reader .Advance (segment .Stop - segment .Start - segment .Padding )
96- block .Closed = true
97- return parser .Close
9895 }
9996 }
100-
101- pos , padding := util .IndentPosition (line , 0 , block .Indent )
102- seg := text .NewSegmentPadding (segment .Start + pos , segment .Stop , padding )
97+ start := segment .Start + giteaUtil .Iif (pos > block .Indent , block .Indent , pos )
98+ seg := text .NewSegmentPadding (start , segment .Stop , segment .Padding )
10399 node .Lines ().Append (seg )
104100 return parser .Continue | parser .NoChildren
105101}
0 commit comments