feat: wangEditor 替换为 ByteMD Markdown 编辑器
- 移除 wangEditor 5 库文件(1.27MB JS + 14KB CSS,含手动 hack) - 集成 ByteMD 1.22.0(基于 CodeMirror 5,不依赖 contentEditable) - 编辑器输出 Markdown 原文,后端 goldmark + bluemonday 双重防护渲染 HTML - 删除 editor.js 中 scrollIntoView hack、code-lang-label DOM 注入、PreviewAPI 调用 - 清理 posts.css 中 ~200 行 wangEditor 样式覆盖(w-e-* 类名) - 清理 show.html 中 code-lang-label JS 注入(goldmark 自带 language-xxx class) - 更新 CSP 安全策略:移除 CodeMirror 注释,保留 esm.sh CDN - go.mod:bluemonday 和 goldmark 均保留为直接依赖(纵深防御)
This commit is contained in:
@ -12,9 +12,10 @@ import (
|
||||
"github.com/yuin/goldmark"
|
||||
)
|
||||
|
||||
// ucgPolicy 对 Goldmark 输出的 HTML 做安全消毒
|
||||
// UGC 默认策略会剥离 code/pre 的 class 属性,需要显式放行以保留 language-xxx
|
||||
var ucgPolicy = func() *bluemonday.Policy {
|
||||
// sanitizePolicy 纵深防御:对 goldmark 输出的 HTML 再做一层消毒
|
||||
// goldmark 默认已过滤原始 HTML 和危险链接,但 bluemonday 作为第二道防线
|
||||
// UGC 策略会剥离 code/pre 的 class 属性,需要显式放行以保留 language-xxx
|
||||
var sanitizePolicy = func() *bluemonday.Policy {
|
||||
p := bluemonday.UGCPolicy()
|
||||
p.AllowAttrs("class").OnElements("code", "pre")
|
||||
return p
|
||||
@ -48,29 +49,32 @@ func NewPostService(repo postStore, ss *config.SiteSettings) *PostService {
|
||||
}
|
||||
}
|
||||
|
||||
// auditEnabled 审核是否开启(文案审核与全局审核开关一致)
|
||||
// auditEnabled 审核是否开启
|
||||
func (s *PostService) auditEnabled() bool {
|
||||
return s.ss.IsAuditEnabled()
|
||||
}
|
||||
|
||||
// sanitizeHTML 对编辑器输出的 HTML 做安全消毒(bluemonday)
|
||||
// 编辑器提交的是富文本 HTML,不再经过 Markdown 渲染
|
||||
func (s *PostService) sanitizeHTML(body string) string {
|
||||
return ucgPolicy.Sanitize(body)
|
||||
}
|
||||
|
||||
// RenderMarkdown 对外暴露,用于预览(从纯文本 Markdown 渲染为 HTML)
|
||||
func (s *PostService) RenderMarkdown(body string) (string, error) {
|
||||
// renderMarkdown 将 Markdown 文本渲染为安全 HTML
|
||||
// goldmark(默认安全模式)→ bluemonday(纵深防御)
|
||||
func (s *PostService) renderMarkdown(body string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := s.md.Convert([]byte(body), &buf); err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("markdown render: %w", err)
|
||||
}
|
||||
return ucgPolicy.Sanitize(buf.String()), nil
|
||||
return sanitizePolicy.Sanitize(buf.String()), nil
|
||||
}
|
||||
|
||||
// RenderMarkdown 对外暴露,用于预览 API
|
||||
func (s *PostService) RenderMarkdown(body string) (string, error) {
|
||||
return s.renderMarkdown(body)
|
||||
}
|
||||
|
||||
// Create 创建帖子
|
||||
func (s *PostService) Create(userID uint, title, body string) (*model.Post, error) {
|
||||
bodyHTML := s.sanitizeHTML(body)
|
||||
bodyHTML, err := s.renderMarkdown(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
status := model.PostStatusApproved
|
||||
if s.auditEnabled() {
|
||||
@ -78,11 +82,11 @@ func (s *PostService) Create(userID uint, title, body string) (*model.Post, erro
|
||||
}
|
||||
|
||||
post := &model.Post{
|
||||
Title: title,
|
||||
Body: body,
|
||||
BodyHTML: bodyHTML,
|
||||
UserID: userID,
|
||||
Status: status,
|
||||
Title: title,
|
||||
Body: body,
|
||||
BodyHTML: bodyHTML,
|
||||
UserID: userID,
|
||||
Status: status,
|
||||
AllowComment: true,
|
||||
}
|
||||
if err := s.repo.Create(post); err != nil {
|
||||
@ -121,14 +125,18 @@ func (s *PostService) Update(postID uint, title, body string) error {
|
||||
return common.ErrUserNotFound
|
||||
}
|
||||
|
||||
// pending / locked 禁止编辑
|
||||
if post.Status == model.PostStatusPending || post.Status == model.PostStatusLocked {
|
||||
return fmt.Errorf("当前状态不允许编辑")
|
||||
}
|
||||
|
||||
bodyHTML, err := s.renderMarkdown(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post.Title = title
|
||||
post.Body = body
|
||||
post.BodyHTML = s.sanitizeHTML(body)
|
||||
post.BodyHTML = bodyHTML
|
||||
|
||||
// rejected 状态编辑后自动重置为 draft
|
||||
if post.Status == model.PostStatusRejected {
|
||||
|
||||
Reference in New Issue
Block a user