refactor: ByteMD 迁移至 Tiptap WYSIWYG 编辑器 + Token 刷新优化
- 移除 ByteMD CDN 资源和 goldmark 依赖,替换为 Tiptap ESM 模块 - 编辑器从源码编辑模式改为 WYSIWYG 所见即所得 - 移除 /api/posts/preview 预览接口(Tiptap 直接输出 HTML 无需后端渲染) - 保留 UploadImage 图片上传接口和 bluemonday HTML 消毒 - 工具栏支持加粗、斜体、删除线、代码、标题、列表、引用、代码块、分割线、撤销/重做 - 支持图片粘贴/拖拽上传,Ctrl+S 快捷提交,F11 全屏,字数统计 - 优化 common.js:自动 401 拦截 Token 刷新,修复刷新死循环 - 草稿 localStorage 键名更新为 draft_post_body_html
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"metazone.cc/metalab/internal/common"
|
||||
@ -9,15 +8,16 @@ import (
|
||||
"metazone.cc/metalab/internal/model"
|
||||
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/yuin/goldmark"
|
||||
)
|
||||
|
||||
// sanitizePolicy 纵深防御:对 goldmark 输出的 HTML 再做一层消毒
|
||||
// goldmark 默认已过滤原始 HTML 和危险链接,但 bluemonday 作为第二道防线
|
||||
// UGC 策略会剥离 code/pre 的 class 属性,需要显式放行以保留 language-xxx
|
||||
// sanitizePolicy Tiptap 输出 HTML 的消毒策略
|
||||
// 前端 WYSIWYG 编辑器输出 HTML,由 bluemonday UGC 策略消毒
|
||||
// 显式放行 code/pre 的 class 属性以保留代码块样式
|
||||
// 放行 img 的 src/alt 属性以保留图片
|
||||
var sanitizePolicy = func() *bluemonday.Policy {
|
||||
p := bluemonday.UGCPolicy()
|
||||
p.AllowAttrs("class").OnElements("code", "pre")
|
||||
p.AllowAttrs("src", "alt", "title").OnElements("img")
|
||||
return p
|
||||
}()
|
||||
|
||||
@ -37,7 +37,6 @@ type postStore interface {
|
||||
type PostService struct {
|
||||
repo postStore
|
||||
ss *config.SiteSettings
|
||||
md goldmark.Markdown
|
||||
}
|
||||
|
||||
// NewPostService 构造函数
|
||||
@ -45,7 +44,6 @@ func NewPostService(repo postStore, ss *config.SiteSettings) *PostService {
|
||||
return &PostService{
|
||||
repo: repo,
|
||||
ss: ss,
|
||||
md: goldmark.New(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,27 +52,15 @@ func (s *PostService) auditEnabled() bool {
|
||||
return s.ss.IsAuditEnabled()
|
||||
}
|
||||
|
||||
// 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 "", fmt.Errorf("markdown render: %w", err)
|
||||
}
|
||||
return sanitizePolicy.Sanitize(buf.String()), nil
|
||||
}
|
||||
|
||||
// RenderMarkdown 对外暴露,用于预览 API
|
||||
func (s *PostService) RenderMarkdown(body string) (string, error) {
|
||||
return s.renderMarkdown(body)
|
||||
// sanitizeHTML 对 Tiptap 输出的 HTML 进行消毒
|
||||
// 前端 WYSIWYG 编辑器直接输出 HTML,后端仅做安全消毒
|
||||
func (s *PostService) sanitizeHTML(body string) string {
|
||||
return sanitizePolicy.Sanitize(body)
|
||||
}
|
||||
|
||||
// Create 创建帖子
|
||||
func (s *PostService) Create(userID uint, title, body string) (*model.Post, error) {
|
||||
bodyHTML, err := s.renderMarkdown(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bodyHTML := s.sanitizeHTML(body)
|
||||
|
||||
status := model.PostStatusApproved
|
||||
if s.auditEnabled() {
|
||||
@ -129,14 +115,9 @@ func (s *PostService) Update(postID uint, title, body string) error {
|
||||
return fmt.Errorf("当前状态不允许编辑")
|
||||
}
|
||||
|
||||
bodyHTML, err := s.renderMarkdown(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post.Title = title
|
||||
post.Body = body
|
||||
post.BodyHTML = bodyHTML
|
||||
post.BodyHTML = s.sanitizeHTML(body)
|
||||
|
||||
// rejected 状态编辑后自动重置为 draft
|
||||
if post.Status == model.PostStatusRejected {
|
||||
|
||||
Reference in New Issue
Block a user