From ef7c2d0a9d0ed574b0fff23e71dadf1d07cc7910 Mon Sep 17 00:00:00 2001 From: fghwett <38271480+fghwett@users.noreply.github.com> Date: Sun, 10 Nov 2024 10:58:12 +0800 Subject: [PATCH] fix: resolve message sending failure when containing "\n" using webhook and custom channel (#111) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 使用webhook和custom通道发送消息时如果内容中包含\n会推送失败的问题 * 消息查看页面增加markdown内容解析,访问链接页面增加GFM拓展解析 * 访问链接页面GFM footnote支持 --- channel/custom.go | 10 +++++----- common/utils.go | 14 +++++++++++++- controller/webhook.go | 2 +- web/src/components/MessagesTable.js | 3 ++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/channel/custom.go b/channel/custom.go index 7e96daacc..61743aa6f 100644 --- a/channel/custom.go +++ b/channel/custom.go @@ -18,11 +18,11 @@ func SendCustomMessage(message *model.Message, user *model.User, channel_ *model return errors.New("自定义通道不能使用本服务地址") } template := channel_.Other - template = strings.Replace(template, "$url", message.URL, -1) - template = strings.Replace(template, "$to", message.To, -1) - template = strings.Replace(template, "$title", message.Title, -1) - template = strings.Replace(template, "$description", message.Description, -1) - template = strings.Replace(template, "$content", message.Content, -1) + template = common.Replace(template, "$url", message.URL, -1) + template = common.Replace(template, "$to", message.To, -1) + template = common.Replace(template, "$title", message.Title, -1) + template = common.Replace(template, "$description", message.Description, -1) + template = common.Replace(template, "$content", message.Content, -1) reqBody := []byte(template) resp, err := http.Post(url, "application/json", bytes.NewReader(reqBody)) if err != nil { diff --git a/common/utils.go b/common/utils.go index bf6145bc3..b4f0d6499 100644 --- a/common/utils.go +++ b/common/utils.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/google/uuid" "github.com/yuin/goldmark" + "github.com/yuin/goldmark/extension" "html/template" "log" "net" @@ -148,7 +149,13 @@ func Markdown2HTML(markdown string) (HTML string, err error) { return "", nil } var buf bytes.Buffer - err = goldmark.Convert([]byte(markdown), &buf) + goldMarkEntity := goldmark.New( + goldmark.WithExtensions( + extension.GFM, + extension.Footnote, + ), + ) + err = goldMarkEntity.Convert([]byte(markdown), &buf) if err != nil { return fmt.Sprintf("Markdown 渲染出错:%s", err.Error()), err } @@ -159,3 +166,8 @@ func Markdown2HTML(markdown string) (HTML string, err error) { func GetTimestamp() int64 { return time.Now().Unix() } + +func Replace(s, old, new string, n int) string { + new = strings.TrimPrefix(strings.TrimSuffix(fmt.Sprintf("%q", new), "\""), "\"") + return strings.Replace(s, old, new, n) +} diff --git a/controller/webhook.go b/controller/webhook.go index f4deeb990..8b90cc32c 100644 --- a/controller/webhook.go +++ b/controller/webhook.go @@ -240,7 +240,7 @@ func TriggerWebhook(c *gin.Context) { } for key, value := range extractRule { variableValue := gjson.Get(reqText, value).String() - webhook.ConstructRule = strings.Replace(webhook.ConstructRule, "$"+key, variableValue, -1) + webhook.ConstructRule = common.Replace(webhook.ConstructRule, "$"+key, variableValue, -1) } constructRule := model.WebhookConstructRule{} err = json.Unmarshal([]byte(webhook.ConstructRule), &constructRule) diff --git a/web/src/components/MessagesTable.js b/web/src/components/MessagesTable.js index ac3de2e7b..2b7bdfaa5 100644 --- a/web/src/components/MessagesTable.js +++ b/web/src/components/MessagesTable.js @@ -5,6 +5,7 @@ import { API, openPage, showError, showSuccess, showWarning } from '../helpers'; import { ITEMS_PER_PAGE } from '../constants'; import { renderTimestamp } from '../helpers/render'; import { Link } from 'react-router-dom'; +import { marked } from 'marked'; function renderStatus(status) { switch (status) { @@ -416,7 +417,7 @@ const MessagesTable = () => { ) : ( '' )} - {message.content ?

{message.content}

: ''} + {message.content ?
: ''}