fix(dingtalk): strip leaked internal source handles

This commit is contained in:
姚章浩
2026-09-15 17:08:33 +08:00
parent c7d2629a77
commit 4e65028d1d
2 changed files with 18 additions and 3 deletions
@@ -8,7 +8,11 @@ import (
const displayReferenceMarkerPattern = `\[\[ref[\t ]+id[\t ]*=[\t ]*["']c[0-9]+["'][\t ]*\]\]`
var (
displayReferenceMarkerRE = regexp.MustCompile(displayReferenceMarkerPattern)
displayReferenceMarkerRE = regexp.MustCompile(displayReferenceMarkerPattern)
// bareInternalSourceHandleRE matches WeKnora request-local source aliases
// leaked by a model (for example [d21], [c12], or [w3]). It intentionally
// does not match ordinary Markdown links or arbitrary bracketed content.
bareInternalSourceHandleRE = regexp.MustCompile(`(?i)\[(?:[dcw][0-9]+)(?:[ ]*[,,][ ]*(?:[dcw][0-9]+))*\]`)
leadingReferenceGroupColonRE = regexp.MustCompile(`(?m)^[ ]*(?:` + displayReferenceMarkerPattern + `[ ]*)+[::][ ]*`)
spacedReferenceGroupRE = regexp.MustCompile(`[ ]+(?:` + displayReferenceMarkerPattern + `[ ]*)+`)
spaceBeforeDisplayPunctuation = regexp.MustCompile(`[ ]+([,。;:!?、,.;:!?])`)
@@ -19,12 +23,13 @@ func sanitizeDisplayAnswer(content string) string {
if index := strings.LastIndex(content, "[[ref"); index >= 0 && !strings.Contains(content[index:], "]]") {
content = content[:index]
}
if !displayReferenceMarkerRE.MatchString(content) {
if !displayReferenceMarkerRE.MatchString(content) && !bareInternalSourceHandleRE.MatchString(content) {
return content
}
content = leadingReferenceGroupColonRE.ReplaceAllString(content, "")
content = spacedReferenceGroupRE.ReplaceAllString(content, " ")
content = displayReferenceMarkerRE.ReplaceAllString(content, "")
content = bareInternalSourceHandleRE.ReplaceAllString(content, "")
content = spaceBeforeDisplayPunctuation.ReplaceAllString(content, "$1")
content = spaceAfterDisplayColon.ReplaceAllString(content, "$1")
return content
@@ -34,10 +34,20 @@ func TestSanitizeDisplayAnswerRemovesInternalReferenceMarkers(t *testing.T) {
want: "Feature A remains supported.",
},
{
name: "unrelated double bracket syntax is preserved",
name: "unrelated double bracket syntax and ordinary link are preserved",
input: "请保留 [[wiki|页面名称]] 和 [普通链接](https://example.com)。",
want: "请保留 [[wiki|页面名称]] 和 [普通链接](https://example.com)。",
},
{
name: "bare internal source handles",
input: "Nova支持虚拟控制器离线仿真[d21],并可搭配VX500[w3]。",
want: "Nova支持虚拟控制器离线仿真,并可搭配VX500。",
},
{
name: "grouped bare internal source handles",
input: "该结论由多个资料支持 [d21, c12,w3]。",
want: "该结论由多个资料支持。",
},
{
name: "content without references remains byte for byte unchanged",
input: "Protocol: value ; keep spacing\n indented",