Skip to content

Commit 3a00e78

Browse files
committed
1 parent 34387a4 commit 3a00e78

File tree

7 files changed

+57
-19
lines changed

7 files changed

+57
-19
lines changed

HISTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Developers are not forced to upgrade if they don't really need it. Upgrade whene
2323

2424
Change applies to `master` branch.
2525

26+
- 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).
27+
2628
- Add `mvc.IgnoreEmbedded` option to handle [#2103](https://github.com/kataras/iris/issues/2103). Example Code:
2729

2830
```go

NOTICE

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Revision ID: 5fc50a00491616d5cd0cbce3abd8b699838e25ca
1919
4e134eadfa
2020
bbolt a8af23b57f672fe https://github.com/etcd-io/bbolt
2121
f05637de531bba5
22-
aa00013364
23-
blackfriday d3b5b032dc8e892 https://github.com/russross/blackfriday
24-
7d31a5071b56e14
25-
c89f045135
22+
aa00013364
23+
markdown 2ced44d5b58482a https://github.com/gomarkdown/markdown
24+
9b77d1abad4c3d3
25+
4b190880fe
2626
bluemonday 0a75d7616912ab9 https://github.com/microcosm-cc/bluemonday
2727
beb9cc6f7283ec1
2828
917c61b135

context/context.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import (
3030

3131
"github.com/Shopify/goreferrer"
3232
"github.com/fatih/structs"
33+
"github.com/gomarkdown/markdown"
34+
"github.com/gomarkdown/markdown/html"
3335
"github.com/iris-contrib/schema"
3436
"github.com/mailru/easyjson"
3537
"github.com/mailru/easyjson/jwriter"
3638
"github.com/microcosm-cc/bluemonday"
37-
"github.com/russross/blackfriday/v2"
3839
"github.com/vmihailenco/msgpack/v5"
3940
"golang.org/x/net/publicsuffix"
4041
"golang.org/x/time/rate"
@@ -4033,6 +4034,10 @@ type Markdown struct {
40334034
// content-specific
40344035
Sanitize bool
40354036
OmitErrorHandler bool // See JSON.OmitErrorHandler.
4037+
//
4038+
// Library-specific.
4039+
// E.g. Flags: html.CommonFlags | html.HrefTargetBlank
4040+
RenderOptions html.RendererOptions
40364041
}
40374042

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

4468+
var sanitizer = bluemonday.UGCPolicy()
4469+
44634470
// WriteMarkdown parses the markdown to html and writes these contents to the writer.
44644471
var WriteMarkdown = func(ctx *Context, markdownB []byte, options *Markdown) error {
4465-
buf := blackfriday.Run(markdownB)
4472+
out := markdown.NormalizeNewlines(markdownB)
4473+
4474+
renderer := html.NewRenderer(options.RenderOptions)
4475+
doc := markdown.Parse(out, nil)
4476+
out = markdown.Render(doc, renderer)
44664477
if options.Sanitize {
4467-
buf = bluemonday.UGCPolicy().SanitizeBytes(buf)
4478+
out = sanitizer.SanitizeBytes(out)
44684479
}
44694480

4470-
_, err := ctx.Write(buf)
4481+
_, err := ctx.Write(out)
44714482
return err
44724483
}
44734484

go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
github.com/fatih/structs v1.1.0
1616
github.com/flosch/pongo2/v4 v4.0.2
1717
github.com/golang/snappy v0.0.4
18+
github.com/gomarkdown/markdown v0.0.0-20230313173142-2ced44d5b584
1819
github.com/google/uuid v1.3.0
1920
github.com/gorilla/securecookie v1.1.1
2021
github.com/iris-contrib/httpexpect/v2 v2.12.1
@@ -32,7 +33,6 @@ require (
3233
github.com/mailru/easyjson v0.7.7
3334
github.com/microcosm-cc/bluemonday v1.0.23
3435
github.com/redis/go-redis/v9 v9.0.2
35-
github.com/russross/blackfriday/v2 v2.1.0
3636
github.com/schollz/closestmatch v2.1.0+incompatible
3737
github.com/shirou/gopsutil/v3 v3.23.2
3838
github.com/tdewolff/minify/v2 v2.12.5
@@ -78,14 +78,15 @@ require (
7878
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
7979
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
8080
github.com/modern-go/reflect2 v1.0.2 // indirect
81-
github.com/nats-io/jwt/v2 v2.3.0 // indirect
81+
github.com/nats-io/jwt/v2 v2.4.0 // indirect
8282
github.com/nats-io/nats.go v1.23.0 // indirect
83-
github.com/nats-io/nkeys v0.3.0 // indirect
83+
github.com/nats-io/nkeys v0.4.4 // indirect
8484
github.com/nats-io/nuid v1.0.1 // indirect
8585
github.com/nxadm/tail v1.4.8 // indirect
8686
github.com/pkg/errors v0.8.1 // indirect
8787
github.com/pmezard/go-difflib v1.0.0 // indirect
8888
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
89+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
8990
github.com/sanity-io/litter v1.5.5 // indirect
9091
github.com/sergi/go-diff v1.0.0 // indirect
9192
github.com/sirupsen/logrus v1.8.1 // indirect

go.sum

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mvc/controller.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,30 @@ func whatEmbeddedMethods(typ reflect.Type) []string {
201201
newEmbeddedStructType := reflect.New(structField.Type).Type()
202202
// let's take its methods and add to methods to ignore from the parent, the controller itself.
203203
for j := 0; j < newEmbeddedStructType.NumMethod(); j++ {
204-
embeddedMethodName := newEmbeddedStructType.Method(j).Name
204+
embeddedMethod := newEmbeddedStructType.Method(j)
205+
embeddedMethodName := embeddedMethod.Name
206+
// An exception should happen if the controller itself overrides the embedded method,
207+
// but Go (1.20) so far doesn't support this detection, as it handles the structure as one.
208+
/*
209+
shouldKeepBecauseParentOverrides := false
210+
211+
if v, existsOnParent := typ.MethodByName(embeddedMethodName); existsOnParent {
212+
213+
embeddedIndex := newEmbeddedStructType.Method(j).Index
214+
controllerMethodIndex := v.Index
215+
216+
if v.Type.In(0) == typ && embeddedIndex == controllerMethodIndex {
217+
fmt.Printf("%s exists on parent = true, receiver = %s\n", v.Name, typ.String())
218+
shouldKeepBecauseParentOverrides = true
219+
continue
220+
}
221+
}
222+
*/
223+
205224
embeddedMethodsToIgnore = append(embeddedMethodsToIgnore, embeddedMethodName)
206225
}
207226
}
227+
208228
return embeddedMethodsToIgnore
209229
}
210230

@@ -227,6 +247,9 @@ func (c *ControllerActivator) SkipMethods(methodNames ...string) {
227247
// SkipEmbeddedMethods should be ran before controller parsing.
228248
// It skips all embedded struct's methods conversation to http handlers.
229249
//
250+
// Note that even if the controller overrides the embedded methods
251+
// they will be still ignored because Go doesn't support this detection so far.
252+
//
230253
// See https://github.com/kataras/iris/issues/2103 for more.
231254
func (c *ControllerActivator) SkipEmbeddedMethods() {
232255
methodsToIgnore := whatEmbeddedMethods(c.Type)

mvc/mvc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ func (opt OptionFunc) Apply(c *ControllerActivator) {
197197
}
198198

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

0 commit comments

Comments
 (0)