增加内容可搜索字段,屏蔽各种标记。

This commit is contained in:
2026-08-29 01:40:52 +08:00
parent cfe5cf53aa
commit f216ef4d1b
5 changed files with 125 additions and 34 deletions
+11 -9
View File
@@ -199,15 +199,16 @@ func CreatePost(c *gin.Context) {
userID, _ := c.Get("userID")
post := models.Post{
Title: req.Title,
Slug: generateSlug(req.Title),
Content: req.Content,
Summary: req.Summary,
Cover: req.Cover,
AuthorID: userID.(uint),
CategoryID: req.CategoryID,
Status: req.Status,
IsTop: req.IsTop && isAdmin(c), // 非管理员不允许置顶
Title: req.Title,
Slug: generateSlug(req.Title),
Content: req.Content,
SearchableText: string(utils.GenerateSearchableText(req.Content)),
Summary: req.Summary,
Cover: req.Cover,
AuthorID: userID.(uint),
CategoryID: req.CategoryID,
Status: req.Status,
IsTop: req.IsTop && isAdmin(c), // 非管理员不允许置顶
}
if req.Status == "published" {
@@ -269,6 +270,7 @@ func UpdatePost(c *gin.Context) {
}
if req.Content != "" {
updates["content"] = req.Content
updates["searchable_text"] = string(utils.GenerateSearchableText(req.Content))
}
if req.Summary != "" {
updates["summary"] = req.Summary
+1 -1
View File
@@ -447,7 +447,7 @@ func SearchView(c *gin.Context) {
db := database.DB.Model(&models.Post{}).Preload("Category").Preload("Tags").Preload("Author").
Where("status = ?", "published").
Where("title LIKE ? OR content LIKE ? OR summary LIKE ?",
Where("title LIKE ? OR searchable_text LIKE ? OR summary LIKE ?",
"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%")
var total int64