Files
HxClaw/cmd/hxclaw/internal/markdown.go
Z.To 88a110e87e fix: 恢复 markdown 渲染修复,应用配置系统重构
- 恢复 e070461 修复:wrap_width=-1 禁用换行,使用 lipgloss.Print
- 应用 dd3c8a0 配置重构:字段名变更(theme/line_width/user_icon)
- 添加用户配置文件支持(~/.config/hxclaw/config.yml)
- 添加 TTS 配置到用户配置(enabled, auto)
2026-04-26 06:44:47 +08:00

108 lines
1.6 KiB
Go

package internal
import (
"os"
"strconv"
"strings"
"charm.land/glamour/v2"
"github.com/charmbracelet/x/term"
)
func RenderMarkdown(md string) string {
if md == "" {
return ""
}
style := getStyle()
wrapWidth := getWrapWidth()
r, err := glamour.NewTermRenderer(
glamour.WithStandardStyle(style),
glamour.WithWordWrap(wrapWidth),
)
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()
wrapWidth := getWrapWidth()
r, err := glamour.NewTermRenderer(
glamour.WithStandardStyle(style),
glamour.WithWordWrap(wrapWidth),
)
if err != nil {
return text
}
defer r.Close()
out, err := r.Render(text)
if err != nil {
return text
}
return out
}
func getStyle() string {
if cfg := GetProjectConfig(); cfg != nil {
if cfg.Markdown.Theme != "" {
return cfg.Markdown.Theme
}
}
if s := os.Getenv("GLAMOUR_STYLE"); s != "" {
return s
}
return "dark"
}
func getWrapWidth() int {
if cfg := GetProjectConfig(); cfg != nil {
if cfg.Markdown.LineWidth > 0 {
return cfg.Markdown.LineWidth
}
if cfg.Markdown.LineWidth < 0 {
return 0
}
}
if cols := os.Getenv("COLUMNS"); cols != "" {
if w, err := strconv.Atoi(cols); err == nil && w > 0 {
return w
}
}
if cols := os.Getenv("LINES"); cols != "" {
if w, err := strconv.Atoi(cols); err == nil && w > 0 {
return w
}
}
width, _, err := term.GetSize(0)
if err != nil || width <= 0 {
return 0
}
return width
}