mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-30 02:57:45 +01:00
Commit messages now rendered with line breaks and prefix spacing.
This commit is contained in:
parent
2340bb1ed2
commit
7acbcf9ddd
2 changed files with 35 additions and 2 deletions
|
@ -96,9 +96,42 @@ func ToUtf8(content string) string {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Replaces all prefixes 'old' in 's' with 'new'.
|
||||||
|
func ReplaceLeft(s, old, new string) string {
|
||||||
|
old_len, new_len, i, n := len(old), len(new), 0, 0
|
||||||
|
for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 {
|
||||||
|
i += old_len
|
||||||
|
}
|
||||||
|
|
||||||
|
// simple optimization
|
||||||
|
if n == 0 {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocating space for the new string
|
||||||
|
newLen := n*new_len + len(s[i:])
|
||||||
|
replacement := make([]byte, newLen, newLen)
|
||||||
|
|
||||||
|
j := 0
|
||||||
|
for ; j < n*new_len; j += new_len {
|
||||||
|
copy(replacement[j:j+new_len], new)
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(replacement[j:], s[i:])
|
||||||
|
return string(replacement)
|
||||||
|
}
|
||||||
|
|
||||||
// RenderCommitMessage renders commit message with XSS-safe and special links.
|
// RenderCommitMessage renders commit message with XSS-safe and special links.
|
||||||
func RenderCommitMessage(msg, urlPrefix string) template.HTML {
|
func RenderCommitMessage(msg, urlPrefix string) template.HTML {
|
||||||
return template.HTML(string(RenderIssueIndexPattern([]byte(template.HTMLEscapeString(msg)), urlPrefix)))
|
cleanMsg := template.HTMLEscapeString(msg)
|
||||||
|
fullMessage := string(RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix))
|
||||||
|
msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n")
|
||||||
|
for i := range msgLines {
|
||||||
|
msgLines[i] = ReplaceLeft(msgLines[i], " ", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
fullMessage = strings.Join(msgLines, "<br>")
|
||||||
|
return template.HTML(fullMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
var TemplateFuncs template.FuncMap = map[string]interface{}{
|
var TemplateFuncs template.FuncMap = map[string]interface{}{
|
||||||
|
|
|
@ -6,12 +6,12 @@
|
||||||
{{template "repo/commits_table" .}}
|
{{template "repo/commits_table" .}}
|
||||||
{{else}}
|
{{else}}
|
||||||
<h4 class="ui top attached info header">
|
<h4 class="ui top attached info header">
|
||||||
{{RenderCommitMessage .Commit.Message $.RepoLink}}
|
|
||||||
<div class="ui right">
|
<div class="ui right">
|
||||||
<a class="ui blue tiny button" href="{{EscapePound .SourcePath}}">
|
<a class="ui blue tiny button" href="{{EscapePound .SourcePath}}">
|
||||||
{{.i18n.Tr "repo.diff.browse_source"}}
|
{{.i18n.Tr "repo.diff.browse_source"}}
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
{{RenderCommitMessage .Commit.Message $.RepoLink}}
|
||||||
</h4>
|
</h4>
|
||||||
<div class="ui attached info segment">
|
<div class="ui attached info segment">
|
||||||
{{if .Author}}
|
{{if .Author}}
|
||||||
|
|
Loading…
Reference in a new issue