@@ -6,20 +6,26 @@ package math
6
6
import (
7
7
"bytes"
8
8
9
+ giteaUtil "code.gitea.io/gitea/modules/util"
10
+
9
11
"github.com/yuin/goldmark/ast"
10
12
"github.com/yuin/goldmark/parser"
11
13
"github.com/yuin/goldmark/text"
12
14
"github.com/yuin/goldmark/util"
13
15
)
14
16
15
17
type blockParser struct {
16
- parseDollars bool
18
+ parseDollars bool
19
+ endBytesDollars []byte
20
+ endBytesBracket []byte
17
21
}
18
22
19
23
// NewBlockParser creates a new math BlockParser
20
24
func NewBlockParser (parseDollarBlocks bool ) parser.BlockParser {
21
25
return & blockParser {
22
- parseDollars : parseDollarBlocks ,
26
+ parseDollars : parseDollarBlocks ,
27
+ endBytesDollars : []byte {'$' , '$' },
28
+ endBytesBracket : []byte {'\\' , ']' },
23
29
}
24
30
}
25
31
@@ -47,10 +53,7 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
47
53
node := NewBlock (dollars , pos )
48
54
49
55
// 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 )
54
57
idx := bytes .Index (line [pos + 2 :], endBytes )
55
58
if idx >= 0 {
56
59
// for case $$ ... $$ any other text
@@ -63,6 +66,7 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
63
66
segment .Stop = segment .Start + idx
64
67
node .Lines ().Append (segment )
65
68
node .Closed = true
69
+ node .Inline = true
66
70
return node , parser .Close | parser .NoChildren
67
71
}
68
72
@@ -79,27 +83,19 @@ func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Cont
79
83
}
80
84
81
85
line , segment := reader .PeekLine ()
82
- w , pos := util .IndentWidth (line , 0 )
86
+ w , pos := util .IndentWidth (line , reader . LineOffset () )
83
87
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 )
92
93
return parser .Close
93
94
}
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
98
95
}
99
96
}
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 )
103
99
node .Lines ().Append (seg )
104
100
return parser .Continue | parser .NoChildren
105
101
}
0 commit comments