mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	Backport #33870 by wxiaoguang Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		@@ -159,6 +159,14 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
 | 
				
			|||||||
		limit: setting.UI.MaxDisplayFileSize * 3,
 | 
							limit: setting.UI.MaxDisplayFileSize * 3,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// FIXME: Don't read all to memory, but goldmark doesn't support
 | 
				
			||||||
 | 
						buf, err := io.ReadAll(input)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Error("Unable to ReadAll: %v", err)
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						buf = giteautil.NormalizeEOL(buf)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// FIXME: should we include a timeout to abort the renderer if it takes too long?
 | 
						// FIXME: should we include a timeout to abort the renderer if it takes too long?
 | 
				
			||||||
	defer func() {
 | 
						defer func() {
 | 
				
			||||||
		err := recover()
 | 
							err := recover()
 | 
				
			||||||
@@ -166,20 +174,12 @@ func render(ctx *markup.RenderContext, input io.Reader, output io.Writer) error
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		log.Warn("Unable to render markdown due to panic in goldmark: %v", err)
 | 
					 | 
				
			||||||
		if (!setting.IsProd && !setting.IsInTesting) || log.IsDebug() {
 | 
					 | 
				
			||||||
		log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
 | 
							log.Error("Panic in markdown: %v\n%s", err, log.Stack(2))
 | 
				
			||||||
		}
 | 
							escapedHTML := template.HTMLEscapeString(giteautil.UnsafeBytesToString(buf))
 | 
				
			||||||
 | 
							_, _ = output.Write(giteautil.UnsafeStringToBytes(escapedHTML))
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// FIXME: Don't read all to memory, but goldmark doesn't support
 | 
					 | 
				
			||||||
	pc := newParserContext(ctx)
 | 
						pc := newParserContext(ctx)
 | 
				
			||||||
	buf, err := io.ReadAll(input)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		log.Error("Unable to ReadAll: %v", err)
 | 
					 | 
				
			||||||
		return err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	buf = giteautil.NormalizeEOL(buf)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Preserve original length.
 | 
						// Preserve original length.
 | 
				
			||||||
	bufWithMetadataLength := len(buf)
 | 
						bufWithMetadataLength := len(buf)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -23,6 +23,11 @@ func TestAttention(t *testing.T) {
 | 
				
			|||||||
	defer svg.MockIcon("octicon-alert")()
 | 
						defer svg.MockIcon("octicon-alert")()
 | 
				
			||||||
	defer svg.MockIcon("octicon-stop")()
 | 
						defer svg.MockIcon("octicon-stop")()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						test := func(input, expected string) {
 | 
				
			||||||
 | 
							result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
 | 
				
			||||||
 | 
							assert.NoError(t, err)
 | 
				
			||||||
 | 
							assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	renderAttention := func(attention, icon string) string {
 | 
						renderAttention := func(attention, icon string) string {
 | 
				
			||||||
		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>`
 | 
							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>`
 | 
				
			||||||
		tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
 | 
							tmpl = strings.ReplaceAll(tmpl, "{attention}", attention)
 | 
				
			||||||
@@ -31,12 +36,6 @@ func TestAttention(t *testing.T) {
 | 
				
			|||||||
		return tmpl
 | 
							return tmpl
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	test := func(input, expected string) {
 | 
					 | 
				
			||||||
		result, err := markdown.RenderString(markup.NewTestRenderContext(), input)
 | 
					 | 
				
			||||||
		assert.NoError(t, err)
 | 
					 | 
				
			||||||
		assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(result)))
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	test(`
 | 
						test(`
 | 
				
			||||||
> [!NOTE]
 | 
					> [!NOTE]
 | 
				
			||||||
> text
 | 
					> text
 | 
				
			||||||
@@ -53,4 +52,7 @@ func TestAttention(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// legacy GitHub style
 | 
						// legacy GitHub style
 | 
				
			||||||
	test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
 | 
						test(`> **warning**`, renderAttention("warning", "octicon-alert")+"\n</blockquote>")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// edge case (it used to cause panic)
 | 
				
			||||||
 | 
						test(">\ntext", "<blockquote>\n</blockquote>\n<p>text</p>")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -115,6 +115,9 @@ func (g *ASTTransformer) transformBlockquote(v *ast.Blockquote, reader text.Read
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// grab these nodes and make sure we adhere to the attention blockquote structure
 | 
						// grab these nodes and make sure we adhere to the attention blockquote structure
 | 
				
			||||||
	firstParagraph := v.FirstChild()
 | 
						firstParagraph := v.FirstChild()
 | 
				
			||||||
 | 
						if firstParagraph == nil {
 | 
				
			||||||
 | 
							return ast.WalkContinue, nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	g.applyElementDir(firstParagraph)
 | 
						g.applyElementDir(firstParagraph)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	attentionType, processedNodes := g.extractBlockquoteAttentionEmphasis(firstParagraph, reader)
 | 
						attentionType, processedNodes := g.extractBlockquoteAttentionEmphasis(firstParagraph, reader)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +0,0 @@
 | 
				
			|||||||
// Copyright 2017 The Gitea Authors. All rights reserved.
 | 
					 | 
				
			||||||
// SPDX-License-Identifier: MIT
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
package markup_test
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user