@@ -40,15 +40,15 @@ func NewRenderUtils(ctx reqctx.RequestContext) *RenderUtils {
4040
4141// RenderCommitMessage renders commit message with XSS-safe and special links.
4242func (ut * RenderUtils ) RenderCommitMessage (msg string , repo * repo.Repository ) template.HTML {
43- cleanMsg := template .HTMLEscapeString (msg )
43+ cleanMsg := template .HTML ( template . HTMLEscapeString (msg ) )
4444 // we can safely assume that it will not return any error, since there shouldn't be any special HTML.
4545 // "repo" can be nil when rendering commit messages for deleted repositories in a user's dashboard feed.
4646 fullMessage , err := markup .PostProcessCommitMessage (renderhelper .NewRenderContextRepoComment (ut .ctx , repo ), cleanMsg )
4747 if err != nil {
4848 log .Error ("PostProcessCommitMessage: %v" , err )
4949 return ""
5050 }
51- msgLines := strings .Split (strings .TrimSpace (fullMessage ), "\n " )
51+ msgLines := strings .Split (strings .TrimSpace (string ( fullMessage ) ), "\n " )
5252 if len (msgLines ) == 0 {
5353 return ""
5454 }
@@ -91,12 +91,14 @@ func (ut *RenderUtils) RenderCommitBody(msg string, repo *repo.Repository) templ
9191 return ""
9292 }
9393
94- renderedMessage , err := markup .PostProcessCommitMessage (renderhelper .NewRenderContextRepoComment (ut .ctx , repo ), template .HTMLEscapeString (msgLine ))
94+ rctx := renderhelper .NewRenderContextRepoComment (ut .ctx , repo )
95+ htmlContent := template .HTML (template .HTMLEscapeString (msgLine ))
96+ renderedMessage , err := markup .PostProcessCommitMessage (rctx , htmlContent )
9597 if err != nil {
9698 log .Error ("PostProcessCommitMessage: %v" , err )
9799 return ""
98100 }
99- return template . HTML ( renderedMessage )
101+ return renderedMessage
100102}
101103
102104// Match text that is between back ticks.
@@ -279,6 +281,26 @@ func (ut *RenderUtils) RenderThemeItem(info *webtheme.ThemeMetaInfo, iconSize in
279281 return htmlutil .HTMLFormat (`<div class="theme-menu-item" data-tooltip-content="%s">%s %s %s</div>` , info .GetDescription (), icon , info .DisplayName , extraIcon )
280282}
281283
284+ func (ut * RenderUtils ) RenderFlashMessage (typ , msg string ) template.HTML {
285+ msg = strings .TrimSpace (msg )
286+ if msg == "" {
287+ return ""
288+ }
289+
290+ var msgContent template.HTML
291+ if strings .Contains (msg , "</pre>" ) || strings .Contains (msg , "</details>" ) || strings .Contains (msg , "</ul>" ) || strings .Contains (msg , "</div>" ) {
292+ // If the message contains some known "block" elements, no need to do more alignment or line-break processing, just sanitize it directly.
293+ msgContent = sanitizeHTML (msg )
294+ } else if ! strings .Contains (msg , "\n " ) {
295+ // If the message is a single line, center-align it by wrapping it
296+ msgContent = htmlutil .HTMLFormat (`<div class="tw-text-center">%s</div>` , sanitizeHTML (msg ))
297+ } else {
298+ // For a multi-line message, preserve line breaks, and left-align it.
299+ msgContent = htmlutil .HTMLFormat (`%s` , sanitizeHTML (strings .ReplaceAll (msg , "\n " , "<br>" )))
300+ }
301+ return htmlutil .HTMLFormat (`<div class="ui %s message flash-message flash-%s">%s</div>` , typ , typ , msgContent )
302+ }
303+
282304func (ut * RenderUtils ) RenderUnicodeEscapeToggleButton (escapeStatus * charset.EscapeStatus ) template.HTML {
283305 if escapeStatus == nil || ! escapeStatus .Escaped {
284306 return ""
0 commit comments