32 lines
1.2 KiB
Go
32 lines
1.2 KiB
Go
package dingtalk
|
||
|
||
import (
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
const displayReferenceMarkerPattern = `\[\[ref[\t ]+id[\t ]*=[\t ]*["']c[0-9]+["'][\t ]*\]\]`
|
||
|
||
var (
|
||
displayReferenceMarkerRE = regexp.MustCompile(displayReferenceMarkerPattern)
|
||
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) {
|
||
return content
|
||
}
|
||
content = leadingReferenceGroupColonRE.ReplaceAllString(content, "")
|
||
content = spacedReferenceGroupRE.ReplaceAllString(content, " ")
|
||
content = displayReferenceMarkerRE.ReplaceAllString(content, "")
|
||
content = spaceBeforeDisplayPunctuation.ReplaceAllString(content, "$1")
|
||
content = spaceAfterDisplayColon.ReplaceAllString(content, "$1")
|
||
return content
|
||
}
|