package dingtalk import ( "regexp" "strings" ) const displayReferenceMarkerPattern = `\[\[ref[\t ]+id[\t ]*=[\t ]*["']c[0-9]+["'][\t ]*\]\]` var ( 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(`[ ]+([,。;:!?、,.;:!?])`) spaceAfterDisplayColon = regexp.MustCompile(`([::])[\t ]+`) ) func sanitizeDisplayAnswer(content string) string { if index := strings.LastIndex(content, "[[ref"); index >= 0 && !strings.Contains(content[index:], "]]") { content = content[:index] } 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 }