diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-12-04 09:39:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-04 01:39:33 +0000 |
commit | 2f43536c3eb6ba20e43521f5539df8372f225652 (patch) | |
tree | b6ca983aaa38d872c22ff2d51897ecf980d54181 /modules | |
parent | 171edfc79373fe0e80e064748c0cec8cc93aea2e (diff) | |
download | gitea-2f43536c3eb6ba20e43521f5539df8372f225652.tar.gz gitea-2f43536c3eb6ba20e43521f5539df8372f225652.zip |
Fix issue title rendering and refactor legacy function names (#32703)
Fix #32700, regression of recent markup refactoring
And by the way, clarify many legacy problems:
1. Some "RenderXxx" functions do not really "render", they only call "post processors"
2. Merge "RenderEmoji | RenderCodeBlock", they are all for "simple issue title"
Diffstat (limited to 'modules')
-rw-r--r-- | modules/markup/html.go | 45 | ||||
-rw-r--r-- | modules/markup/html_internal_test.go | 4 | ||||
-rw-r--r-- | modules/markup/render.go | 4 | ||||
-rw-r--r-- | modules/templates/util_render.go | 27 | ||||
-rw-r--r-- | modules/templates/util_render_test.go | 4 |
5 files changed, 50 insertions, 34 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index 04b768bb8e..25628db39e 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -159,9 +159,9 @@ func PostProcessDefault(ctx *RenderContext, input io.Reader, output io.Writer) e return postProcess(ctx, procs, input, output) } -// RenderCommitMessage will use the same logic as PostProcess, but will disable +// PostProcessCommitMessage will use the same logic as PostProcess, but will disable // the shortLinkProcessor. -func RenderCommitMessage(ctx *RenderContext, content string) (string, error) { +func PostProcessCommitMessage(ctx *RenderContext, content string) (string, error) { procs := []processor{ fullIssuePatternProcessor, comparePatternProcessor, @@ -183,11 +183,11 @@ var emojiProcessors = []processor{ emojiProcessor, } -// RenderCommitMessageSubject will use the same logic as PostProcess and -// RenderCommitMessage, but will disable the shortLinkProcessor and +// PostProcessCommitMessageSubject will use the same logic as PostProcess and +// PostProcessCommitMessage, but will disable the shortLinkProcessor and // emailAddressProcessor, will add a defaultLinkProcessor if defaultLink is set, // which changes every text node into a link to the passed default link. -func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string) (string, error) { +func PostProcessCommitMessageSubject(ctx *RenderContext, defaultLink, content string) (string, error) { procs := []processor{ fullIssuePatternProcessor, comparePatternProcessor, @@ -211,26 +211,20 @@ func RenderCommitMessageSubject(ctx *RenderContext, defaultLink, content string) return postProcessString(ctx, procs, content) } -// RenderIssueTitle to process title on individual issue/pull page -func RenderIssueTitle(ctx *RenderContext, title string) (string, error) { - // do not render other issue/commit links in an issue's title - which in most cases is already a link. +// PostProcessIssueTitle to process title on individual issue/pull page +func PostProcessIssueTitle(ctx *RenderContext, title string) (string, error) { return postProcessString(ctx, []processor{ + issueIndexPatternProcessor, + commitCrossReferencePatternProcessor, + hashCurrentPatternProcessor, emojiShortCodeProcessor, emojiProcessor, }, title) } -func postProcessString(ctx *RenderContext, procs []processor, content string) (string, error) { - var buf strings.Builder - if err := postProcess(ctx, procs, strings.NewReader(content), &buf); err != nil { - return "", err - } - return buf.String(), nil -} - -// RenderDescriptionHTML will use similar logic as PostProcess, but will +// PostProcessDescriptionHTML will use similar logic as PostProcess, but will // use a single special linkProcessor. -func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) { +func PostProcessDescriptionHTML(ctx *RenderContext, content string) (string, error) { return postProcessString(ctx, []processor{ descriptionLinkProcessor, emojiShortCodeProcessor, @@ -238,13 +232,24 @@ func RenderDescriptionHTML(ctx *RenderContext, content string) (string, error) { }, content) } -// RenderEmoji for when we want to just process emoji and shortcodes +// PostProcessEmoji for when we want to just process emoji and shortcodes // in various places it isn't already run through the normal markdown processor -func RenderEmoji(ctx *RenderContext, content string) (string, error) { +func PostProcessEmoji(ctx *RenderContext, content string) (string, error) { return postProcessString(ctx, emojiProcessors, content) } +func postProcessString(ctx *RenderContext, procs []processor, content string) (string, error) { + var buf strings.Builder + if err := postProcess(ctx, procs, strings.NewReader(content), &buf); err != nil { + return "", err + } + return buf.String(), nil +} + func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output io.Writer) error { + if !ctx.usedByRender && ctx.RenderHelper != nil { + defer ctx.RenderHelper.CleanUp() + } // FIXME: don't read all content to memory rawHTML, err := io.ReadAll(input) if err != nil { diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 651e674108..9419350e61 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -252,7 +252,7 @@ func TestRender_IssueIndexPattern_NoShortPattern(t *testing.T) { testRenderIssueIndexPattern(t, "!1", "!1", NewTestRenderContext(metas)) } -func TestRender_RenderIssueTitle(t *testing.T) { +func TestRender_PostProcessIssueTitle(t *testing.T) { setting.AppURL = TestAppURL metas := map[string]string{ "format": "https://someurl.com/{user}/{repo}/{index}", @@ -260,7 +260,7 @@ func TestRender_RenderIssueTitle(t *testing.T) { "repo": "someRepo", "style": IssueNameStyleNumeric, } - actual, err := RenderIssueTitle(NewTestRenderContext(metas), "#1") + actual, err := PostProcessIssueTitle(NewTestRenderContext(metas), "#1") assert.NoError(t, err) assert.Equal(t, "#1", actual) } diff --git a/modules/markup/render.go b/modules/markup/render.go index 3b112b1a14..b239e59687 100644 --- a/modules/markup/render.go +++ b/modules/markup/render.go @@ -57,6 +57,9 @@ type RenderOptions struct { type RenderContext struct { ctx context.Context + // the context might be used by the "render" function, but it might also be used by "postProcess" function + usedByRender bool + SidebarTocNode ast.Node RenderHelper RenderHelper @@ -182,6 +185,7 @@ func pipes() (io.ReadCloser, io.WriteCloser, func()) { } func render(ctx *RenderContext, renderer Renderer, input io.Reader, output io.Writer) error { + ctx.usedByRender = true if ctx.RenderHelper != nil { defer ctx.RenderHelper.CleanUp() } diff --git a/modules/templates/util_render.go b/modules/templates/util_render.go index 3237de5ecb..1800747f48 100644 --- a/modules/templates/util_render.go +++ b/modules/templates/util_render.go @@ -38,9 +38,9 @@ func (ut *RenderUtils) RenderCommitMessage(msg string, metas map[string]string) cleanMsg := template.HTMLEscapeString(msg) // we can safely assume that it will not return any error, since there // shouldn't be any special HTML. - fullMessage, err := markup.RenderCommitMessage(markup.NewRenderContext(ut.ctx).WithMetas(metas), cleanMsg) + fullMessage, err := markup.PostProcessCommitMessage(markup.NewRenderContext(ut.ctx).WithMetas(metas), cleanMsg) if err != nil { - log.Error("RenderCommitMessage: %v", err) + log.Error("PostProcessCommitMessage: %v", err) return "" } msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n") @@ -65,9 +65,9 @@ func (ut *RenderUtils) RenderCommitMessageLinkSubject(msg, urlDefault string, me // we can safely assume that it will not return any error, since there // shouldn't be any special HTML. - renderedMessage, err := markup.RenderCommitMessageSubject(markup.NewRenderContext(ut.ctx).WithMetas(metas), urlDefault, template.HTMLEscapeString(msgLine)) + renderedMessage, err := markup.PostProcessCommitMessageSubject(markup.NewRenderContext(ut.ctx).WithMetas(metas), urlDefault, template.HTMLEscapeString(msgLine)) if err != nil { - log.Error("RenderCommitMessageSubject: %v", err) + log.Error("PostProcessCommitMessageSubject: %v", err) return "" } return renderCodeBlock(template.HTML(renderedMessage)) @@ -87,9 +87,9 @@ func (ut *RenderUtils) RenderCommitBody(msg string, metas map[string]string) tem return "" } - renderedMessage, err := markup.RenderCommitMessage(markup.NewRenderContext(ut.ctx).WithMetas(metas), template.HTMLEscapeString(msgLine)) + renderedMessage, err := markup.PostProcessCommitMessage(markup.NewRenderContext(ut.ctx).WithMetas(metas), template.HTMLEscapeString(msgLine)) if err != nil { - log.Error("RenderCommitMessage: %v", err) + log.Error("PostProcessCommitMessage: %v", err) return "" } return template.HTML(renderedMessage) @@ -106,12 +106,19 @@ func renderCodeBlock(htmlEscapedTextToRender template.HTML) template.HTML { // RenderIssueTitle renders issue/pull title with defined post processors func (ut *RenderUtils) RenderIssueTitle(text string, metas map[string]string) template.HTML { - renderedText, err := markup.RenderIssueTitle(markup.NewRenderContext(ut.ctx).WithMetas(metas), template.HTMLEscapeString(text)) + renderedText, err := markup.PostProcessIssueTitle(markup.NewRenderContext(ut.ctx).WithMetas(metas), template.HTMLEscapeString(text)) if err != nil { - log.Error("RenderIssueTitle: %v", err) + log.Error("PostProcessIssueTitle: %v", err) return "" } - return template.HTML(renderedText) + return renderCodeBlock(template.HTML(renderedText)) +} + +// RenderIssueSimpleTitle only renders with emoji and inline code block +func (ut *RenderUtils) RenderIssueSimpleTitle(text string) template.HTML { + ret := ut.RenderEmoji(text) + ret = renderCodeBlock(ret) + return ret } // RenderLabel renders a label @@ -174,7 +181,7 @@ func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML { // RenderEmoji renders html text with emoji post processors func (ut *RenderUtils) RenderEmoji(text string) template.HTML { - renderedText, err := markup.RenderEmoji(markup.NewRenderContext(ut.ctx), template.HTMLEscapeString(text)) + renderedText, err := markup.PostProcessEmoji(markup.NewRenderContext(ut.ctx), template.HTMLEscapeString(text)) if err != nil { log.Error("RenderEmoji: %v", err) return "" diff --git a/modules/templates/util_render_test.go b/modules/templates/util_render_test.go index c0241c0a8c..80094ab26e 100644 --- a/modules/templates/util_render_test.go +++ b/modules/templates/util_render_test.go @@ -164,11 +164,11 @@ com 88fc37a3c0a4dda553bdcfc80c178a58247f42fb mit <span class="emoji" aria-label="thumbs up">👍</span> mail@domain.com @mention-user test -#123 +<a href="/user13/repo11/issues/123" class="ref-issue">#123</a> space<SPACE><SPACE> ` expected = strings.ReplaceAll(expected, "<SPACE>", " ") - assert.EqualValues(t, expected, string(newTestRenderUtils().RenderIssueTitle(testInput(), nil))) + assert.EqualValues(t, expected, string(newTestRenderUtils().RenderIssueTitle(testInput(), testMetas))) } func TestRenderMarkdownToHtml(t *testing.T) { |