fix(钉钉通知): 修复 Markdown 消息换行被折叠导致排版错乱的问题

Motivation:
钉钉 Markdown 渲染时会将单个换行符折叠为空格,导致推送的通知内容被压缩成一行,严重影响消息可读性。

Changes:

* 发送前将消息正文中的单个换行统一转换为段落分隔(\n\n),确保模板换行在钉钉中正确渲染
* 归一化 \r\n 与 \r 换行,并合并已有连续换行,避免产生多余空行
* 新增换行转换逻辑的单元测试
This commit is contained in:
2026-08-27 16:58:41 +08:00
parent 4d4dbd27b2
commit 219a850d93
2 changed files with 23 additions and 1 deletions
+14 -1
View File
@@ -10,6 +10,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
@@ -69,7 +70,7 @@ func (s *DingTalkSender) Send(title, content string, config json.RawMessage) err
MsgType: "markdown",
Markdown: &dingtalkMD{
Title: title,
Text: content,
Text: dingtalkMarkdownText(content),
},
}
@@ -86,6 +87,18 @@ func (s *DingTalkSender) Send(title, content string, config json.RawMessage) err
return nil
}
// dingtalkMarkdownText turns template newlines into DingTalk markdown paragraph
// breaks. A single \n is collapsed to a space by DingTalk markdown, so each
// logical line must be separated by \n\n.
func dingtalkMarkdownText(content string) string {
content = strings.ReplaceAll(content, "\r\n", "\n")
content = strings.ReplaceAll(content, "\r", "\n")
for strings.Contains(content, "\n\n") {
content = strings.ReplaceAll(content, "\n\n", "\n")
}
return strings.ReplaceAll(content, "\n", "\n\n")
}
func dingtalkSign(timestamp int64, secret string) string {
mac := hmac.New(sha256.New, []byte(secret))
fmt.Fprintf(mac, "%d\n%s", timestamp, secret)