From 3c018c498800a0265a2b6b4d7a5152dc472be6b5 Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Tue, 9 May 2017 17:28:29 +1000 Subject: [PATCH] Move markdown stuff to it's own function --- model/torrent.go | 10 ++-------- util/markdown.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 util/markdown.go diff --git a/model/torrent.go b/model/torrent.go index 9087f583..1ed3dd95 100644 --- a/model/torrent.go +++ b/model/torrent.go @@ -3,8 +3,6 @@ package model import ( "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/util" - "github.com/microcosm-cc/bluemonday" - "github.com/russross/blackfriday" "html" "html/template" @@ -81,13 +79,9 @@ func (t *Torrents) ToJson() TorrentsJson { commentsJson = append(commentsJson, CommentsJson{Username: c.Username, Content: template.HTML(c.Content), Date: c.Date}) } for _, c := range t.Comments { - unsafe := blackfriday.MarkdownCommon([]byte(c.Content)) - html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) - commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: template.HTML(html), Date: c.CreatedAt}) + commentsJson = append(commentsJson, CommentsJson{Username: c.User.Username, Content: util.MarkdownToHTML(c.Content), Date: c.CreatedAt}) } - unsafe := blackfriday.MarkdownCommon([]byte(t.Description)) - description := bluemonday.UGCPolicy().SanitizeBytes(unsafe) res := TorrentsJson{ Id: strconv.FormatUint(uint64(t.Id), 10), Name: html.UnescapeString(t.Name), @@ -95,7 +89,7 @@ func (t *Torrents) ToJson() TorrentsJson { Hash: t.Hash, Date: t.Date.Format(time.RFC3339), Filesize: util.FormatFilesize2(t.Filesize), - Description: template.HTML(description), + Description: util.MarkdownToHTML(t.Description), Comments: commentsJson, Sub_Category: strconv.Itoa(t.Sub_Category), Category: strconv.Itoa(t.Category), diff --git a/util/markdown.go b/util/markdown.go new file mode 100644 index 00000000..4b1c02b6 --- /dev/null +++ b/util/markdown.go @@ -0,0 +1,15 @@ +package util + +import ( + "github.com/microcosm-cc/bluemonday" + "github.com/russross/blackfriday" + + "html/template" +) + +// TODO restrict certain types of markdown +func MarkdownToHTML(markdown string) template.HTML { + unsafe := blackfriday.MarkdownCommon([]byte(markdown)) + html := bluemonday.UGCPolicy().SanitizeBytes(unsafe) + return template.HTML(html) +}