Explorar o código

template: simple html filter for repository description (#5242)

* Add markdown renderer function to templates, use it for description in repo template

* Fix function call

* Define function Md2html in template namespace

* Change filter from Md2html to nl2br

* Change filter name to NewLine2br as suggested

* Update description output:

- just replace `\n` by `<br>`
- sanitize html after replace
Sergey Dryabzhinsky %!s(int64=6) %!d(string=hai) anos
pai
achega
b5a1daa756
Modificáronse 2 ficheiros con 7 adicións e 1 borrados
  1. 6 0
      pkg/template/template.go
  2. 1 1
      templates/repo/home.tmpl

+ 6 - 0
pkg/template/template.go

@@ -65,6 +65,7 @@ func NewFuncMap() []template.FuncMap {
 		"Safe":             Safe,
 		"Sanitize":         bluemonday.UGCPolicy().Sanitize,
 		"Str2html":         Str2html,
+		"NewLine2br":       NewLine2br,
 		"TimeSince":        tool.TimeSince,
 		"RawTimeSince":     tool.RawTimeSince,
 		"FileSize":         tool.FileSize,
@@ -130,6 +131,11 @@ func Str2html(raw string) template.HTML {
 	return template.HTML(markup.Sanitize(raw))
 }
 
+// Simple filter, converts newline symbols to <br>
+func NewLine2br(raw string) template.HTML {
+	return template.HTML(strings.Replace(raw, "\n", "<br>", -1))
+}
+
 func List(l *list.List) chan interface{} {
 	e := l.Front()
 	c := make(chan interface{})

+ 1 - 1
templates/repo/home.tmpl

@@ -5,7 +5,7 @@
 		{{template "base/alert" .}}
 		{{if .PageIsRepoHome}}
 			<p id="repo-desc">
-				{{if .Repository.Description}}<span class="description has-emoji">{{.Repository.Description | Str2html}}</span>{{else}}<span class="no-description text-italic">{{.i18n.Tr "repo.no_desc"}}</span>{{end}}
+				{{if .Repository.Description}}<span class="description has-emoji">{{.Repository.Description | NewLine2br | Str2html}}</span>{{else}}<span class="no-description text-italic">{{.i18n.Tr "repo.no_desc"}}</span>{{end}}
 				<a class="link" href="{{.Repository.Website}}">{{.Repository.Website}}</a>
 			</p>
 			<div class="ui segment" id="git-stats">