Refactor use of glamour to allow style overrides (#3243)
* Refactor use of glamour to allow style overrides * leave the things the way they were and just expose the ability to set overrides
This commit is contained in:
parent
84f0f59779
commit
2ab073d599
10 changed files with 43 additions and 27 deletions
|
|
@ -127,7 +127,7 @@ func viewRun(opts *ViewOptions) error {
|
|||
}
|
||||
|
||||
if strings.Contains(gf.Type, "markdown") && !opts.Raw {
|
||||
rendered, err := markdown.Render(gf.Content, markdownStyle, "")
|
||||
rendered, err := markdown.Render(gf.Content, markdownStyle)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ func printHumanIssuePreview(opts *ViewOptions, issue *api.Issue) error {
|
|||
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No description provided"))
|
||||
} else {
|
||||
style := markdown.GetStyle(opts.IO.TerminalTheme())
|
||||
md, err = markdown.Render(issue.Body, style, "")
|
||||
md, err = markdown.Render(issue.Body, style)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -276,7 +276,7 @@ func reviewSurvey(io *iostreams.IOStreams, editorCommand string) (*api.PullReque
|
|||
|
||||
if len(bodyAnswers.Body) > 0 {
|
||||
style := markdown.GetStyle(io.DetectTerminalTheme())
|
||||
renderedBody, err := markdown.Render(bodyAnswers.Body, style, "")
|
||||
renderedBody, err := markdown.Render(bodyAnswers.Body, style)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ func formatComment(io *iostreams.IOStreams, comment Comment, newest bool) (strin
|
|||
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No body provided"))
|
||||
} else {
|
||||
style := markdown.GetStyle(io.TerminalTheme())
|
||||
md, err = markdown.Render(comment.Content(), style, "")
|
||||
md, err = markdown.Render(comment.Content(), style)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,7 +199,7 @@ func printHumanPrPreview(opts *ViewOptions, pr *api.PullRequest) error {
|
|||
md = fmt.Sprintf("\n %s\n\n", cs.Gray("No description provided"))
|
||||
} else {
|
||||
style := markdown.GetStyle(opts.IO.TerminalTheme())
|
||||
md, err = markdown.Render(pr.Body, style, "")
|
||||
md, err = markdown.Render(pr.Body, style)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ func renderReleaseTTY(io *iostreams.IOStreams, release *shared.Release) error {
|
|||
}
|
||||
|
||||
style := markdown.GetStyle(io.DetectTerminalTheme())
|
||||
renderedDescription, err := markdown.Render(release.Body, style, "")
|
||||
renderedDescription, err := markdown.Render(release.Body, style)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ func viewRun(opts *ViewOptions) error {
|
|||
} else if isMarkdownFile(readme.Filename) {
|
||||
var err error
|
||||
style := markdown.GetStyle(opts.IO.TerminalTheme())
|
||||
readmeContent, err = markdown.Render(readme.Content, style, readme.BaseURL)
|
||||
readmeContent, err = markdown.RenderWithBaseURL(readme.Content, style, readme.BaseURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error rendering markdown: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ func referenceHelpFn(io *iostreams.IOStreams) func(*cobra.Command, []string) {
|
|||
style = markdown.GetStyle(io.DetectTerminalTheme())
|
||||
}
|
||||
|
||||
md, err := markdown.RenderWrap(cmd.Long, style, wrapWidth)
|
||||
md, err := markdown.RenderWithWrap(cmd.Long, style, wrapWidth)
|
||||
if err != nil {
|
||||
fmt.Fprintln(io.ErrOut, err)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -7,16 +7,14 @@ import (
|
|||
"github.com/charmbracelet/glamour"
|
||||
)
|
||||
|
||||
func Render(text, style string, baseURL string) (string, error) {
|
||||
type RenderOpts []glamour.TermRendererOption
|
||||
|
||||
func render(text string, opts RenderOpts) (string, error) {
|
||||
// Glamour rendering preserves carriage return characters in code blocks, but
|
||||
// we need to ensure that no such characters are present in the output.
|
||||
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||
|
||||
tr, err := glamour.NewTermRenderer(
|
||||
glamour.WithStylePath(style),
|
||||
glamour.WithBaseURL(baseURL),
|
||||
// glamour.WithWordWrap(80), // TODO: make configurable
|
||||
)
|
||||
tr, err := glamour.NewTermRenderer(opts...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -24,21 +22,39 @@ func Render(text, style string, baseURL string) (string, error) {
|
|||
return tr.Render(text)
|
||||
}
|
||||
|
||||
func RenderWrap(text, style string, wrap int) (string, error) {
|
||||
// Glamour rendering preserves carriage return characters in code blocks, but
|
||||
// we need to ensure that no such characters are present in the output.
|
||||
text = strings.ReplaceAll(text, "\r\n", "\n")
|
||||
|
||||
tr, err := glamour.NewTermRenderer(
|
||||
func Render(text, style string) (string, error) {
|
||||
opts := RenderOpts{
|
||||
glamour.WithStylePath(style),
|
||||
// glamour.WithBaseURL(""), // TODO: make configurable
|
||||
glamour.WithWordWrap(wrap),
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tr.Render(text)
|
||||
return render(text, opts)
|
||||
}
|
||||
|
||||
func RenderWithOpts(text, style string, opts RenderOpts) (string, error) {
|
||||
defaultOpts := RenderOpts{
|
||||
glamour.WithStylePath(style),
|
||||
}
|
||||
opts = append(defaultOpts, opts...)
|
||||
|
||||
return render(text, opts)
|
||||
}
|
||||
|
||||
func RenderWithBaseURL(text, style, baseURL string) (string, error) {
|
||||
opts := RenderOpts{
|
||||
glamour.WithStylePath(style),
|
||||
glamour.WithBaseURL(baseURL),
|
||||
}
|
||||
|
||||
return render(text, opts)
|
||||
}
|
||||
|
||||
func RenderWithWrap(text, style string, wrap int) (string, error) {
|
||||
opts := RenderOpts{
|
||||
glamour.WithStylePath(style),
|
||||
glamour.WithWordWrap(wrap),
|
||||
}
|
||||
|
||||
return render(text, opts)
|
||||
}
|
||||
|
||||
func GetStyle(defaultStyle string) string {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ func Test_Render(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := Render(tt.input.text, tt.input.style, "")
|
||||
_, err := Render(tt.input.text, tt.input.style)
|
||||
if tt.output.wantsErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue