From b5a1daa756b181f15a1318a53aba09478b988e6a Mon Sep 17 00:00:00 2001 From: Sergey Dryabzhinsky Date: Tue, 29 May 2018 01:42:27 +0300 Subject: [PATCH] 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 `
` - sanitize html after replace --- pkg/template/template.go | 6 ++++++ templates/repo/home.tmpl | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/template/template.go b/pkg/template/template.go index dd92ba61e..f56194b91 100644 --- a/pkg/template/template.go +++ b/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
+func NewLine2br(raw string) template.HTML { + return template.HTML(strings.Replace(raw, "\n", "
", -1)) +} + func List(l *list.List) chan interface{} { e := l.Front() c := make(chan interface{}) diff --git a/templates/repo/home.tmpl b/templates/repo/home.tmpl index ce49e380a..843ec5d00 100644 --- a/templates/repo/home.tmpl +++ b/templates/repo/home.tmpl @@ -5,7 +5,7 @@ {{template "base/alert" .}} {{if .PageIsRepoHome}}

- {{if .Repository.Description}}{{.Repository.Description | Str2html}}{{else}}{{.i18n.Tr "repo.no_desc"}}{{end}} + {{if .Repository.Description}}{{.Repository.Description | NewLine2br | Str2html}}{{else}}{{.i18n.Tr "repo.no_desc"}}{{end}} {{.Repository.Website}}