cli/pkg/markdown/markdown.go
Nikola Ristić e87b5bcaff
Add "reference" help topic (#2223)
* Add "reference" help topic

* Only print reference as a help topic

* fix for color fns, slightly generalize

* WIP for switching to markdown

* escape gt/lt

* minor

* higher wrap point

* detect terminal theme

* futz with angle brackets once more

* minor cleanup

* prepend parent commands

* rename help topic fns and add test

* Simplify reference help generation

- the `<...>` characters from command usage line are now preserved by enclosing the entire usage synopsis in a code span
- hard breaks in flag usage lines are preserved by enclosing flag usage in a code block
- TTY detection and Markdown rendering are now delayed until the user explicitly requests `gh help reference`
- `gh help reference` output is now pager-enabled

Co-authored-by: vilmibm <vilmibm@github.com>
Co-authored-by: vilmibm <vilmibm@neongrid.space>
Co-authored-by: Mislav Marohnić <mislav@github.com>
2020-11-18 12:31:36 -06:00

59 lines
1.3 KiB
Go

package markdown
import (
"os"
"strings"
"github.com/charmbracelet/glamour"
)
func Render(text, style string, baseURL string) (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
)
if err != nil {
return "", err
}
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(
glamour.WithStylePath(style),
// glamour.WithBaseURL(""), // TODO: make configurable
glamour.WithWordWrap(wrap),
)
if err != nil {
return "", err
}
return tr.Render(text)
}
func GetStyle(defaultStyle string) string {
style := fromEnv()
if style != "" && style != "auto" {
return style
}
if defaultStyle == "light" || defaultStyle == "dark" {
return defaultStyle
}
return "notty"
}
var fromEnv = func() string {
return os.Getenv("GLAMOUR_STYLE")
}