diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2017-11-12 17:35:55 -0800 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-11-13 09:35:55 +0800 |
commit | 5481be0ac5e137a1127ca37975a6d319bf0ad940 (patch) | |
tree | 44e39f02682181b7cf4d593f3b5d18e2b4d857a8 /modules/templates | |
parent | 47f40ccd5ee1b54019cec6b5d3bce6b8075a1384 (diff) | |
download | gitea-5481be0ac5e137a1127ca37975a6d319bf0ad940.tar.gz gitea-5481be0ac5e137a1127ca37975a6d319bf0ad940.zip |
Fix issue link rendering in commit messages (#2897)
* Fix issue link rendering in commit messages
* Update page.tmpl
* No links for parens
* remove comment
Diffstat (limited to 'modules/templates')
-rw-r--r-- | modules/templates/helper.go | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 34881b788a..454bd648a3 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -110,7 +110,8 @@ func NewFuncMap() []template.FuncMap { "EscapePound": func(str string) string { return strings.NewReplacer("%", "%25", "#", "%23", " ", "%20", "?", "%3F").Replace(str) }, - "RenderCommitMessage": RenderCommitMessage, + "RenderCommitMessage": RenderCommitMessage, + "RenderCommitMessageLink": RenderCommitMessageLink, "ThemeColorMetaTag": func() string { return setting.UI.ThemeColorMetaTag }, @@ -252,28 +253,31 @@ func ReplaceLeft(s, old, new string) string { } // RenderCommitMessage renders commit message with XSS-safe and special links. -func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]string) template.HTML { +func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML { + return renderCommitMessage(msg, markup.RenderIssueIndexPatternOptions{ + URLPrefix: urlPrefix, + Metas: metas, + }) +} + +// RenderCommitMessageLink renders commit message as a XXS-safe link to the provided +// default url, handling for special links. +func RenderCommitMessageLink(msg, urlPrefix string, urlDefault string, metas map[string]string) template.HTML { + return renderCommitMessage(msg, markup.RenderIssueIndexPatternOptions{ + DefaultURL: urlDefault, + URLPrefix: urlPrefix, + Metas: metas, + }) +} + +func renderCommitMessage(msg string, opts markup.RenderIssueIndexPatternOptions) template.HTML { cleanMsg := template.HTMLEscapeString(msg) - fullMessage := string(markup.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix, metas)) + fullMessage := string(markup.RenderIssueIndexPattern([]byte(cleanMsg), opts)) msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n") - numLines := len(msgLines) - if numLines == 0 { + if len(msgLines) == 0 { return template.HTML("") - } else if !full { - return template.HTML(msgLines[0]) - } else if numLines == 1 || (numLines >= 2 && len(msgLines[1]) == 0) { - // First line is a header, standalone or followed by empty line - header := fmt.Sprintf("<h3>%s</h3>", msgLines[0]) - if numLines >= 2 { - fullMessage = header + fmt.Sprintf("\n<pre>%s</pre>", strings.Join(msgLines[2:], "\n")) - } else { - fullMessage = header - } - } else { - // Non-standard git message, there is no header line - fullMessage = fmt.Sprintf("<h4>%s</h4>", strings.Join(msgLines, "<br>")) } - return template.HTML(fullMessage) + return template.HTML(msgLines[0]) } // Actioner describes an action |