Skip to content

Commit

Permalink
Add accessible themes to markdown rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmcg committed Feb 12, 2025
1 parent 897a7fe commit 9ccc524
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 14 deletions.
6 changes: 5 additions & 1 deletion pkg/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down
97 changes: 84 additions & 13 deletions pkg/markdown/markdown_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9ccc524

Please sign in to comment.