增加markdown支持。
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user