71 lines
916 B
Go
71 lines
916 B
Go
|
|
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
|
||
|
|
}
|