// Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package markdown import ( "strings" "testing" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" "github.com/stretchr/testify/assert" ) const nl = "\n" func TestMathRender(t *testing.T) { setting.Markdown.MathCodeBlockOptions = setting.MarkdownMathCodeBlockOptions{ParseInlineDollar: true, ParseInlineParentheses: true} testcases := []struct { testcase string expected string }{ { "$a$", `

a

` + nl, }, { "$ a $", `

a

` + nl, }, { "$a$ $b$", `

a b

` + nl, }, { `\(a\) \(b\)`, `

a b

` + nl, }, { `$a$.`, `

a.

` + nl, }, { `.$a$`, `

.$a$

` + nl, }, { `$a a$b b$`, `

$a a$b b$

` + nl, }, { `a a$b b`, `

a a$b b

` + nl, }, { `a$b $a a$b b$`, `

a$b $a a$b b$

` + nl, }, { "a$x$", `

a$x$

` + nl, }, { "$x$a", `

$x$a

` + nl, }, { "$a$ ($b$) [$c$] {$d$}", `

a (b) [$c$] {$d$}

` + nl, }, { "$$a$$", `

a

` + nl, }, { "$$a$$ test", `

a test

` + nl, }, { "test $$a$$", `

test a

` + nl, }, { `foo $x=\$$ bar`, `

foo x=\$ bar

` + nl, }, { `$\text{$b$}$`, `

\text{$b$}

` + nl, }, { "a$`b`$c", `

abc

` + nl, }, { "a $`b`$ c", `

a b c

` + nl, }, { "a$``b``$c x$```y```$z", `

abc xyz

` + nl, }, } for _, test := range testcases { t.Run(test.testcase, func(t *testing.T) { res, err := RenderString(markup.NewTestRenderContext(), test.testcase) assert.NoError(t, err) assert.Equal(t, test.expected, string(res)) }) } } func TestMathRenderBlockIndent(t *testing.T) { setting.Markdown.MathCodeBlockOptions = setting.MarkdownMathCodeBlockOptions{ParseBlockDollar: true, ParseBlockSquareBrackets: true} testcases := []struct { name string testcase string expected string }{ { "indent-0", ` \[ \alpha \] `, `

\alpha
`, }, { "indent-1", ` \[ \alpha \] `, `

\alpha
`, }, { "indent-2-mismatch", ` \[ a b c d \] `, `

a
b
c
 d
`, }, { "indent-2", ` \[ a b c \] `, `

a
 b
c
`, }, { "indent-0-oneline", `$$ x $$ foo`, ` x

foo

`, }, { "indent-3-oneline", ` $$ x $$ foo`, ` x

foo

`, }, { "quote-block", ` > \[ > a > \] > \[ > b > \] `, `

a

b
`, }, { "list-block", ` 1. a \[ x \] 2. b`, `
  1. a
    
    x
    
  2. b
`, }, { "inline-non-math", `\[x]`, `

[x]

` + nl, }, } for _, test := range testcases { t.Run(test.name, func(t *testing.T) { res, err := RenderString(markup.NewTestRenderContext(), strings.ReplaceAll(test.testcase, "", " ")) assert.NoError(t, err) assert.Equal(t, test.expected, string(res), "unexpected result for test case:\n%s", test.testcase) }) } } func TestMathRenderOptions(t *testing.T) { setting.Markdown.MathCodeBlockOptions = setting.MarkdownMathCodeBlockOptions{} defer test.MockVariableValue(&setting.Markdown.MathCodeBlockOptions) test := func(t *testing.T, expected, input string) { res, err := RenderString(markup.NewTestRenderContext(), input) assert.NoError(t, err) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(res)), "input: %s", input) } // default (non-conflict) inline syntax test(t, `

a

`, "$`a`$") // ParseInlineDollar test(t, `

$a$

`, `$a$`) setting.Markdown.MathCodeBlockOptions.ParseInlineDollar = true test(t, `

a

`, `$a$`) // ParseInlineParentheses test(t, `

(a)

`, `\(a\)`) setting.Markdown.MathCodeBlockOptions.ParseInlineParentheses = true test(t, `

a

`, `\(a\)`) // ParseBlockDollar test(t, `

$$ a $$

`, ` $$ a $$ `) setting.Markdown.MathCodeBlockOptions.ParseBlockDollar = true test(t, `

a
`, ` $$ a $$ `) // ParseBlockSquareBrackets test(t, `

[ a ]

`, ` \[ a \] `) setting.Markdown.MathCodeBlockOptions.ParseBlockSquareBrackets = true test(t, `

a
`, ` \[ a \] `) }