增加markdown支持。

This commit is contained in:
2026-07-28 15:51:54 +08:00
parent 98161a2fe8
commit 21be62fa7c
15 changed files with 353 additions and 6 deletions
+16
View File
@@ -8,6 +8,7 @@ import (
"goblog/database"
"goblog/models"
"goblog/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -47,6 +48,21 @@ type UpdatePostRequest struct {
IsTop bool `json:"is_top"`
}
// Markdown 预览请求
type MarkdownPreviewRequest struct {
Content string `json:"content"`
}
// PreviewMarkdown 将 Markdown 渲染为 HTML(后台编辑器预览用)
func PreviewMarkdown(c *gin.Context) {
var req MarkdownPreviewRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"html": string(utils.RenderMarkdown(req.Content))})
}
// 生成 slug
func generateSlug(title string) string {
slug := strings.ToLower(title)
+2
View File
@@ -11,6 +11,7 @@ import (
"goblog/config"
"goblog/database"
"goblog/models"
"goblog/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -37,6 +38,7 @@ func templateFuncs() template.FuncMap {
"html": func(s string) template.HTML {
return template.HTML(s)
},
"markdown": utils.RenderMarkdown,
}
}