fix: enable multi-line math equations in wiki (#8424)
Some checks are pending
/ release (push) Waiting to run
testing-integration / test-unit (push) Waiting to run
testing-integration / test-sqlite (push) Waiting to run
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-e2e (push) Blocked by required conditions
testing / test-remote-cacher (redis) (push) Blocked by required conditions
testing / test-remote-cacher (valkey) (push) Blocked by required conditions
testing / test-remote-cacher (garnet) (push) Blocked by required conditions
testing / test-remote-cacher (redict) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions

- When math equation support was added into Gitea it allowed for math equations to be typed over multiple lines via `\[ ... \]` and `$$ ... $$`. Specifically the former delimiters caused problems with legitimate markdown input to be seen as a math equation and therefore was disabled in e1a82a15d3.
- Enable this multi-line parsing for wiki as it's less likely to cause issues in the context of the wiki.
- It is hard to fix this issue in a proper way without investing a good amount of time in Goldmark as explained in https://codeberg.org/forgejo/forgejo/issues/6902#issuecomment-2845317
- Added unit test.
- Resolves forgejo/forgejo#6902

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8424
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-07-06 22:00:09 +02:00 committed by Gusted
parent f5cbb9604d
commit da1c0f7f18
5 changed files with 56 additions and 9 deletions

View file

@ -9,6 +9,7 @@ import (
"strings"
"forgejo.org/modules/markup"
markdownutil "forgejo.org/modules/markup/markdown/util"
"forgejo.org/modules/setting"
"github.com/yuin/goldmark/ast"
@ -35,8 +36,8 @@ func (g *ASTTransformer) applyElementDir(n ast.Node) {
func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
firstChild := node.FirstChild()
tocMode := ""
ctx := pc.Get(renderContextKey).(*markup.RenderContext)
rc := pc.Get(renderConfigKey).(*RenderConfig)
ctx := pc.Get(markdownutil.RenderContextKey).(*markup.RenderContext)
rc := pc.Get(markdownutil.RenderConfigKey).(*RenderConfig)
tocList := make([]markup.Header, 0, 20)
if rc.yamlNode != nil {

View file

@ -16,6 +16,7 @@ import (
"forgejo.org/modules/markup/common"
"forgejo.org/modules/markup/markdown/callout"
"forgejo.org/modules/markup/markdown/math"
markdownutil "forgejo.org/modules/markup/markdown/util"
"forgejo.org/modules/setting"
giteautil "forgejo.org/modules/util"
@ -34,11 +35,6 @@ var (
specMarkdownOnce sync.Once
)
var (
renderContextKey = parser.NewContextKey()
renderConfigKey = parser.NewContextKey()
)
type limitWriter struct {
w io.Writer
sum int64
@ -64,7 +60,7 @@ func (l *limitWriter) Write(data []byte) (int, error) {
// newParserContext creates a parser.Context with the render context set
func newParserContext(ctx *markup.RenderContext) parser.Context {
pc := parser.NewContext(parser.WithIDs(newPrefixedIDs()))
pc.Set(renderContextKey, ctx)
pc.Set(markdownutil.RenderContextKey, ctx)
return pc
}
@ -192,7 +188,7 @@ func actualRender(ctx *markup.RenderContext, input io.Reader, output io.Writer)
}
rc.metaLength = metaLength
pc.Set(renderConfigKey, rc)
pc.Set(markdownutil.RenderConfigKey, rc)
if err := converter.Convert(buf, lw, parser.WithContext(pc)); err != nil {
log.Error("Unable to render: %v", err)

View file

@ -561,6 +561,14 @@ func TestMathBlock(t *testing.T) {
"test $$a$$",
`<p>test <code class="language-math display is-loading">a</code></p>` + nl,
},
{
`\[
[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}
\]`,
`<p>[<br/>
[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}<br/>
]</p>` + nl,
},
}
for _, test := range testcases {
@ -568,6 +576,32 @@ func TestMathBlock(t *testing.T) {
require.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
}
t.Run("Wiki context", func(t *testing.T) {
testcases := []struct {
testcase string
expected string
}{
{
"$a$",
`<p><code class="language-math is-loading">a</code></p>` + nl,
},
{
`\[
[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}
\]`,
`<pre class="code-block is-loading"><code class="chroma language-math display">
[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}
</code></pre>` + nl,
},
}
for _, test := range testcases {
res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext, IsWiki: true}, test.testcase)
require.NoError(t, err, "Unexpected error in testcase: %q", test.testcase)
assert.Equal(t, template.HTML(test.expected), res, "Unexpected result in testcase %q", test.testcase)
}
})
}
func TestFootnote(t *testing.T) {

View file

@ -6,6 +6,9 @@ package math
import (
"bytes"
"forgejo.org/modules/markup"
markdownutil "forgejo.org/modules/markup/markdown/util"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
@ -61,6 +64,13 @@ func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Contex
return node, parser.Close | parser.NoChildren
}
ctx := pc.Get(markdownutil.RenderContextKey).(*markup.RenderContext)
if ctx.IsWiki {
reader.Advance(segment.Len() - 1)
segment.Start += 2
node.Lines().Append(segment)
return node, parser.NoChildren
}
return nil, parser.NoChildren
}

View file

@ -7,6 +7,7 @@ import (
"bytes"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
)
func textOfChildren(n ast.Node, src []byte, b *bytes.Buffer) {
@ -24,3 +25,8 @@ func Text(n ast.Node, src []byte) []byte {
textOfChildren(n, src, &b)
return b.Bytes()
}
var (
RenderContextKey = parser.NewContextKey()
RenderConfigKey = parser.NewContextKey()
)