Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(markdown): convert markdown to html in backend

LinkinStarfdde89cbb1ff
diff --git a/go.mod b/go.mod
index ce792597..78fd6031 100644
--- a/go.mod
+++ b/go.mod
@@ -15,6 +15,7 @@ require (
github.com/go-sql-driver/mysql v1.6.0
github.com/goccy/go-json v0.9.11
github.com/golang/mock v1.4.4
+ github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c
github.com/google/uuid v1.3.0
github.com/google/wire v0.5.0
github.com/gosimple/slug v1.13.1
diff --git a/go.sum b/go.sum
index d36b4d64..dec4922a 100644
--- a/go.sum
+++ b/go.sum
@@ -256,6 +256,8 @@ github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8l
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
+github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c h1:iyaGYbCmcYK0Ja9a3OUa2Fo+EaN0cbLu0eKpBwPFzc8=
+github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
diff --git a/internal/schema/answer_schema.go b/internal/schema/answer_schema.go
index aaac3558..c6f47485 100644
--- a/internal/schema/answer_schema.go
+++ b/internal/schema/answer_schema.go
@@ -1,5 +1,10 @@
package schema
+import (
+ "github.com/answerdev/answer/internal/base/validator"
+ "github.com/answerdev/answer/pkg/converter"
+)
+
// RemoveAnswerReq delete answer request
type RemoveAnswerReq struct {
// answer id
@@ -21,6 +26,11 @@ type AnswerAddReq struct {
UserID string `json:"-" ` // user_id
}
+func (req *AnswerAddReq) Check() (errFields []*validator.FormErrorField, err error) {
+ req.HTML = converter.Markdown2HTML(req.Content)
+ return nil, nil
+}
+
type AnswerUpdateReq struct {
ID string `json:"id"` // id
QuestionID string `json:"question_id" ` // question_id
@@ -34,6 +44,11 @@ type AnswerUpdateReq struct {
CanEdit bool `json:"-"`
}
+func (req *AnswerUpdateReq) Check() (errFields []*validator.FormErrorField, err error) {
+ req.HTML = converter.Markdown2HTML(req.Content)
+ return nil, nil
+}
+
// AnswerUpdateResp answer update resp
type AnswerUpdateResp struct {
WaitForReview bool `json:"wait_for_review"`
diff --git a/internal/schema/comment_schema.go b/internal/schema/comment_schema.go
index cf49edc8..05b3e18a 100644
--- a/internal/schema/comment_schema.go
+++ b/internal/schema/comment_schema.go
@@ -1,7 +1,9 @@
package schema
import (
+ "github.com/answerdev/answer/internal/base/validator"
"github.com/answerdev/answer/internal/entity"
+ "github.com/answerdev/answer/pkg/converter"
"github.com/jinzhu/copier"
)
@@ -27,6 +29,11 @@ type AddCommentReq struct {
CanDelete bool `json:"-"`
}
+func (req *AddCommentReq) Check() (errFields []*validator.FormErrorField, err error) {
+ req.ParsedText = converter.Markdown2HTML(req.OriginalText)
+ return nil, nil
+}
+
// RemoveCommentReq remove comment
type RemoveCommentReq struct {
// comment id
@@ -47,6 +54,11 @@ type UpdateCommentReq struct {
UserID string `json:"-"`
}
+func (req *UpdateCommentReq) Check() (errFields []*validator.FormErrorField, err error) {
+ req.ParsedText = converter.Markdown2HTML(req.OriginalText)
+ return nil, nil
+}
+
// GetCommentListReq get comment list all request
type GetCommentListReq struct {
// user id
diff --git a/internal/schema/question_schema.go b/internal/schema/question_schema.go
index 08f61d11..47c48cde 100644
--- a/internal/schema/question_schema.go
+++ b/internal/schema/question_schema.go
@@ -1,5 +1,10 @@
package schema
+import (
+ "github.com/answerdev/answer/internal/base/validator"
+ "github.com/answerdev/answer/pkg/converter"
+)
+
const (
SitemapMaxSize = 50000
SitemapCachekey = "answer@sitemap"
@@ -46,6 +51,16 @@ type QuestionAdd struct {
QuestionPermission
}
+func (req *QuestionAdd) Check() (errFields []*validator.FormErrorField, err error) {
+ req.HTML = converter.Markdown2HTML(req.Content)
+ for _, tag := range req.Tags {
+ if len(tag.OriginalText) > 0 {
+ tag.ParsedText = converter.Markdown2HTML(tag.OriginalText)
+ }
+ }
+ return nil, nil
+}
+
type QuestionPermission struct {
// whether user can add it
CanAdd bool `json:"-"`
@@ -88,6 +103,11 @@ type QuestionUpdate struct {
QuestionPermission
}
+func (req *QuestionUpdate) Check() (errFields []*validator.FormErrorField, err error) {
+ req.HTML = converter.Markdown2HTML(req.Content)
+ return nil, nil
+}
+
type QuestionBaseInfo struct {
ID string `json:"id" `
Title string `json:"title" xorm:"title"` // title
diff --git a/internal/schema/tag_schema.go b/internal/schema/tag_schema.go
index 278b309d..3b7100a2 100644
--- a/internal/schema/tag_schema.go
+++ b/internal/schema/tag_schema.go
@@ -5,6 +5,7 @@ import (
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/validator"
+ "github.com/answerdev/answer/pkg/converter"
"github.com/segmentfault/pacman/errors"
)
@@ -175,8 +176,9 @@ type UpdateTagReq struct {
func (r *UpdateTagReq) Check() (errFields []*validator.FormErrorField, err error) {
if len(r.EditSummary) == 0 {
- r.EditSummary = "tag.edit.summary" // to do i18n
+ r.EditSummary = "tag.edit.summary"
}
+ r.ParsedText = converter.Markdown2HTML(r.OriginalText)
return nil, nil
}
diff --git a/pkg/converter/markdown.go b/pkg/converter/markdown.go
new file mode 100644
index 00000000..79ebead6
--- /dev/null
+++ b/pkg/converter/markdown.go
@@ -0,0 +1,11 @@
+package converter
+
+import (
+ "github.com/gomarkdown/markdown"
+)
+
+// Markdown2HTML convert markdown to html
+func Markdown2HTML(md string) string {
+ html := markdown.ToHTML([]byte(md), nil, nil)
+ return string(html)
+}