Skip to content

Commit

Permalink
fix #2098
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Mar 20, 2023
1 parent 34387a4 commit 3a00e78
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene

Change applies to `master` branch.

- Replace [russross/blackfriday](github.com/russross/blackfriday/v2) with [gomarkdown](https://github.com/gomarkdown/markdown) as requested at [#2098](https://github.com/kataras/iris/issues/2098).

- Add `mvc.IgnoreEmbedded` option to handle [#2103](https://github.com/kataras/iris/issues/2103). Example Code:

```go
Expand Down
8 changes: 4 additions & 4 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Revision ID: 5fc50a00491616d5cd0cbce3abd8b699838e25ca
4e134eadfa
bbolt a8af23b57f672fe https://github.com/etcd-io/bbolt
f05637de531bba5
aa00013364
blackfriday d3b5b032dc8e892 https://github.com/russross/blackfriday
7d31a5071b56e14
c89f045135
aa00013364
markdown 2ced44d5b58482a https://github.com/gomarkdown/markdown
9b77d1abad4c3d3
4b190880fe
bluemonday 0a75d7616912ab9 https://github.com/microcosm-cc/bluemonday
beb9cc6f7283ec1
917c61b135
Expand Down
19 changes: 15 additions & 4 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ import (

"github.com/Shopify/goreferrer"
"github.com/fatih/structs"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/iris-contrib/schema"
"github.com/mailru/easyjson"
"github.com/mailru/easyjson/jwriter"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/net/publicsuffix"
"golang.org/x/time/rate"
Expand Down Expand Up @@ -4033,6 +4034,10 @@ type Markdown struct {
// content-specific
Sanitize bool
OmitErrorHandler bool // See JSON.OmitErrorHandler.
//
// Library-specific.
// E.g. Flags: html.CommonFlags | html.HrefTargetBlank
RenderOptions html.RendererOptions
}

var (
Expand Down Expand Up @@ -4460,14 +4465,20 @@ func (ctx *Context) Problem(v interface{}, opts ...ProblemOptions) error {
return ctx.writeJSON(v, &options.JSON)
}

var sanitizer = bluemonday.UGCPolicy()

// WriteMarkdown parses the markdown to html and writes these contents to the writer.
var WriteMarkdown = func(ctx *Context, markdownB []byte, options *Markdown) error {
buf := blackfriday.Run(markdownB)
out := markdown.NormalizeNewlines(markdownB)

renderer := html.NewRenderer(options.RenderOptions)
doc := markdown.Parse(out, nil)
out = markdown.Render(doc, renderer)
if options.Sanitize {
buf = bluemonday.UGCPolicy().SanitizeBytes(buf)
out = sanitizer.SanitizeBytes(out)
}

_, err := ctx.Write(buf)
_, err := ctx.Write(out)
return err
}

Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/fatih/structs v1.1.0
github.com/flosch/pongo2/v4 v4.0.2
github.com/golang/snappy v0.0.4
github.com/gomarkdown/markdown v0.0.0-20230313173142-2ced44d5b584
github.com/google/uuid v1.3.0
github.com/gorilla/securecookie v1.1.1
github.com/iris-contrib/httpexpect/v2 v2.12.1
Expand All @@ -32,7 +33,6 @@ require (
github.com/mailru/easyjson v0.7.7
github.com/microcosm-cc/bluemonday v1.0.23
github.com/redis/go-redis/v9 v9.0.2
github.com/russross/blackfriday/v2 v2.1.0
github.com/schollz/closestmatch v2.1.0+incompatible
github.com/shirou/gopsutil/v3 v3.23.2
github.com/tdewolff/minify/v2 v2.12.5
Expand Down Expand Up @@ -78,14 +78,15 @@ require (
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nats-io/jwt/v2 v2.3.0 // indirect
github.com/nats-io/jwt/v2 v2.4.0 // indirect
github.com/nats-io/nats.go v1.23.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sanity-io/litter v1.5.5 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion mvc/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,30 @@ func whatEmbeddedMethods(typ reflect.Type) []string {
newEmbeddedStructType := reflect.New(structField.Type).Type()
// let's take its methods and add to methods to ignore from the parent, the controller itself.
for j := 0; j < newEmbeddedStructType.NumMethod(); j++ {
embeddedMethodName := newEmbeddedStructType.Method(j).Name
embeddedMethod := newEmbeddedStructType.Method(j)
embeddedMethodName := embeddedMethod.Name
// An exception should happen if the controller itself overrides the embedded method,
// but Go (1.20) so far doesn't support this detection, as it handles the structure as one.
/*
shouldKeepBecauseParentOverrides := false
if v, existsOnParent := typ.MethodByName(embeddedMethodName); existsOnParent {
embeddedIndex := newEmbeddedStructType.Method(j).Index
controllerMethodIndex := v.Index
if v.Type.In(0) == typ && embeddedIndex == controllerMethodIndex {
fmt.Printf("%s exists on parent = true, receiver = %s\n", v.Name, typ.String())
shouldKeepBecauseParentOverrides = true
continue
}
}
*/

embeddedMethodsToIgnore = append(embeddedMethodsToIgnore, embeddedMethodName)
}
}

return embeddedMethodsToIgnore
}

Expand All @@ -227,6 +247,9 @@ func (c *ControllerActivator) SkipMethods(methodNames ...string) {
// SkipEmbeddedMethods should be ran before controller parsing.
// It skips all embedded struct's methods conversation to http handlers.
//
// Note that even if the controller overrides the embedded methods
// they will be still ignored because Go doesn't support this detection so far.
//
// See https://github.com/kataras/iris/issues/2103 for more.
func (c *ControllerActivator) SkipEmbeddedMethods() {
methodsToIgnore := whatEmbeddedMethods(c.Type)
Expand Down
3 changes: 2 additions & 1 deletion mvc/mvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func (opt OptionFunc) Apply(c *ControllerActivator) {
}

// IgnoreEmbedded is an Option which can be used to ignore all embedded struct's method handlers.
//
// Note that even if the controller overrides the embedded methods
// they will be still ignored because Go doesn't support this detection so far.
// For global affect, set the `IgnoreEmbeddedControllers` package-level variable to true.
var IgnoreEmbedded OptionFunc = func(c *ControllerActivator) {
c.SkipEmbeddedMethods()
Expand Down

0 comments on commit 3a00e78

Please sign in to comment.