Skip to content

Commit 84d9618

Browse files
committed
Unexport experimental markdown colors
The experimental markdown colors shouldn't be used for any arbitrary purpose outside of `x/markdown` package. This commit unexports several types and functions, leaving `x/markdown` functions as the main use cases. Additionally, there are a few additional changes to improve understanding and reduce the code involved: 1. Use of "ANSIColorCode" has been replaced in favor of "glamourStyleColor" to explain their purpose 2. Replace "ANSIColorCode.String()" and "ANSIColor.StrPtr()" for "glamourStyleColor.Code()" to simplify and clarify use over types 3. Add comments to explain importance of enumeration order 4. Expand test verifying color codes
1 parent c2b3300 commit 84d9618

File tree

2 files changed

+131
-88
lines changed

2 files changed

+131
-88
lines changed

pkg/x/markdown/accessibility.go

+41-45
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,33 @@ import (
77
"github.com/charmbracelet/glamour/styles"
88
)
99

10-
type ANSIColorCode int
10+
// glamourStyleColor represents color codes used to customize glamour style elements.
11+
type glamourStyleColor int
1112

13+
// Do not change the order of the following glamour color constants,
14+
// which matches 4-bit colors with their respective color codes.
1215
const (
13-
Black ANSIColorCode = iota
14-
Red
15-
Green
16-
Yellow
17-
Blue
18-
Magenta
19-
Cyan
20-
White
21-
BrightBlack
22-
BrightRed
23-
BrightGreen
24-
BrightYellow
25-
BrightBlue
26-
BrightMagenta
27-
BrightCyan
28-
BrightWhite
16+
black glamourStyleColor = iota
17+
red
18+
green
19+
yellow
20+
blue
21+
magenta
22+
cyan
23+
white
24+
brightBlack
25+
brightRed
26+
brightGreen
27+
brightYellow
28+
brightBlue
29+
brightMagenta
30+
brightCyan
31+
brightWhite
2932
)
3033

31-
func (a ANSIColorCode) String() string {
32-
return strconv.Itoa(int(a))
33-
}
34-
35-
func (a ANSIColorCode) StrPtr() *string {
36-
return strPtr(a.String())
34+
func (a glamourStyleColor) Code() *string {
35+
s := strconv.Itoa(int(a))
36+
return &s
3737
}
3838

3939
func AccessibleStyleConfig(theme string) ansi.StyleConfig {
@@ -51,25 +51,25 @@ func accessibleDarkStyleConfig() ansi.StyleConfig {
5151
cfg := styles.DarkStyleConfig
5252

5353
// Text color
54-
cfg.Document.StylePrimitive.Color = White.StrPtr()
54+
cfg.Document.StylePrimitive.Color = white.Code()
5555

5656
// Link colors
57-
cfg.Link.Color = BrightCyan.StrPtr()
57+
cfg.Link.Color = brightCyan.Code()
5858

5959
// Heading colors
60-
cfg.Heading.StylePrimitive.Color = BrightMagenta.StrPtr()
61-
cfg.H1.StylePrimitive.Color = BrightWhite.StrPtr()
62-
cfg.H1.StylePrimitive.BackgroundColor = BrightBlue.StrPtr()
60+
cfg.Heading.StylePrimitive.Color = brightMagenta.Code()
61+
cfg.H1.StylePrimitive.Color = brightWhite.Code()
62+
cfg.H1.StylePrimitive.BackgroundColor = brightBlue.Code()
6363

6464
// Code colors
65-
cfg.Code.BackgroundColor = BrightWhite.StrPtr()
66-
cfg.Code.Color = Red.StrPtr()
65+
cfg.Code.BackgroundColor = brightWhite.Code()
66+
cfg.Code.Color = red.Code()
6767

6868
// Image colors
69-
cfg.Image.Color = BrightMagenta.StrPtr()
69+
cfg.Image.Color = brightMagenta.Code()
7070

7171
// Horizontal rule colors
72-
cfg.HorizontalRule.Color = White.StrPtr()
72+
cfg.HorizontalRule.Color = white.Code()
7373

7474
return cfg
7575
}
@@ -78,29 +78,25 @@ func accessibleLightStyleConfig() ansi.StyleConfig {
7878
cfg := styles.LightStyleConfig
7979

8080
// Text color
81-
cfg.Document.StylePrimitive.Color = Black.StrPtr()
81+
cfg.Document.StylePrimitive.Color = black.Code()
8282

8383
// Link colors
84-
cfg.Link.Color = BrightBlue.StrPtr()
84+
cfg.Link.Color = brightBlue.Code()
8585

8686
// Heading colors
87-
cfg.Heading.StylePrimitive.Color = Magenta.StrPtr()
88-
cfg.H1.StylePrimitive.Color = BrightWhite.StrPtr()
89-
cfg.H1.StylePrimitive.BackgroundColor = Blue.StrPtr()
87+
cfg.Heading.StylePrimitive.Color = magenta.Code()
88+
cfg.H1.StylePrimitive.Color = brightWhite.Code()
89+
cfg.H1.StylePrimitive.BackgroundColor = blue.Code()
9090

9191
// Code colors
92-
cfg.Code.BackgroundColor = BrightWhite.StrPtr()
93-
cfg.Code.Color = Red.StrPtr()
92+
cfg.Code.BackgroundColor = brightWhite.Code()
93+
cfg.Code.Color = red.Code()
9494

9595
// Image colors
96-
cfg.Image.Color = Magenta.StrPtr()
96+
cfg.Image.Color = magenta.Code()
9797

9898
// Horizontal rule colors
99-
cfg.HorizontalRule.Color = White.StrPtr()
99+
cfg.HorizontalRule.Color = white.Code()
100100

101101
return cfg
102102
}
103-
104-
func strPtr(s string) *string {
105-
return &s
106-
}

pkg/x/markdown/accessibility_test.go

+90-43
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,97 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
func TestANSIColorCodeString(t *testing.T) {
11+
// TestGlamourStyleColors ensures that the resulting string color codes match the expected values.
12+
func TestGlamourStyleColors(t *testing.T) {
1213
tests := []struct {
1314
name string
14-
c ANSIColorCode
15+
c glamourStyleColor
1516
want string
1617
}{
18+
{
19+
name: "black",
20+
c: black,
21+
want: "0",
22+
},
1723
{
1824
name: "red",
19-
c: Red,
25+
c: red,
2026
want: "1",
2127
},
28+
{
29+
name: "green",
30+
c: green,
31+
want: "2",
32+
},
33+
{
34+
name: "yellow",
35+
c: yellow,
36+
want: "3",
37+
},
38+
{
39+
name: "blue",
40+
c: blue,
41+
want: "4",
42+
},
43+
{
44+
name: "magenta",
45+
c: magenta,
46+
want: "5",
47+
},
48+
{
49+
name: "cyan",
50+
c: cyan,
51+
want: "6",
52+
},
53+
{
54+
name: "white",
55+
c: white,
56+
want: "7",
57+
},
58+
{
59+
name: "bright black",
60+
c: brightBlack,
61+
want: "8",
62+
},
2263
{
2364
name: "bright red",
24-
c: BrightRed,
65+
c: brightRed,
2566
want: "9",
2667
},
27-
}
28-
for _, tt := range tests {
29-
t.Run(tt.name, func(t *testing.T) {
30-
assert.Equal(t, tt.want, tt.c.String())
31-
})
32-
}
33-
}
34-
35-
func TestANSIColorCodeStrPtr(t *testing.T) {
36-
tests := []struct {
37-
name string
38-
c ANSIColorCode
39-
want *string
40-
}{
4168
{
42-
name: "green",
43-
c: Green,
44-
want: strPtr("2"),
69+
name: "bright green",
70+
c: brightGreen,
71+
want: "10",
4572
},
4673
{
47-
name: "bright green",
48-
c: BrightGreen,
49-
want: strPtr("10"),
74+
name: "bright yellow",
75+
c: brightYellow,
76+
want: "11",
77+
},
78+
{
79+
name: "bright blue",
80+
c: brightBlue,
81+
want: "12",
82+
},
83+
{
84+
name: "bright magenta",
85+
c: brightMagenta,
86+
want: "13",
87+
},
88+
{
89+
name: "bright cyan",
90+
c: brightCyan,
91+
want: "14",
92+
},
93+
{
94+
name: "bright white",
95+
c: brightWhite,
96+
want: "15",
5097
},
5198
}
5299
for _, tt := range tests {
53100
t.Run(tt.name, func(t *testing.T) {
54-
assert.Equal(t, tt.want, tt.c.StrPtr())
101+
assert.Equal(t, tt.want, *tt.c.Code())
55102
})
56103
}
57104
}
@@ -87,31 +134,31 @@ func TestAccessibleStyleConfig(t *testing.T) {
87134

88135
func Test_accessibleDarkStyleConfig(t *testing.T) {
89136
cfg := accessibleDarkStyleConfig()
90-
assert.Equal(t, White.StrPtr(), cfg.Document.StylePrimitive.Color)
91-
assert.Equal(t, BrightCyan.StrPtr(), cfg.Link.Color)
92-
assert.Equal(t, BrightMagenta.StrPtr(), cfg.Heading.StylePrimitive.Color)
93-
assert.Equal(t, BrightWhite.StrPtr(), cfg.H1.StylePrimitive.Color)
94-
assert.Equal(t, BrightBlue.StrPtr(), cfg.H1.StylePrimitive.BackgroundColor)
95-
assert.Equal(t, BrightWhite.StrPtr(), cfg.Code.BackgroundColor)
96-
assert.Equal(t, Red.StrPtr(), cfg.Code.Color)
97-
assert.Equal(t, BrightMagenta.StrPtr(), cfg.Image.Color)
98-
assert.Equal(t, White.StrPtr(), cfg.HorizontalRule.Color)
137+
assert.Equal(t, white.Code(), cfg.Document.StylePrimitive.Color)
138+
assert.Equal(t, brightCyan.Code(), cfg.Link.Color)
139+
assert.Equal(t, brightMagenta.Code(), cfg.Heading.StylePrimitive.Color)
140+
assert.Equal(t, brightWhite.Code(), cfg.H1.StylePrimitive.Color)
141+
assert.Equal(t, brightBlue.Code(), cfg.H1.StylePrimitive.BackgroundColor)
142+
assert.Equal(t, brightWhite.Code(), cfg.Code.BackgroundColor)
143+
assert.Equal(t, red.Code(), cfg.Code.Color)
144+
assert.Equal(t, brightMagenta.Code(), cfg.Image.Color)
145+
assert.Equal(t, white.Code(), cfg.HorizontalRule.Color)
99146

100147
// Test that we haven't changed the original style
101148
assert.Equal(t, styles.DarkStyleConfig.H2, cfg.H2)
102149
}
103150

104151
func Test_accessibleLightStyleConfig(t *testing.T) {
105152
cfg := accessibleLightStyleConfig()
106-
assert.Equal(t, Black.StrPtr(), cfg.Document.StylePrimitive.Color)
107-
assert.Equal(t, BrightBlue.StrPtr(), cfg.Link.Color)
108-
assert.Equal(t, Magenta.StrPtr(), cfg.Heading.StylePrimitive.Color)
109-
assert.Equal(t, BrightWhite.StrPtr(), cfg.H1.StylePrimitive.Color)
110-
assert.Equal(t, Blue.StrPtr(), cfg.H1.StylePrimitive.BackgroundColor)
111-
assert.Equal(t, BrightWhite.StrPtr(), cfg.Code.BackgroundColor)
112-
assert.Equal(t, Red.StrPtr(), cfg.Code.Color)
113-
assert.Equal(t, Magenta.StrPtr(), cfg.Image.Color)
114-
assert.Equal(t, White.StrPtr(), cfg.HorizontalRule.Color)
153+
assert.Equal(t, black.Code(), cfg.Document.StylePrimitive.Color)
154+
assert.Equal(t, brightBlue.Code(), cfg.Link.Color)
155+
assert.Equal(t, magenta.Code(), cfg.Heading.StylePrimitive.Color)
156+
assert.Equal(t, brightWhite.Code(), cfg.H1.StylePrimitive.Color)
157+
assert.Equal(t, blue.Code(), cfg.H1.StylePrimitive.BackgroundColor)
158+
assert.Equal(t, brightWhite.Code(), cfg.Code.BackgroundColor)
159+
assert.Equal(t, red.Code(), cfg.Code.Color)
160+
assert.Equal(t, magenta.Code(), cfg.Image.Color)
161+
assert.Equal(t, white.Code(), cfg.HorizontalRule.Color)
115162

116163
// Test that we haven't changed the original style
117164
assert.Equal(t, styles.LightStyleConfig.H2, cfg.H2)

0 commit comments

Comments
 (0)