Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Move markdown stuff to it's own function

Cette révision appartient à :
Eliot Whalan 2017-05-09 17:28:29 +10:00
Parent be06251bba
révision 3c018c4988
2 fichiers modifiés avec 17 ajouts et 8 suppressions

Voir le fichier

@ -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),

15
util/markdown.go Fichier normal
Voir le fichier

@ -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)
}