From 30c3aa4f5bb5a9b73821c39d0e6b5143d68830f1 Mon Sep 17 00:00:00 2001 From: Norwin Date: Mon, 5 Oct 2020 02:23:57 +0000 Subject: [PATCH] Handle Invalid Markdown (#218) markdown: use builtin dark/light theme detection handle invalid markdown Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/218 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: techknowlogick --- modules/print/issue.go | 15 ++++----------- modules/print/login.go | 11 +---------- modules/print/markdown.go | 24 ++++++++++++++++++++++++ modules/print/print.go | 9 --------- modules/print/pull.go | 15 ++++----------- 5 files changed, 33 insertions(+), 41 deletions(-) create mode 100644 modules/print/markdown.go diff --git a/modules/print/issue.go b/modules/print/issue.go index dc7a908..35a41dd 100644 --- a/modules/print/issue.go +++ b/modules/print/issue.go @@ -8,24 +8,17 @@ import ( "fmt" "code.gitea.io/sdk/gitea" - "github.com/charmbracelet/glamour" ) // IssueDetails print an issue rendered to stdout func IssueDetails(issue *gitea.Issue) { - - in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", issue.Index, + OutputMarkdown(fmt.Sprintf( + "# #%d %s (%s)\n%s created %s\n\n%s\n", + issue.Index, issue.Title, issue.State, issue.Poster.UserName, issue.Created.Format("2006-01-02 15:04:05"), issue.Body, - ) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + )) } diff --git a/modules/print/login.go b/modules/print/login.go index 4801212..e217515 100644 --- a/modules/print/login.go +++ b/modules/print/login.go @@ -10,13 +10,10 @@ import ( "time" "code.gitea.io/tea/modules/config" - - "github.com/charmbracelet/glamour" ) // LoginDetails print login entry to stdout func LoginDetails(login *config.Login) { - in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n", login.Name, login.User, @@ -31,11 +28,5 @@ func LoginDetails(login *config.Login) { } in += fmt.Sprintf("\nCreated: %s", time.Unix(login.Created, 0).Format(time.RFC822)) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + OutputMarkdown(in) } diff --git a/modules/print/markdown.go b/modules/print/markdown.go new file mode 100644 index 0000000..6c5e323 --- /dev/null +++ b/modules/print/markdown.go @@ -0,0 +1,24 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package print + +import ( + "fmt" + + "github.com/charmbracelet/glamour" +) + +// OutputMarkdown prints markdown to stdout, formatted for terminals. +// If the input could not be parsed, it is printed unformatted, the error +// is returned anyway. +func OutputMarkdown(markdown string) error { + out, err := glamour.Render(markdown, "auto") + if err != nil { + fmt.Printf(markdown) + return err + } + fmt.Print(out) + return nil +} diff --git a/modules/print/print.go b/modules/print/print.go index 21b4b87..49f6046 100644 --- a/modules/print/print.go +++ b/modules/print/print.go @@ -6,17 +6,8 @@ package print import ( "fmt" - - "github.com/muesli/termenv" ) -func getGlamourTheme() string { - if termenv.HasDarkBackground() { - return "dark" - } - return "light" -} - // formatSize get kb in int and return string func formatSize(kb int64) string { if kb < 1024 { diff --git a/modules/print/pull.go b/modules/print/pull.go index 97c4cc8..f92cc11 100644 --- a/modules/print/pull.go +++ b/modules/print/pull.go @@ -8,24 +8,17 @@ import ( "fmt" "code.gitea.io/sdk/gitea" - "github.com/charmbracelet/glamour" ) // PullDetails print an pull rendered to stdout func PullDetails(pr *gitea.PullRequest) { - - in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", pr.Index, + OutputMarkdown(fmt.Sprintf( + "# #%d %s (%s)\n%s created %s\n\n%s\n", + pr.Index, pr.Title, pr.State, pr.Poster.UserName, pr.Created.Format("2006-01-02 15:04:05"), pr.Body, - ) - out, err := glamour.Render(in, getGlamourTheme()) - if err != nil { - // TODO: better Error handling - fmt.Printf("Error:\n%v\n\n", err) - return - } - fmt.Print(out) + )) }