mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 08:57:14 -04:00
GLAMOUR_STYLE environment variable support (#69)
This commit is contained in:
+22
@@ -36,6 +36,13 @@ func Render(in string, stylePath string) (string, error) {
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
// RenderWithEnvironmentConfig initializes a new TermRenderer and renders a
|
||||
// markdown with a specific style defined by the GLAMOUR_STYLE environment variable.
|
||||
func RenderWithEnvironmentConfig(in string) (string, error) {
|
||||
b, err := RenderBytes([]byte(in), getEnvironmentStyle())
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
// RenderBytes initializes a new TermRenderer and renders a markdown with a
|
||||
// specific style.
|
||||
func RenderBytes(in []byte, stylePath string) ([]byte, error) {
|
||||
@@ -117,6 +124,12 @@ func WithAutoStyle() TermRendererOption {
|
||||
return WithStandardStyle("auto")
|
||||
}
|
||||
|
||||
// WithEnvironmentConfig sets a TermRenderer's styles based on the
|
||||
// GLAMOUR_STYLE environment variable.
|
||||
func WithEnvironmentConfig() TermRendererOption {
|
||||
return WithStylePath(getEnvironmentStyle())
|
||||
}
|
||||
|
||||
// WithStylePath sets a TermRenderer's style from stylePath. stylePath is first
|
||||
// interpreted as a filename. If no such file exists, it is re-interpreted as a
|
||||
// standard style.
|
||||
@@ -207,6 +220,15 @@ func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error) {
|
||||
return buf.Bytes(), err
|
||||
}
|
||||
|
||||
func getEnvironmentStyle() string {
|
||||
glamourStyle := os.Getenv("GLAMOUR_STYLE")
|
||||
if len(glamourStyle) == 0 {
|
||||
glamourStyle = "auto"
|
||||
}
|
||||
|
||||
return glamourStyle
|
||||
}
|
||||
|
||||
func getDefaultStyle(style string) (*ansi.StyleConfig, error) {
|
||||
if style == "auto" {
|
||||
if termenv.HasDarkBackground() {
|
||||
|
||||
Reference in New Issue
Block a user