Skip to content

Commit

Permalink
fix: resolve message sending failure when containing "\n" using webho…
Browse files Browse the repository at this point in the history
…ok and custom channel (#111)

* 使用webhook和custom通道发送消息时如果内容中包含\n会推送失败的问题

* 消息查看页面增加markdown内容解析,访问链接页面增加GFM拓展解析

* 访问链接页面GFM footnote支持
  • Loading branch information
fghwett authored Nov 10, 2024
1 parent d5cceb9 commit ef7c2d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 5 additions & 5 deletions channel/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/google/uuid"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"html/template"
"log"
"net"
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion controller/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion web/src/components/MessagesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -416,7 +417,7 @@ const MessagesTable = () => {
) : (
''
)}
{message.content ? <p>{message.content}</p> : ''}
{message.content ? <div dangerouslySetInnerHTML={{ __html: marked.parse(message.content) }}></div> : ''}
</Modal.Content>
<Modal.Actions>
<Button
Expand Down

0 comments on commit ef7c2d0

Please sign in to comment.