Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/markdown.go

41 lignes
1 Kio
Go
Brut Vue normale Historique

package util
import (
"github.com/microcosm-cc/bluemonday"
md "github.com/russross/blackfriday"
"html/template"
"strings"
)
//Some default rules, plus and minus some.
var mdOptions = 0 |
md.EXTENSION_AUTOLINK |
md.EXTENSION_HARD_LINE_BREAK |
md.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK |
md.EXTENSION_NO_INTRA_EMPHASIS |
md.EXTENSION_SPACE_HEADERS |
md.EXTENSION_STRIKETHROUGH
var htmlFlags = 0 |
md.HTML_USE_XHTML |
md.HTML_SMARTYPANTS_FRACTIONS |
md.HTML_SAFELINK |
md.HTML_NOREFERRER_LINKS |
md.HTML_HREF_TARGET_BLANK
func init() {
HtmlMdRenderer = md.HtmlRenderer(htmlFlags, "", "")
}
var HtmlMdRenderer md.Renderer
// TODO: restrict certain types of markdown
func MarkdownToHTML(markdown string) template.HTML {
if len(markdown) >= 3 && markdown[:3] == ">" {
markdown = ">" + markdown[3:]
}
markdown = strings.Replace(markdown,"\n>","\n>", -1)
unsafe := md.MarkdownOptions([]byte(markdown), HtmlMdRenderer, md.Options{Extensions: mdOptions})
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
return template.HTML(html)
}