- 引入 @tiptap/extension-code-block-lowlight + lowlight 实现代码语法高亮 - 工具栏新增语言下拉选择器,光标在代码块内时显示,支持 27 种常用语言 - 默认语言 text,高亮主题适配 MetaLab 深蓝背景 (#1a2332) - 代码块语言标签用 JS 动态注入 DOM 元素(避免 ::before 在 overflow 容器中失效) - 详情页同步代码块高亮和语言标签显示 - 编辑器与详情页样式统一:代码块暗色背景、引用蓝色边框、链接 accent 蓝 - sanitizePolicy 放行 data-language 属性和 hljs-* class - 修复 import map 避免 keyed plugin 重复实例 - 修复详情页代码块开头空白行(移除多余的 padding-top) - 修复语法错误(多余的闭合括号)
207 lines
5.9 KiB
Go
207 lines
5.9 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"regexp"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/config"
|
||
"metazone.cc/metalab/internal/model"
|
||
|
||
"github.com/microcosm-cc/bluemonday"
|
||
)
|
||
|
||
// sanitizePolicy Tiptap 输出 HTML 的消毒策略
|
||
// 前端 WYSIWYG 编辑器输出 HTML,由 bluemonday UGC 策略消毒
|
||
// 放行 code/pre 的 class/data-language 属性以保留代码高亮和语言标签
|
||
// 放行 span 的 class 属性以保留 highlight.js 的 hljs-* 高亮
|
||
// 放行 img 的 src/alt/title 属性以保留图片
|
||
var sanitizePolicy = func() *bluemonday.Policy {
|
||
p := bluemonday.UGCPolicy()
|
||
p.AllowAttrs("class", "data-language").OnElements("code", "pre")
|
||
p.AllowAttrs("class").Matching(regexp.MustCompile("^hljs-")).OnElements("span")
|
||
p.AllowAttrs("src", "alt", "title").OnElements("img")
|
||
return p
|
||
}()
|
||
|
||
// postStore 帖子服务所需的最小仓储接口(ISP)
|
||
type postStore interface {
|
||
Create(post *model.Post) error
|
||
FindByID(id uint) (*model.Post, error)
|
||
FindByIDWithAuthor(id uint) (*model.Post, error)
|
||
FindPageable(keyword string, offset, limit int) ([]model.Post, int64, error)
|
||
FindAdminPageable(keyword, status string, offset, limit int) ([]model.Post, int64, error)
|
||
Update(post *model.Post) error
|
||
SoftDelete(id uint) error
|
||
Restore(id uint) error
|
||
}
|
||
|
||
// PostService 帖子业务逻辑
|
||
type PostService struct {
|
||
repo postStore
|
||
ss *config.SiteSettings
|
||
}
|
||
|
||
// NewPostService 构造函数
|
||
func NewPostService(repo postStore, ss *config.SiteSettings) *PostService {
|
||
return &PostService{
|
||
repo: repo,
|
||
ss: ss,
|
||
}
|
||
}
|
||
|
||
// auditEnabled 审核是否开启
|
||
func (s *PostService) auditEnabled() bool {
|
||
return s.ss.IsAuditEnabled()
|
||
}
|
||
|
||
// 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 := s.sanitizeHTML(body)
|
||
|
||
status := model.PostStatusApproved
|
||
if s.auditEnabled() {
|
||
status = model.PostStatusDraft
|
||
}
|
||
|
||
post := &model.Post{
|
||
Title: title,
|
||
Body: body,
|
||
BodyHTML: bodyHTML,
|
||
UserID: userID,
|
||
Status: status,
|
||
AllowComment: true,
|
||
}
|
||
if err := s.repo.Create(post); err != nil {
|
||
return nil, err
|
||
}
|
||
return post, nil
|
||
}
|
||
|
||
// GetByID 按 ID 获取帖子(含作者名,仅 approved 公开可见)
|
||
func (s *PostService) GetByID(id uint) (*model.Post, error) {
|
||
post, err := s.repo.FindByIDWithAuthor(id)
|
||
if err != nil {
|
||
return nil, common.ErrUserNotFound // 复用哨兵表示资源不存在
|
||
}
|
||
return post, nil
|
||
}
|
||
|
||
// List 公开帖子列表(仅 approved)
|
||
func (s *PostService) List(keyword string, page, pageSize int) ([]model.Post, int64, error) {
|
||
p := common.Pagination{Page: page, PageSize: pageSize}
|
||
p.DefaultPagination()
|
||
return s.repo.FindPageable(keyword, p.Offset(), p.PageSize)
|
||
}
|
||
|
||
// ListAdmin 管理后台帖子列表(全状态)
|
||
func (s *PostService) ListAdmin(keyword, status string, page, pageSize int) ([]model.Post, int64, error) {
|
||
p := common.Pagination{Page: page, PageSize: pageSize}
|
||
p.DefaultPagination()
|
||
return s.repo.FindAdminPageable(keyword, status, p.Offset(), p.PageSize)
|
||
}
|
||
|
||
// Update 编辑帖子(权限在 controller 层检查)
|
||
func (s *PostService) Update(postID uint, title, body string) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
|
||
if post.Status == model.PostStatusPending || post.Status == model.PostStatusLocked {
|
||
return fmt.Errorf("当前状态不允许编辑")
|
||
}
|
||
|
||
post.Title = title
|
||
post.Body = body
|
||
post.BodyHTML = s.sanitizeHTML(body)
|
||
|
||
// rejected 状态编辑后自动重置为 draft
|
||
if post.Status == model.PostStatusRejected {
|
||
post.Status = model.PostStatusDraft
|
||
post.RejectReason = ""
|
||
}
|
||
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Delete 软删除(权限在 controller 层检查)
|
||
func (s *PostService) Delete(postID uint) error {
|
||
return s.repo.SoftDelete(postID)
|
||
}
|
||
|
||
// SubmitForAudit 提交审核:draft → pending
|
||
func (s *PostService) SubmitForAudit(postID uint) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
if post.Status != model.PostStatusDraft && post.Status != model.PostStatusRejected {
|
||
return fmt.Errorf("仅草稿和已退回帖子可提交审核")
|
||
}
|
||
post.Status = model.PostStatusPending
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Approve 审核通过:pending → approved
|
||
func (s *PostService) Approve(postID uint) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
if post.Status != model.PostStatusPending {
|
||
return fmt.Errorf("仅待审核帖子可通过")
|
||
}
|
||
post.Status = model.PostStatusApproved
|
||
post.RejectReason = ""
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Reject 退回:pending/approved → rejected
|
||
func (s *PostService) Reject(postID uint, reason string) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
if post.Status != model.PostStatusPending && post.Status != model.PostStatusApproved {
|
||
return fmt.Errorf("仅待审核和已发布帖子可退回")
|
||
}
|
||
post.Status = model.PostStatusRejected
|
||
post.RejectReason = reason
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Lock 锁定:任意状态 → locked
|
||
func (s *PostService) Lock(postID uint) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
post.Status = model.PostStatusLocked
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Unlock 解锁:locked → 草稿
|
||
func (s *PostService) Unlock(postID uint) error {
|
||
post, err := s.repo.FindByID(postID)
|
||
if err != nil {
|
||
return common.ErrUserNotFound
|
||
}
|
||
if post.Status != model.PostStatusLocked {
|
||
return fmt.Errorf("仅锁定状态可解锁")
|
||
}
|
||
post.Status = model.PostStatusDraft
|
||
return s.repo.Update(post)
|
||
}
|
||
|
||
// Restore 恢复软删除
|
||
func (s *PostService) Restore(postID uint) error {
|
||
return s.repo.Restore(postID)
|
||
}
|