X Tutup
Skip to content

Commit 456d55e

Browse files
committed
Have a single Render function handle all Markdown rendering
1 parent 8198cce commit 456d55e

File tree

11 files changed

+56
-189
lines changed

11 files changed

+56
-189
lines changed

pkg/cmd/gist/view/view.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ func viewRun(opts *ViewOptions) error {
128128
return err
129129
}
130130

131-
theme := opts.IO.DetectTerminalTheme()
132-
markdownStyle := markdown.GetStyle(theme)
131+
opts.IO.DetectTerminalTheme()
133132
if err := opts.IO.StartPager(); err != nil {
134133
fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err)
135134
}
@@ -145,7 +144,7 @@ func viewRun(opts *ViewOptions) error {
145144
}
146145

147146
if strings.Contains(gf.Type, "markdown") && !opts.Raw {
148-
rendered, err := markdown.Render(gf.Content, markdownStyle)
147+
rendered, err := markdown.Render(gf.Content, markdown.WithIO(opts.IO))
149148
if err != nil {
150149
return err
151150
}

pkg/cmd/issue/view/view.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error {
231231
if issue.Body == "" {
232232
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No description provided"))
233233
} else {
234-
style := markdown.GetStyle(opts.IO.TerminalTheme())
235-
md, err = markdown.Render(issue.Body, style)
234+
md, err = markdown.Render(issue.Body, markdown.WithIO(opts.IO))
236235
if err != nil {
237236
return err
238237
}

pkg/cmd/pr/review/review.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ func reviewSurvey(io *iostreams.IOStreams, editorCommand string) (*api.PullReque
273273
}
274274

275275
if len(bodyAnswers.Body) > 0 {
276-
style := markdown.GetStyle(io.DetectTerminalTheme())
277-
renderedBody, err := markdown.Render(bodyAnswers.Body, style)
276+
renderedBody, err := markdown.Render(bodyAnswers.Body, markdown.WithIO(io))
278277
if err != nil {
279278
return nil, err
280279
}

pkg/cmd/pr/shared/comments.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ func formatComment(io *iostreams.IOStreams, comment Comment, newest bool) (strin
123123
if comment.Content() == "" {
124124
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No body provided"))
125125
} else {
126-
style := markdown.GetStyle(io.TerminalTheme())
127-
md, err = markdown.Render(comment.Content(), style)
126+
md, err = markdown.Render(comment.Content(), markdown.WithIO(io))
128127
if err != nil {
129128
return "", err
130129
}

pkg/cmd/pr/view/view.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,7 @@ func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error {
215215
if pr.Body == "" {
216216
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No description provided"))
217217
} else {
218-
style := markdown.GetStyle(opts.IO.TerminalTheme())
219-
md, err = markdown.Render(pr.Body, style)
218+
md, err = markdown.Render(pr.Body, markdown.WithIO(opts.IO))
220219
if err != nil {
221220
return err
222221
}

pkg/cmd/release/view/view.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ func renderReleaseTTY(io *iostreams.IOStreams, release *shared.Release) error {
141141
fmt.Fprintf(w, "%s\n", iofmt.Gray(fmt.Sprintf("%s released this %s", release.Author.Login, utils.FuzzyAgo(time.Since(*release.PublishedAt)))))
142142
}
143143

144-
style := markdown.GetStyle(io.TerminalTheme())
145-
renderedDescription, err := markdown.Render(release.Body, style)
144+
renderedDescription, err := markdown.Render(release.Body, markdown.WithIO(io))
146145
if err != nil {
147146
return err
148147
}

pkg/cmd/repo/view/view.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ func viewRun(opts *ViewOptions) error {
187187
readmeContent = cs.Gray("This repository does not have a README")
188188
} else if isMarkdownFile(readme.Filename) {
189189
var err error
190-
style := markdown.GetStyle(opts.IO.TerminalTheme())
191-
readmeContent, err = markdown.RenderWithBaseURL(readme.Content, style, readme.BaseURL)
190+
readmeContent, err = markdown.Render(readme.Content, markdown.WithIO(opts.IO), markdown.WithBaseURL(readme.BaseURL))
192191
if err != nil {
193192
return fmt.Errorf("error rendering markdown: %w", err)
194193
}

pkg/cmd/root/help_reference.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import (
1414
func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) {
1515
return func(cmd *cobra.Command, args []string) {
1616
wrapWidth := 0
17-
style := "notty"
1817
if io.IsStdoutTTY() {
18+
io.DetectTerminalTheme()
1919
wrapWidth = io.TerminalWidth()
20-
style = markdown.GetStyle(io.DetectTerminalTheme())
2120
}
2221

23-
md, err := markdown.RenderWithWrap(cmd.Long, style, wrapWidth)
22+
md, err := markdown.Render(cmd.Long, markdown.WithIO(io), markdown.WithWrap(wrapWidth))
2423
if err != nil {
2524
fmt.Fprintln(io.ErrOut, err)
2625
return

pkg/cmd/workflow/view/view.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
156156

157157
yaml := string(yamlBytes)
158158

159-
theme := opts.IO.DetectTerminalTheme()
160-
markdownStyle := markdown.GetStyle(theme)
159+
opts.IO.DetectTerminalTheme()
161160
if err := opts.IO.StartPager(); err != nil {
162161
fmt.Fprintf(opts.IO.ErrOut, "starting pager failed: %v\n", err)
163162
}
@@ -172,11 +171,7 @@ func viewWorkflowContent(opts *ViewOptions, client *api.Client, workflow *shared
172171
fmt.Fprintf(out, "ID: %s", cs.Cyanf("%d", workflow.ID))
173172

174173
codeBlock := fmt.Sprintf("```yaml\n%s\n```", yaml)
175-
rendered, err := markdown.RenderWithOpts(codeBlock, markdownStyle,
176-
markdown.RenderOpts{
177-
markdown.WithoutIndentation(),
178-
markdown.WithoutWrap(),
179-
})
174+
rendered, err := markdown.Render(codeBlock, markdown.WithIO(opts.IO), markdown.WithoutIndentation(), markdown.WithWrap(0))
180175
if err != nil {
181176
return err
182177
}

pkg/markdown/markdown.go

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/charmbracelet/glamour"
88
)
99

10-
type RenderOpts []glamour.TermRendererOption
11-
1210
func WithoutIndentation() glamour.TermRendererOption {
1311
overrides := []byte(`
1412
{
@@ -23,75 +21,42 @@ func WithoutIndentation() glamour.TermRendererOption {
2321
return glamour.WithStylesFromJSONBytes(overrides)
2422
}
2523

26-
func WithoutWrap() glamour.TermRendererOption {
27-
return glamour.WithWordWrap(0)
28-
}
29-
30-
func render(text string, opts RenderOpts) (string, error) {
31-
// Glamour rendering preserves carriage return characters in code blocks, but
32-
// we need to ensure that no such characters are present in the output.
33-
text = strings.ReplaceAll(text, "\r\n", "\n")
34-
35-
tr, err := glamour.NewTermRenderer(opts...)
36-
if err != nil {
37-
return "", err
38-
}
39-
40-
return tr.Render(text)
41-
}
42-
43-
func Render(text, style string) (string, error) {
44-
opts := RenderOpts{
45-
glamour.WithStylePath(style),
46-
glamour.WithEmoji(),
47-
}
48-
49-
return render(text, opts)
24+
func WithWrap(w int) glamour.TermRendererOption {
25+
return glamour.WithWordWrap(w)
5026
}
5127

52-
func RenderWithOpts(text, style string, opts RenderOpts) (string, error) {
53-
defaultOpts := RenderOpts{
54-
glamour.WithStylePath(style),
55-
glamour.WithEmoji(),
56-
}
57-
opts = append(defaultOpts, opts...)
58-
59-
return render(text, opts)
28+
type IOStreams interface {
29+
TerminalTheme() string
6030
}
6131

62-
func RenderWithBaseURL(text, style, baseURL string) (string, error) {
63-
opts := RenderOpts{
64-
glamour.WithStylePath(style),
65-
glamour.WithEmoji(),
66-
glamour.WithBaseURL(baseURL),
32+
func WithIO(io IOStreams) glamour.TermRendererOption {
33+
style := os.Getenv("GLAMOUR_STYLE")
34+
if style == "" || style == "auto" {
35+
theme := io.TerminalTheme()
36+
switch theme {
37+
case "light", "dark":
38+
style = theme
39+
default:
40+
style = "notty"
41+
}
6742
}
68-
69-
return render(text, opts)
43+
return glamour.WithStylePath(style)
7044
}
7145

72-
func RenderWithWrap(text, style string, wrap int) (string, error) {
73-
opts := RenderOpts{
74-
glamour.WithStylePath(style),
75-
glamour.WithEmoji(),
76-
glamour.WithWordWrap(wrap),
77-
}
78-
79-
return render(text, opts)
46+
func WithBaseURL(u string) glamour.TermRendererOption {
47+
return glamour.WithBaseURL(u)
8048
}
8149

82-
func GetStyle(defaultStyle string) string {
83-
style := fromEnv()
84-
if style != "" && style != "auto" {
85-
return style
86-
}
50+
func Render(text string, opts ...glamour.TermRendererOption) (string, error) {
51+
// Glamour rendering preserves carriage return characters in code blocks, but
52+
// we need to ensure that no such characters are present in the output.
53+
text = strings.ReplaceAll(text, "\r\n", "\n")
8754

88-
if defaultStyle == "light" || defaultStyle == "dark" {
89-
return defaultStyle
55+
opts = append(opts, glamour.WithEmoji())
56+
tr, err := glamour.NewTermRenderer(opts...)
57+
if err != nil {
58+
return "", err
9059
}
9160

92-
return "notty"
93-
}
94-
95-
var fromEnv = func() string {
96-
return os.Getenv("GLAMOUR_STYLE")
61+
return tr.Render(text)
9762
}

0 commit comments

Comments
 (0)
X Tutup