Skip to content

Commit 50d572b

Browse files
committed
re-enable testcases
Signed-off-by: Andrew Thornton <[email protected]>
1 parent 840d938 commit 50d572b

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

modules/markup/markdown/markdown_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package markdown_test
66

77
import (
88
"context"
9+
"os"
910
"strings"
1011
"testing"
1112

@@ -37,6 +38,7 @@ func TestMain(m *testing.M) {
3738
if err := git.InitSimple(context.Background()); err != nil {
3839
log.Fatal("git init failed, err: %v", err)
3940
}
41+
os.Exit(m.Run())
4042
}
4143

4244
func TestRender_StandardLinks(t *testing.T) {
@@ -426,3 +428,51 @@ func TestRenderEmojiInLinks_Issue12331(t *testing.T) {
426428
assert.NoError(t, err)
427429
assert.Equal(t, expected, res)
428430
}
431+
432+
func TestMathBlock(t *testing.T) {
433+
const nl = "\n"
434+
testcases := []struct {
435+
testcase string
436+
expected string
437+
}{
438+
{
439+
"$a$",
440+
`<p><code class="language-math is-loading">a</code></p>` + nl,
441+
},
442+
{
443+
"$ a $",
444+
`<p><code class="language-math is-loading">a</code></p>` + nl,
445+
},
446+
{
447+
"$a$ $b$",
448+
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
449+
},
450+
{
451+
`\(a\) \(b\)`,
452+
`<p><code class="language-math is-loading">a</code> <code class="language-math is-loading">b</code></p>` + nl,
453+
},
454+
{
455+
`$a a$b b$`,
456+
`<p><code class="language-math is-loading">a a$b b</code></p>` + nl,
457+
},
458+
{
459+
`a a$b b`,
460+
`<p>a a$b b</p>` + nl,
461+
},
462+
{
463+
`a$b $a a$b b$`,
464+
`<p>a$b <code class="language-math is-loading">a a$b b</code></p>` + nl,
465+
},
466+
{
467+
"$$a$$",
468+
`<pre class="code-block is-loading"><code class="chroma language-math display">a</code></pre>` + nl,
469+
},
470+
}
471+
472+
for _, test := range testcases {
473+
res, err := RenderString(&markup.RenderContext{}, test.testcase)
474+
assert.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
475+
assert.Equal(t, test.expected, res, "Unexpected result in testcase %q", test.testcase)
476+
477+
}
478+
}

modules/markup/markdown/math/inline_parser.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func NewInlineBracketParser() parser.InlineParser {
3737
return defaultInlineBracketParser
3838
}
3939

40-
// Trigger triggers this parser on $
40+
// Trigger returns nil
4141
func (parser *inlineParser) Trigger() []byte {
4242
return parser.start[0:1]
4343
}
@@ -50,30 +50,42 @@ func isAlphanumeric(b byte) bool {
5050
// Parse parses the current line and returns a result of parsing.
5151
func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
5252
line, _ := block.PeekLine()
53-
opener := bytes.Index(line, parser.start)
54-
if opener < 0 {
53+
54+
if !bytes.HasPrefix(line, parser.start) {
55+
// We'll catch this one on the next time round
5556
return nil
5657
}
57-
if opener != 0 && isAlphanumeric(line[opener-1]) {
58+
59+
precedingCharacter := block.PrecendingCharacter()
60+
if precedingCharacter < 256 && isAlphanumeric(byte(precedingCharacter)) {
61+
// need to exclude things like `a$` from being considered a start
5862
return nil
5963
}
6064

61-
opener += len(parser.start)
65+
// move the opener marker point at the start of the text
66+
opener := len(parser.start)
67+
68+
// Now look for an ending line
6269
ender := opener
63-
for pos := opener; pos < len(line); {
64-
ender = bytes.Index(line[pos:], parser.end)
65-
if ender < 0 {
70+
for {
71+
pos := bytes.Index(line[ender:], parser.end)
72+
if pos < 0 {
6673
return nil
6774
}
6875

6976
ender += pos
70-
pos += len(parser.end)
77+
78+
// Now we want to check the character at the end of our parser section
79+
// that is ender + len(parser.end)
80+
pos = ender + len(parser.end)
7181
if len(line) <= pos {
7282
break
7383
}
7484
if !isAlphanumeric(line[pos]) {
7585
break
7686
}
87+
// move the pointer onwards
88+
ender += len(parser.end)
7789
}
7890

7991
block.Advance(opener)

0 commit comments

Comments
 (0)