Skip to content

Commit 62a0952

Browse files
committed
fix
1 parent 0f18046 commit 62a0952

File tree

5 files changed

+77
-31
lines changed

5 files changed

+77
-31
lines changed

modules/markup/markdown/markdown_math_test.go

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestMathRender(t *testing.T) {
6868
},
6969
{
7070
"$$a$$",
71-
`<pre class="code-block is-loading"><code class="chroma language-math display">a</code></pre>` + nl,
71+
`<code class="chroma language-math display">a</code>` + nl,
7272
},
7373
{
7474
"$$a$$ test",
@@ -79,9 +79,13 @@ func TestMathRender(t *testing.T) {
7979
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
8080
},
8181
{
82-
"foo $x=\\$$ bar",
82+
`foo $x=\$$ bar`,
8383
`<p>foo <code class="language-math is-loading">x=\$</code> bar</p>` + nl,
8484
},
85+
{
86+
`$\text{$b$}$`,
87+
`<p><code class="language-math is-loading">\text{$b$}</code></p>` + nl,
88+
},
8589
}
8690

8791
for _, test := range testcases {
@@ -119,7 +123,7 @@ func TestMathRenderBlockIndent(t *testing.T) {
119123
\]
120124
`,
121125
`<pre class="code-block is-loading"><code class="chroma language-math display">
122-
\alpha
126+
\alpha
123127
</code></pre>
124128
`,
125129
},
@@ -131,24 +135,62 @@ func TestMathRenderBlockIndent(t *testing.T) {
131135
\]
132136
`,
133137
`<pre class="code-block is-loading"><code class="chroma language-math display">
134-
\alpha
138+
\alpha
135139
</code></pre>
136140
`,
137141
},
138142
{
139143
"indent-0-oneline",
140144
`$$ x $$
141145
foo`,
142-
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
146+
`<code class="chroma language-math display"> x </code>
143147
<p>foo</p>
144148
`,
145149
},
146150
{
147151
"indent-3-oneline",
148152
` $$ x $$<SPACE>
149153
foo`,
150-
`<pre class="code-block is-loading"><code class="chroma language-math display"> x </code></pre>
154+
`<code class="chroma language-math display"> x </code>
151155
<p>foo</p>
156+
`,
157+
},
158+
{
159+
"quote-block",
160+
`
161+
> \[
162+
> a
163+
> \]
164+
> \[
165+
> b
166+
> \]
167+
`,
168+
`<blockquote>
169+
<pre class="code-block is-loading"><code class="chroma language-math display">
170+
a
171+
</code></pre>
172+
<pre class="code-block is-loading"><code class="chroma language-math display">
173+
b
174+
</code></pre>
175+
</blockquote>
176+
`,
177+
},
178+
{
179+
"list-block",
180+
`
181+
1. a
182+
\[
183+
x
184+
\]
185+
2. b`,
186+
`<ol>
187+
<li>a
188+
<pre class="code-block is-loading"><code class="chroma language-math display">
189+
x
190+
</code></pre>
191+
</li>
192+
<li>b</li>
193+
</ol>
152194
`,
153195
},
154196
}

modules/markup/markdown/math/block_node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Block struct {
1111
Dollars bool
1212
Indent int
1313
Closed bool
14+
Inline bool
1415
}
1516

1617
// KindBlock is the node kind for math blocks

modules/markup/markdown/math/block_parser.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,26 @@ package math
66
import (
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

1517
type blockParser struct {
16-
parseDollars bool
18+
parseDollars bool
19+
endBytesDollars []byte
20+
endBytesBracket []byte
1721
}
1822

1923
// NewBlockParser creates a new math BlockParser
2024
func 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,18 @@ 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+
seg := text.NewSegmentPadding(segment.Start, segment.Stop, segment.Padding)
10398
node.Lines().Append(seg)
10499
return parser.Continue | parser.NoChildren
105100
}

modules/markup/markdown/math/block_renderer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package math
55

66
import (
77
"code.gitea.io/gitea/modules/markup/internal"
8+
giteaUtil "code.gitea.io/gitea/modules/util"
89

910
gast "github.com/yuin/goldmark/ast"
1011
"github.com/yuin/goldmark/renderer"
@@ -37,10 +38,11 @@ func (r *BlockRenderer) writeLines(w util.BufWriter, source []byte, n gast.Node)
3738
func (r *BlockRenderer) renderBlock(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
3839
n := node.(*Block)
3940
if entering {
40-
_ = r.renderInternal.FormatWithSafeAttrs(w, `<pre class="code-block is-loading"><code class="chroma language-math display">`)
41+
code := giteaUtil.Iif(n.Inline, "", `<pre class="code-block is-loading">`) + `<code class="chroma language-math display">`
42+
_ = r.renderInternal.FormatWithSafeAttrs(w, code)
4143
r.writeLines(w, source, n)
4244
} else {
43-
_, _ = w.WriteString(`</code></pre>` + "\n")
45+
_, _ = w.WriteString(`</code>` + giteaUtil.Iif(n.Inline, "", `</pre>`) + "\n")
4446
}
4547
return gast.WalkContinue, nil
4648
}

modules/markup/markdown/math/inline_parser.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
7979
opener := len(parser.start)
8080

8181
// Now look for an ending line
82+
depth := 0
8283
ender := -1
8384
for i := opener; i < len(line); i++ {
84-
if bytes.HasPrefix(line[i:], parser.end) {
85+
if depth == 0 && bytes.HasPrefix(line[i:], parser.end) {
8586
succeedingCharacter := byte(0)
8687
if i+len(parser.end) < len(line) {
8788
succeedingCharacter = line[i+len(parser.end)]
@@ -99,6 +100,11 @@ func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.
99100
i++
100101
continue
101102
}
103+
if line[i] == '{' {
104+
depth++
105+
} else if line[i] == '}' {
106+
depth--
107+
}
102108
}
103109
if ender == -1 {
104110
return nil

0 commit comments

Comments
 (0)