115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package dingtalk
|
|
|
|
import "strings"
|
|
|
|
type markdownStreamLine struct {
|
|
start int
|
|
text string
|
|
inFence bool
|
|
}
|
|
|
|
// stableMarkdownPrefix returns the cumulative content that is safe to render
|
|
// while Markdown is still streaming. Once a table is confirmed, the table and
|
|
// every following token stay hidden until the terminal card update.
|
|
func stableMarkdownPrefix(content string) string {
|
|
lines := splitMarkdownStreamLines(content)
|
|
for i := 0; i < len(lines); i++ {
|
|
if lines[i].inFence || !isTableHeaderCandidate(lines[i].text) {
|
|
continue
|
|
}
|
|
|
|
if i == len(lines)-1 {
|
|
return content[:lines[i].start]
|
|
}
|
|
if lines[i+1].inFence || !isTableDelimiterCandidate(lines[i+1].text) {
|
|
continue
|
|
}
|
|
if !isCompleteTableDelimiter(lines[i+1].text) {
|
|
return content[:lines[i].start]
|
|
}
|
|
// DingTalk only lays out Markdown tables reliably on the terminal
|
|
// isFinalize=true update. Keep the table and every following partial
|
|
// token hidden; the authoritative final update bypasses this helper.
|
|
return content[:lines[i].start]
|
|
}
|
|
return content
|
|
}
|
|
|
|
func splitMarkdownStreamLines(content string) []markdownStreamLine {
|
|
lines := make([]markdownStreamLine, 0, strings.Count(content, "\n")+1)
|
|
fence := ""
|
|
for start := 0; start < len(content); {
|
|
end := strings.IndexByte(content[start:], '\n')
|
|
if end < 0 {
|
|
end = len(content)
|
|
} else {
|
|
end += start + 1
|
|
}
|
|
text := strings.TrimSuffix(content[start:end], "\n")
|
|
text = strings.TrimSuffix(text, "\r")
|
|
trimmed := strings.TrimSpace(text)
|
|
line := markdownStreamLine{start: start, text: text, inFence: fence != ""}
|
|
|
|
if marker := markdownFenceMarker(trimmed); marker != "" {
|
|
line.inFence = true
|
|
if fence == "" {
|
|
fence = marker
|
|
} else if marker == fence {
|
|
fence = ""
|
|
}
|
|
}
|
|
lines = append(lines, line)
|
|
start = end
|
|
}
|
|
return lines
|
|
}
|
|
|
|
func markdownFenceMarker(trimmed string) string {
|
|
switch {
|
|
case strings.HasPrefix(trimmed, "```"):
|
|
return "```"
|
|
case strings.HasPrefix(trimmed, "~~~"):
|
|
return "~~~"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func isTableHeaderCandidate(line string) bool {
|
|
return strings.HasPrefix(strings.TrimSpace(line), "|")
|
|
}
|
|
|
|
func isTableDelimiterCandidate(line string) bool {
|
|
trimmed := strings.TrimSpace(line)
|
|
if !strings.HasPrefix(trimmed, "|") {
|
|
return false
|
|
}
|
|
for _, r := range trimmed {
|
|
switch r {
|
|
case '|', ':', '-', ' ', '\t':
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func isCompleteTableDelimiter(line string) bool {
|
|
trimmed := strings.TrimSpace(line)
|
|
trimmed = strings.TrimPrefix(trimmed, "|")
|
|
trimmed = strings.TrimSuffix(trimmed, "|")
|
|
cells := strings.Split(trimmed, "|")
|
|
if len(cells) < 2 {
|
|
return false
|
|
}
|
|
for _, cell := range cells {
|
|
cell = strings.TrimSpace(cell)
|
|
cell = strings.TrimPrefix(cell, ":")
|
|
cell = strings.TrimSuffix(cell, ":")
|
|
if len(cell) < 3 || strings.Trim(cell, "-") != "" {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|