mirror of
https://github.com/eryajf/chatgpt-dingtalk.git
synced 2026-04-22 23:47:15 +08:00
fix: 修复钉钉Markdown消息内不支持html代码显示的bug,对html标签进行转义 (#213)
This commit is contained in:
@@ -2,6 +2,7 @@ package process
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -231,6 +232,7 @@ func Do(mode string, rmsg *dingbot.ReceiveMsg) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FormatTimeDuation 格式化时间
|
||||
// 主要提示单聊/群聊切换时多久后恢复默认聊天模式
|
||||
func FormatTimeDuation(duration time.Duration) string {
|
||||
@@ -246,15 +248,23 @@ func FormatTimeDuation(duration time.Duration) string {
|
||||
}
|
||||
|
||||
// FormatMarkdown 格式化Markdown
|
||||
// 主要修复ChatGPT返回多行代码块,钉钉会将代码块中的#当作Markdown语法里的标题来处理,这里进行下转义
|
||||
// 主要修复ChatGPT返回多行代码块,钉钉会将代码块中的#当作Markdown语法里的标题来处理,进行转义;如果Markdown格式内存在html,将Markdown中的html标签转义
|
||||
// 代码块缩进问题暂无法解决,因不管是四个空格,还是Tab,在钉钉上均会顶格显示,建议复制代码后用IDE进行代码格式化,针对缩进严格的语言,例如Python,不确定的建议手机端查看下代码块的缩进
|
||||
func FormatMarkdown(md string) string {
|
||||
lines := strings.Split(md, "\n")
|
||||
codeblock := false
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, "```") {
|
||||
codeblock = !codeblock
|
||||
} else if codeblock && strings.HasPrefix(line, "#") {
|
||||
lines[i] = "\\" + lines[i]
|
||||
}
|
||||
if codeblock {
|
||||
lines[i] = strings.ReplaceAll(line, "#", "\\#")
|
||||
}
|
||||
}
|
||||
// 如果Markdown内存在html,将Markdown中的html标签转义
|
||||
if strings.Contains(md, "<") {
|
||||
for i, line := range lines {
|
||||
lines[i] = html.EscapeString(line)
|
||||
}
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
|
||||
Reference in New Issue
Block a user