feat: 添加 Markdown 终端渲染支持(glamour)
This commit is contained in:
70
cmd/hxclaw/internal/markdown.go
Normal file
70
cmd/hxclaw/internal/markdown.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user