diff --git a/pkg/markdown/markdown.go b/pkg/markdown/markdown.go index ace466b..6960b93 100644 --- a/pkg/markdown/markdown.go +++ b/pkg/markdown/markdown.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/charmbracelet/glamour" + "github.com/cli/go-gh/v2/pkg/accessibility" ) // WithoutIndentation is a rendering option that removes indentation from the markdown rendering. @@ -33,15 +34,18 @@ func WithWrap(w int) glamour.TermRendererOption { // If the environment variable GLAMOUR_STYLE is set, it will take precedence over the provided theme. func WithTheme(theme string) glamour.TermRendererOption { style := os.Getenv("GLAMOUR_STYLE") + accessible := accessibility.IsEnabled() if style == "" || style == "auto" { switch theme { case "light", "dark": + if accessible { + return glamour.WithStyles(AccessibleStyleConfig(theme)) + } style = theme default: style = "notty" } } - return glamour.WithStylePath(style) } diff --git a/pkg/markdown/markdown_test.go b/pkg/markdown/markdown_test.go index f131b42..7de9de7 100644 --- a/pkg/markdown/markdown_test.go +++ b/pkg/markdown/markdown_test.go @@ -1,38 +1,109 @@ package markdown import ( + "fmt" + "os" + "path/filepath" "testing" + "github.com/cli/go-gh/v2/pkg/accessibility" "github.com/stretchr/testify/assert" ) +// glamour theme colors found at https://github.com/charmbracelet/glamour/tree/master/styles +const ( + glamourLightH2Color = "27" + glamourDarkH2Color = "39" + customH2Color = "61" + magentaEscapeCode = "35" + brightMagentaEscapeCode = "95" +) + func Test_Render(t *testing.T) { t.Setenv("GLAMOUR_STYLE", "") tests := []struct { - name string - text string - theme string + name string + text string + theme string + styleEnvVar string + accessibleEnvVar string + wantOut string + only bool }{ { - name: "light style", - text: "some text", - theme: "light", + name: "light style", + text: "## h2", + theme: "light", + wantOut: fmt.Sprintf("\x1b[0m\x1b[38;5;%s;1mh2", glamourLightH2Color), + }, + { + name: "dark style", + text: "## h2", + theme: "dark", + wantOut: fmt.Sprintf("\x1b[0m\x1b[38;5;%s;1mh2", glamourDarkH2Color), }, { - name: "dark style", - text: "some text", - theme: "dark", + name: "notty style", + text: "## h2", + theme: "none", + wantOut: "## h2", // no tty should maintain the original text }, { - name: "notty style", - text: "some text", - theme: "none", + name: "when the style env var is set, we override the theme whith that style", + text: "## h2", + theme: "light", + styleEnvVar: "customStyle", + wantOut: fmt.Sprintf("\x1b[0m\x1b[38;5;%s;1mh2", customH2Color), + }, + { + name: "when the accessible env var is set and the light theme is selected, we use the light accessible style", + text: "## h2", + theme: "light", + accessibleEnvVar: "true", + wantOut: fmt.Sprintf("\x1b[0m\x1b[%s;1mh2", magentaEscapeCode), + only: true, + }, + { + name: "when the accessible env var is set and the dark theme is selected, we use the dark accessible style", + text: "## h2", + theme: "dark", + accessibleEnvVar: "true", + wantOut: fmt.Sprintf("\x1b[0m\x1b[%s;1mh2", brightMagentaEscapeCode), + only: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := Render(tt.text, WithTheme(tt.theme)) + if !tt.only { + t.Skip() + } + t.Setenv(accessibility.ACCESSIBILITY_ENV, tt.accessibleEnvVar) + + if tt.styleEnvVar != "" { + tmpDir := t.TempDir() + path := filepath.Join(tmpDir, fmt.Sprintf("%s.json", tt.styleEnvVar)) + os.WriteFile(path, []byte(customGlamourStyle(t)), 0644) + t.Setenv("GLAMOUR_STYLE", path) + } + + out, err := Render(tt.text, WithTheme(tt.theme)) assert.NoError(t, err) + assert.Contains(t, out, tt.wantOut) }) } } + +func customGlamourStyle(t *testing.T) string { + t.Helper() + return fmt.Sprintf(` +{ + "heading": { + "block_suffix": "\n", + "color": "%s", + "bold": true + }, + "h2": { + "prefix": "## " + } +}`, customH2Color) +}