From 324cc2a35085f243c04cec187a769430d1e93588 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 5 Feb 2020 14:39:56 +0100 Subject: [PATCH] Add markdown parser --- go.mod | 3 +++ go.sum | 7 +++++++ orm/participant.go | 1 + renderer/funcmap.go | 12 ++++++++++-- templates/contests_show.html.tpl | 2 +- templates/participants_show.html.tpl | 2 +- 6 files changed, 23 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index abc26254..4a5dc6f1 100644 --- a/go.mod +++ b/go.mod @@ -25,12 +25,14 @@ require ( github.com/jinzhu/inflection v1.0.0 github.com/kr/pretty v0.1.0 // indirect github.com/mailru/easyjson v0.7.0 // indirect + github.com/microcosm-cc/bluemonday v1.0.2 github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9 // indirect github.com/pkg/profile v1.4.0 github.com/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1 github.com/robfig/cron v1.2.0 github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805 // indirect github.com/sethvargo/go-password v0.1.3 + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect github.com/tsenart/go-tsz v0.0.0-20180814235614-0bd30b3df1c3 // indirect @@ -43,5 +45,6 @@ require ( golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df + gopkg.in/russross/blackfriday.v2 v2.0.0 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 4b9a4ce0..44b6e2d2 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,8 @@ github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7 github.com/mattn/go-sqlite3 v1.11.0 h1:LDdKkqtYlom37fkvqs8rMPFKAMe8+SgjbwZ6ex1/A/Q= github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s= +github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc= github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9 h1:YhsMshmD0JlqM4ss3JBY6Ul4QLigGvBtShFYHkb3JGg= github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9/go.mod h1:9N3QHEQ4Ov/dAnHmOIJ8ffm8O1iQfCPfso+PpakXPsY= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= @@ -135,6 +137,8 @@ github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805 h1:Y9XRz+J6fBX74oHZX9KwMz github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805/go.mod h1:1GjGIxIXi6t9QbWYLwl8E7knyiIbtoGShze7zXmj2Nw= github.com/sethvargo/go-password v0.1.3 h1:18KkbGDkw8SuzeohAbWqBLNSfRQblVwEHOLbPa0PvWM= github.com/sethvargo/go-password v0.1.3/go.mod h1:2tyaaoHK/AlXwh5WWQDYjqQbHcq4cjPj5qb/ciYvu/Q= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= @@ -171,6 +175,7 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -225,6 +230,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= +gopkg.in/russross/blackfriday.v2 v2.0.0 h1:+FlnIV8DSQnT7NZ43hcVKcdJdzZoeCmJj4Ql8gq5keA= +gopkg.in/russross/blackfriday.v2 v2.0.0/go.mod h1:6sSBNz/GtOm/pJTuh5UmBK2ZHfmnxGbl2NZg1UliSOI= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/orm/participant.go b/orm/participant.go index ba25e762..a680c4ab 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -294,6 +294,7 @@ func (model *Participant) Read(db *Database, args map[string]string, w http.Resp Preload("School"). Preload("School.Participants"). Preload("Responses"). + Preload("Responses.Contest"). Preload("Contests"). Preload("Category"). First(&participant, id).Error; err != nil { diff --git a/renderer/funcmap.go b/renderer/funcmap.go index aaf6296c..41ba47f4 100644 --- a/renderer/funcmap.go +++ b/renderer/funcmap.go @@ -15,6 +15,9 @@ import ( jwt "github.com/dgrijalva/jwt-go" "github.com/jinzhu/inflection" yml "gopkg.in/yaml.v2" + + "github.com/microcosm-cc/bluemonday" + "gopkg.in/russross/blackfriday.v2" ) const ( @@ -23,6 +26,7 @@ const ( var ( funcMap = template.FuncMap{ + "markdown": markdown, "version": version, "toInt": toInt, "isResponseIn": isResponseIn, @@ -60,14 +64,18 @@ var ( "trim": trim, "username": username, "isAdmin": isAdmin, + "isSchool": isSchool, + "modelId": modelId, "isParticipant": isParticipant, "isSubscriber": isSubscriber, - "isSchool": isSchool, "attr": attr, - "modelId": modelId, } ) +func markdown(text string) string { + unsafe := blackfriday.Run([]byte(text)) + return string(bluemonday.UGCPolicy().SanitizeBytes(unsafe)) +} func version() string { version, err := ioutil.ReadFile("./VERSION") if err != nil { diff --git a/templates/contests_show.html.tpl b/templates/contests_show.html.tpl index ffb12888..6e43edf9 100644 --- a/templates/contests_show.html.tpl +++ b/templates/contests_show.html.tpl @@ -27,7 +27,7 @@

Descrizione

- {{.Data.Description}} + {{.Data.Description|markdown|html}}
diff --git a/templates/participants_show.html.tpl b/templates/participants_show.html.tpl index 4b88a9c7..4fa14a9e 100644 --- a/templates/participants_show.html.tpl +++ b/templates/participants_show.html.tpl @@ -97,7 +97,7 @@ {{$response}}
-

Some quick example text to build on the card title and make up the bulk of the card's content.

+

{{$response.Contest.Description|markdown|html}}

Apri la prova