Skip to content

Commit 9054a66

Browse files
GiteaBotwxiaoguang
andauthored
Fix markdown render (#33870) (#33875)
Backport #33870 by wxiaoguang Co-authored-by: wxiaoguang <[email protected]>
1 parent fc82204 commit 9054a66

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

modules/markup/markdown/markdown.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,27 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
159159
limit: setting.UI.MaxDisplayFileSize * 3,
160160
}
161161

162+
// FIXME: Don't read all to memory, but goldmark doesn't support
163+
buf, err := io.ReadAll(input)
164+
if err != nil {
165+
log.Error("Unable to ReadAll: %v", err)
166+
return err
167+
}
168+
buf = giteautil.NormalizeEOL(buf)
169+
162170
// FIXME: should we include a timeout to abort the renderer if it takes too long?
163171
defer func() {
164172
err := recover()
165173
if err == nil {
166174
return
167175
}
168176

169-
log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
170-
if (!setting.IsProd && !setting.IsInTesting) || log.IsDebug() {
171-
log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
172-
}
177+
log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
178+
escapedHTML := template.HTMLEscapeString(giteautil.UnsafeBytesToString(buf))
179+
_, _ = output.Write(giteautil.UnsafeStringToBytes(escapedHTML))
173180
}()
174181

175-
// FIXME: Don't read all to memory, but goldmark doesn't support
176182
pc := newParserContext(ctx)
177-
buf, err := io.ReadAll(input)
178-
if err != nil {
179-
log.Error("Unable to ReadAll: %v", err)
180-
return err
181-
}
182-
buf = giteautil.NormalizeEOL(buf)
183183

184184
// Preserve original length.
185185
bufWithMetadataLength := len(buf)

modules/markup/markdown/markdown_attention_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ func TestAttention(t *testing.T) {
2323
defer svg.MockIcon("octicon-alert")()
2424
defer svg.MockIcon("octicon-stop")()
2525

26+
test := func(input, expected string) {
27+
result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
28+
assert.NoError(t, err)
29+
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
30+
}
2631
renderAttention := func(attention, icon string) string {
2732
tmpl := `<blockquote class="attention-header attention-{attention}"><p><svg class="attention-icon attention-{attention} svg {icon}" width="16" height="16"></svg><strong class="attention-{attention}">{Attention}</strong></p>`
2833
tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
@@ -31,12 +36,6 @@ func TestAttention(t *testing.T) {
3136
return tmpl
3237
}
3338

34-
test := func(input, expected string) {
35-
result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
36-
assert.NoError(t, err)
37-
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
38-
}
39-
4039
test(`
4140
> [!NOTE]
4241
> text
@@ -53,4 +52,7 @@ func TestAttention(t *testing.T) {
5352

5453
// legacy GitHub style
5554
test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
55+
56+
// edge case (it used to cause panic)
57+
test(">\ntext", "<blockquote>\n</blockquote>\n<p>text</p>")
5658
}

modules/markup/markdown/transform_blockquote.go

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func (g *ASTTransformer) transformBlockquote(v *ast.Blockquote, reader text.Read
115115

116116
// grab these nodes and make sure we adhere to the attention blockquote structure
117117
firstParagraph := v.FirstChild()
118+
if firstParagraph == nil {
119+
return ast.WalkContinue, nil
120+
}
118121
g.applyElementDir(firstParagraph)
119122

120123
attentionType, processedNodes := g.extractBlockquoteAttentionEmphasis(firstParagraph, reader)

modules/markup/renderer_test.go

-4
This file was deleted.

0 commit comments

Comments
 (0)