diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go index d229afa8e3..67d81488fd 100644 --- a/modules/markup/markdown/goldmark.go +++ b/modules/markup/markdown/goldmark.go @@ -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 { diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go index e811d29994..2b19e0f1c9 100644 --- a/modules/markup/markdown/markdown.go +++ b/modules/markup/markdown/markdown.go @@ -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) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index f7955115e0..c854861031 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -561,6 +561,14 @@ func TestMathBlock(t *testing.T) { "test $$a$$", `
test a
[
+[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}
+]
a
+[\triangle ABC] = \sqrt{s(s-a)(s-b)(s-c)}
+
` + 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) {
diff --git a/modules/markup/markdown/math/block_parser.go b/modules/markup/markdown/math/block_parser.go
index 527df84975..b0fe1d588a 100644
--- a/modules/markup/markdown/math/block_parser.go
+++ b/modules/markup/markdown/math/block_parser.go
@@ -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
}
diff --git a/modules/markup/markdown/util/text.go b/modules/markup/markdown/util/text.go
index 8a42e5835b..db6e432e79 100644
--- a/modules/markup/markdown/util/text.go
+++ b/modules/markup/markdown/util/text.go
@@ -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()
+)