Render commit msg as header + verbatim description
Most commit in Git are expected to follow standard of single header line, followed by description paragraphs, separated by empty line from previous block. Previously Gogs were treating everything as single header. Now we are trying to render only first line as header, but following lines (description chunks) as a verbatim.
This commit is contained in:
parent
b5f6206a65
commit
e2ca53029e
6 changed files with 52 additions and 25 deletions
|
@ -183,15 +183,27 @@ func ReplaceLeft(s, old, new string) string {
|
|||
}
|
||||
|
||||
// RenderCommitMessage renders commit message with XSS-safe and special links.
|
||||
func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML {
|
||||
func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]string) template.HTML {
|
||||
cleanMsg := template.HTMLEscapeString(msg)
|
||||
fullMessage := string(base.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix, metas))
|
||||
msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n")
|
||||
for i := range msgLines {
|
||||
msgLines[i] = ReplaceLeft(msgLines[i], " ", " ")
|
||||
numLines := len(msgLines)
|
||||
if numLines == 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>"))
|
||||
}
|
||||
|
||||
fullMessage = strings.Join(msgLines, "<br>")
|
||||
return template.HTML(fullMessage)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue