From 71a4cdac81112975969129d308899edd155c0e80 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 22 Feb 2023 18:19:30 +0800 Subject: [PATCH] update user about me markdown --- internal/schema/user_schema.go | 2 +- pkg/converter/markdown.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.go index 308568c8..dcc8ad89 100644 --- a/internal/schema/user_schema.go +++ b/internal/schema/user_schema.go @@ -309,7 +309,7 @@ func (req *UpdateInfoRequest) Check() (errFields []*validator.FormErrorField, er return errFields, errors.BadRequest(reason.UsernameInvalid) } } - req.BioHTML = converter.Markdown2HTML(req.Bio) + req.BioHTML = converter.Markdown2BasicHTML(req.Bio) return nil, nil } diff --git a/pkg/converter/markdown.go b/pkg/converter/markdown.go index d61d79f9..b1e2a418 100644 --- a/pkg/converter/markdown.go +++ b/pkg/converter/markdown.go @@ -35,6 +35,17 @@ func Markdown2HTML(source string) string { return buf.String() } +// Markdown2BasicHTML convert markdown to html ,Only basic syntax can be used +func Markdown2BasicHTML(source string) string { + content := Markdown2HTML(source) + filter := bluemonday.NewPolicy() + filter.AllowElements("p", "b", "br") + filter.AllowAttrs("src").OnElements("img") + filter.AddSpaceWhenStrippingTag(true) + content = filter.Sanitize(content) + return content +} + type DangerousHTMLFilterExtension struct { }