Skip to content

Commit

Permalink
issue_show: Render markdown in notes
Browse files Browse the repository at this point in the history
Just as the issue/MR description, notes can use markdown. It's a bit
odd to render it for the former and not the latter, so do just that
(unless markdown is disabled).

Besides the consistency, this improves the readability of long comments
by quite a lot.
  • Loading branch information
fmuellner committed Dec 23, 2020
1 parent b0aed83 commit b1d6c06
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
31 changes: 29 additions & 2 deletions cmd/issue_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (

"github.com/araddon/dateparse"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glamour/ansi"
"github.com/fatih/color"
"github.com/jaytaylor/html2text"
"github.com/muesli/termenv"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
Expand Down Expand Up @@ -64,7 +66,7 @@ var issueShowCmd = &cobra.Command{
log.Fatal(err)
}

PrintDiscussions(discussions, since, "issues", int(issueNum))
PrintDiscussions(discussions, since, "issues", int(issueNum), renderMarkdown)
}
},
}
Expand Down Expand Up @@ -269,7 +271,7 @@ func displayCommitDiscussion(idNum int, note *gitlab.Note) {
fmt.Println("")
}

func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr string, idNum int) {
func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr string, idNum int, renderMarkdown bool) {
NewAccessTime := time.Now().UTC()

issueEntry := fmt.Sprintf("%s%d", idstr, idNum)
Expand All @@ -288,6 +290,11 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
sinceIsSet = false
}

mdRendererNormal, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle())
mdRendererBold, _ := glamour.NewTermRenderer(
glamour.WithStyles(getBoldStyle()))

// for available fields, see
// https://godoc.org/github.com/xanzy/go-gitlab#Note
// https://godoc.org/github.com/xanzy/go-gitlab#Discussion
Expand Down Expand Up @@ -319,6 +326,7 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
OmitLinks: true,
}
noteBody, _ = html2text.FromString(noteBody, html2textOptions)
mdRenderer := mdRendererNormal
printit := color.New().PrintfFunc()
if note.System {
splitNote := strings.SplitN(noteBody, "\n", 2)
Expand All @@ -331,6 +339,9 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
`,
note.Author.Username, splitNote[0], time.Time(*note.UpdatedAt).String())
if len(splitNote) == 2 {
if renderMarkdown {
splitNote[1], _ = mdRenderer.Render(splitNote[1])
}
printit(`%s
`,
splitNote[1])
Expand All @@ -344,9 +355,13 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
indentHeader)

if time.Time(*note.UpdatedAt).After(CompareTime) {
mdRenderer = mdRendererBold
printit = color.New(color.Bold).PrintfFunc()
}

if renderMarkdown {
noteBody, _ = mdRenderer.Render(noteBody)
}
noteBody = strings.Replace(noteBody, "\n", "\n"+indentNote, -1)

printit(`%s#%d: %s %s at %s
Expand All @@ -368,6 +383,18 @@ func PrintDiscussions(discussions []*gitlab.Discussion, since string, idstr stri
}
}

func getBoldStyle() ansi.StyleConfig {
var style ansi.StyleConfig
if termenv.HasDarkBackground() {
style = glamour.DarkStyleConfig
} else {
style = glamour.LightStyleConfig
}
bold := true
style.Document.Bold = &bold
return style
}

func init() {
issueShowCmd.Flags().BoolP("no-markdown", "M", false, "don't use markdown renderer to print the issue description")
issueShowCmd.Flags().BoolP("comments", "c", false, "show comments for the issue")
Expand Down
2 changes: 1 addition & 1 deletion cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var mrShowCmd = &cobra.Command{
log.Fatal(err)
}

PrintDiscussions(discussions, since, "mr", int(mrNum))
PrintDiscussions(discussions, since, "mr", int(mrNum), renderMarkdown)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/microcosm-cc/bluemonday v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.3.3 // indirect
github.com/muesli/reflow v0.2.0 // indirect
github.com/muesli/termenv v0.7.4 // indirect
github.com/muesli/termenv v0.7.4
github.com/onsi/gomega v1.4.3 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pkg/errors v0.9.1
Expand Down

0 comments on commit b1d6c06

Please sign in to comment.