fix: render description as HTML when rendering message (close #42)

This commit is contained in:
JustSong
2023-01-06 14:23:07 +08:00
parent ccf131037b
commit 89e7a10d20
4 changed files with 23 additions and 14 deletions

View File

@@ -1,9 +1,7 @@
package channel package channel
import ( import (
"bytes"
"errors" "errors"
"github.com/yuin/goldmark"
"message-pusher/common" "message-pusher/common"
"message-pusher/model" "message-pusher/model"
) )
@@ -17,12 +15,10 @@ func SendEmailMessage(message *model.Message, user *model.User) error {
subject = message.Title subject = message.Title
} }
if message.Content != "" { if message.Content != "" {
var buf bytes.Buffer var err error
err := goldmark.Convert([]byte(message.Content), &buf) message.HTMLContent, err = common.Markdown2HTML(message.Content)
if err != nil { if err != nil {
common.SysLog(err.Error()) common.SysLog(err.Error())
} else {
message.HTMLContent = buf.String()
} }
} }
return common.SendEmail(subject, user.Email, message.HTMLContent) return common.SendEmail(subject, user.Email, message.HTMLContent)

View File

@@ -19,7 +19,7 @@
<span class="line">发布于:<span class="tag is-light">{{.time}}</span></span> <span class="line">发布于:<span class="tag is-light">{{.time}}</span></span>
</div> </div>
<blockquote> <blockquote>
<p>{{.description}}</p> {{.description | unescape}}
</blockquote> </blockquote>
{{.content | unescape}} {{.content | unescape}}
</article> </article>

View File

@@ -1,8 +1,10 @@
package common package common
import ( import (
"bytes"
"fmt" "fmt"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/yuin/goldmark"
"html/template" "html/template"
"log" "log"
"net" "net"
@@ -139,3 +141,13 @@ func Max(a int, b int) int {
return b return b
} }
} }
func Markdown2HTML(markdown string) (HTML string, err error) {
var buf bytes.Buffer
err = goldmark.Convert([]byte(markdown), &buf)
if err != nil {
return fmt.Sprintf("Markdown 渲染出错:%s", err.Error()), err
}
HTML = buf.String()
return
}

View File

@@ -1,11 +1,9 @@
package controller package controller
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/yuin/goldmark"
"message-pusher/channel" "message-pusher/channel"
"message-pusher/common" "message-pusher/common"
"message-pusher/model" "message-pusher/model"
@@ -173,13 +171,16 @@ func RenderMessage(c *gin.Context) {
c.Status(http.StatusNotFound) c.Status(http.StatusNotFound)
return return
} }
if message.Content != "" { if message.Description != "" {
var buf bytes.Buffer message.Description, err = common.Markdown2HTML(message.Description)
err := goldmark.Convert([]byte(message.Content), &buf) if err != nil {
common.SysLog(err.Error())
}
}
if message.Content != "" {
message.HTMLContent, err = common.Markdown2HTML(message.Content)
if err != nil { if err != nil {
common.SysLog(err.Error()) common.SysLog(err.Error())
} else {
message.HTMLContent = buf.String()
} }
} }
c.HTML(http.StatusOK, "message.html", gin.H{ c.HTML(http.StatusOK, "message.html", gin.H{