feat: 添加 Markdown 终端渲染支持(glamour)

This commit is contained in:
2026-04-12 02:26:17 +08:00
parent c1b4f59704
commit 1568c63462
6 changed files with 221 additions and 11 deletions

View File

@@ -0,0 +1,70 @@
package internal
import (
"os"
"strings"
"charm.land/glamour/v2"
)
func RenderMarkdown(md string) string {
if md == "" {
return ""
}
style := getStyle()
r, err := glamour.NewTermRenderer(
glamour.WithStandardStyle(style),
glamour.WithWordWrap(80),
)
if err != nil {
return md
}
defer r.Close()
out, err := r.Render(md)
if err != nil {
return md
}
return out
}
func RenderParagraph(text string) string {
if text == "" {
return ""
}
text = strings.TrimRight(text, "\n")
if text == "" {
return ""
}
style := getStyle()
r, err := glamour.NewTermRenderer(
glamour.WithStandardStyle(style),
glamour.WithWordWrap(80),
)
if err != nil {
return text
}
defer r.Close()
out, err := r.Render(text)
if err != nil {
return text
}
return out
}
func getStyle() string {
style := "dark"
if s := os.Getenv("GLAMOUR_STYLE"); s != "" {
style = s
}
return style
}