Files
cc-connect/core/markdown_html.go
T
chenhg5andClaude e40d033c3f feat(engine): enhance session switching and markdown conversion
- Improved `/switch` command to support session names and better matching logic
- Refactored markdown-to-HTML conversion with proper tag nesting and quote escaping
- Added detailed logging for streaming preview operations
- Fixed HTML tag validation in markdown tests
- Updated Telegram platform to handle HTML parsing errors gracefully

generated by llmgit

Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-06 10:30:29 +08:00

242 lines
6.3 KiB
Go

package core
import (
"regexp"
"strings"
)
// MarkdownToTelegramHTML converts common Markdown to Telegram-compatible HTML.
// Telegram supports: <b>, <i>, <s>, <code>, <pre>, <a href="">, <blockquote>.
// This is more reliable than Telegram's native Markdown parser which frequently
// fails on content with underscores, asterisks in code, etc.
func MarkdownToTelegramHTML(md string) string {
var b strings.Builder
b.Grow(len(md) + len(md)/4)
lines := strings.Split(md, "\n")
inCodeBlock := false
codeLang := ""
var codeLines []string
for i, line := range lines {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "```") {
if !inCodeBlock {
inCodeBlock = true
codeLang = strings.TrimPrefix(trimmed, "```")
codeLines = nil
} else {
inCodeBlock = false
if codeLang != "" {
b.WriteString("<pre><code class=\"language-" + escapeHTML(codeLang) + "\">")
} else {
b.WriteString("<pre><code>")
}
b.WriteString(escapeHTML(strings.Join(codeLines, "\n")))
b.WriteString("</code></pre>")
if i < len(lines)-1 {
b.WriteByte('\n')
}
}
continue
}
if inCodeBlock {
codeLines = append(codeLines, line)
continue
}
// Headings → bold
if heading := reHeading.FindString(line); heading != "" {
rest := strings.TrimPrefix(line, heading)
b.WriteString("<b>")
b.WriteString(convertInlineHTML(rest))
b.WriteString("</b>")
} else if strings.HasPrefix(trimmed, "> ") || trimmed == ">" {
quote := strings.TrimPrefix(line, "> ")
if quote == ">" {
quote = ""
}
b.WriteString("<blockquote>")
b.WriteString(convertInlineHTML(quote))
b.WriteString("</blockquote>")
} else if reHorizontal.MatchString(trimmed) {
b.WriteString("———")
} else {
b.WriteString(convertInlineHTML(line))
}
if i < len(lines)-1 {
b.WriteByte('\n')
}
}
// Handle unclosed code block
if inCodeBlock && len(codeLines) > 0 {
b.WriteString("<pre><code>")
b.WriteString(escapeHTML(strings.Join(codeLines, "\n")))
b.WriteString("</code></pre>")
}
return b.String()
}
var (
reInlineCodeHTML = regexp.MustCompile("`([^`]+)`")
reBoldAstHTML = regexp.MustCompile(`\*\*(.+?)\*\*`)
reBoldUndHTML = regexp.MustCompile(`__(.+?)__`)
reItalicAstHTML = regexp.MustCompile(`(?:^|[^*])\*([^*]+?)\*(?:[^*]|$)`)
reStrikeHTML = regexp.MustCompile(`~~(.+?)~~`)
reLinkHTML = regexp.MustCompile(`\[([^\]]+)\]\(([^)]+)\)`)
)
// convertInlineHTML converts inline Markdown formatting to Telegram-compatible HTML.
//
// Each formatting pass (bold, strikethrough) protects its output as placeholders
// so that subsequent passes (italic) cannot match across HTML tag boundaries.
// Without this, input like `**bold *text***` would produce crossed tags
// `<b>bold <i>text</b></i>` which Telegram rejects.
func convertInlineHTML(s string) string {
type placeholder struct {
key string
html string
}
var phs []placeholder
phIdx := 0
nextPH := func(html string) string {
key := "\x00PH" + string(rune('0'+phIdx)) + "\x00"
phs = append(phs, placeholder{key: key, html: html})
phIdx++
return key
}
// 1. Extract inline code → placeholder (content escaped)
s = reInlineCodeHTML.ReplaceAllStringFunc(s, func(m string) string {
inner := m[1 : len(m)-1]
return nextPH("<code>" + escapeHTML(inner) + "</code>")
})
// 2. Extract links → placeholder (text & URL escaped)
s = reLinkHTML.ReplaceAllStringFunc(s, func(m string) string {
sm := reLinkHTML.FindStringSubmatch(m)
if len(sm) < 3 {
return m
}
return nextPH(`<a href="` + escapeHTML(sm[2]) + `">` + escapeHTML(sm[1]) + `</a>`)
})
// 3. HTML-escape the entire remaining text.
s = escapeHTML(s)
// 4. Bold → placeholder (so italic regex can't cross bold boundaries)
s = reBoldAstHTML.ReplaceAllStringFunc(s, func(m string) string {
inner := m[2 : len(m)-2]
return nextPH("<b>" + inner + "</b>")
})
s = reBoldUndHTML.ReplaceAllStringFunc(s, func(m string) string {
inner := m[2 : len(m)-2]
return nextPH("<b>" + inner + "</b>")
})
// 5. Strikethrough → placeholder
s = reStrikeHTML.ReplaceAllStringFunc(s, func(m string) string {
inner := m[2 : len(m)-2]
return nextPH("<s>" + inner + "</s>")
})
// 6. Italic (applied last, on text with bold/strike already protected)
s = reItalicAstHTML.ReplaceAllStringFunc(s, func(m string) string {
idx := strings.Index(m, "*")
if idx < 0 {
return m
}
lastIdx := strings.LastIndex(m, "*")
if lastIdx <= idx {
return m
}
return m[:idx] + "<i>" + m[idx+1:lastIdx] + "</i>" + m[lastIdx+1:]
})
// 7. Restore all placeholders (may be nested, so iterate until stable)
for i := 0; i < 3; i++ {
changed := false
for _, ph := range phs {
if strings.Contains(s, ph.key) {
s = strings.Replace(s, ph.key, ph.html, 1)
changed = true
}
}
if !changed {
break
}
}
return s
}
func escapeHTML(s string) string {
s = strings.ReplaceAll(s, "&", "&amp;")
s = strings.ReplaceAll(s, "<", "&lt;")
s = strings.ReplaceAll(s, ">", "&gt;")
s = strings.ReplaceAll(s, "\"", "&quot;")
return s
}
// SplitMessageCodeFenceAware splits text into chunks respecting code fence boundaries.
// When a chunk boundary falls inside a code block, the fence is closed at the end of
// the chunk and re-opened at the start of the next chunk.
func SplitMessageCodeFenceAware(text string, maxLen int) []string {
if len(text) <= maxLen {
return []string{text}
}
lines := strings.Split(text, "\n")
var chunks []string
var current []string
currentLen := 0
openFence := "" // the ``` opening line, or "" if outside code block
for _, line := range lines {
lineLen := len(line) + 1 // +1 for newline
if currentLen+lineLen > maxLen && len(current) > 0 {
chunk := strings.Join(current, "\n")
if openFence != "" {
chunk += "\n```"
}
chunks = append(chunks, chunk)
current = nil
currentLen = 0
if openFence != "" {
current = append(current, openFence)
currentLen = len(openFence) + 1
}
}
current = append(current, line)
currentLen += lineLen
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "```") {
if openFence != "" {
openFence = ""
} else {
openFence = trimmed
}
}
}
if len(current) > 0 {
chunk := strings.Join(current, "\n")
if openFence != "" {
chunk += "\n```"
}
chunks = append(chunks, chunk)
}
return chunks
}