Handle Invalid Markdown (#218)
markdown: use builtin dark/light theme detection handle invalid markdown Co-authored-by: Norwin Roosen <git@nroo.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/218 Reviewed-by: 6543 <6543@noreply.gitea.io> Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
7ac3ffcc1b
commit
30c3aa4f5b
|
@ -8,24 +8,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
"github.com/charmbracelet/glamour"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// IssueDetails print an issue rendered to stdout
|
// IssueDetails print an issue rendered to stdout
|
||||||
func IssueDetails(issue *gitea.Issue) {
|
func IssueDetails(issue *gitea.Issue) {
|
||||||
|
OutputMarkdown(fmt.Sprintf(
|
||||||
in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", issue.Index,
|
"# #%d %s (%s)\n%s created %s\n\n%s\n",
|
||||||
|
issue.Index,
|
||||||
issue.Title,
|
issue.Title,
|
||||||
issue.State,
|
issue.State,
|
||||||
issue.Poster.UserName,
|
issue.Poster.UserName,
|
||||||
issue.Created.Format("2006-01-02 15:04:05"),
|
issue.Created.Format("2006-01-02 15:04:05"),
|
||||||
issue.Body,
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/tea/modules/config"
|
"code.gitea.io/tea/modules/config"
|
||||||
|
|
||||||
"github.com/charmbracelet/glamour"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoginDetails print login entry to stdout
|
// LoginDetails print login entry to stdout
|
||||||
func LoginDetails(login *config.Login) {
|
func LoginDetails(login *config.Login) {
|
||||||
|
|
||||||
in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n",
|
in := fmt.Sprintf("# %s\n\n[@%s](%s/%s)\n",
|
||||||
login.Name,
|
login.Name,
|
||||||
login.User,
|
login.User,
|
||||||
|
@ -31,11 +28,5 @@ func LoginDetails(login *config.Login) {
|
||||||
}
|
}
|
||||||
in += fmt.Sprintf("\nCreated: %s", time.Unix(login.Created, 0).Format(time.RFC822))
|
in += fmt.Sprintf("\nCreated: %s", time.Unix(login.Created, 0).Format(time.RFC822))
|
||||||
|
|
||||||
out, err := glamour.Render(in, getGlamourTheme())
|
OutputMarkdown(in)
|
||||||
if err != nil {
|
|
||||||
// TODO: better Error handling
|
|
||||||
fmt.Printf("Error:\n%v\n\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Print(out)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -6,17 +6,8 @@ package print
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/muesli/termenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getGlamourTheme() string {
|
|
||||||
if termenv.HasDarkBackground() {
|
|
||||||
return "dark"
|
|
||||||
}
|
|
||||||
return "light"
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatSize get kb in int and return string
|
// formatSize get kb in int and return string
|
||||||
func formatSize(kb int64) string {
|
func formatSize(kb int64) string {
|
||||||
if kb < 1024 {
|
if kb < 1024 {
|
||||||
|
|
|
@ -8,24 +8,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
"github.com/charmbracelet/glamour"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PullDetails print an pull rendered to stdout
|
// PullDetails print an pull rendered to stdout
|
||||||
func PullDetails(pr *gitea.PullRequest) {
|
func PullDetails(pr *gitea.PullRequest) {
|
||||||
|
OutputMarkdown(fmt.Sprintf(
|
||||||
in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", pr.Index,
|
"# #%d %s (%s)\n%s created %s\n\n%s\n",
|
||||||
|
pr.Index,
|
||||||
pr.Title,
|
pr.Title,
|
||||||
pr.State,
|
pr.State,
|
||||||
pr.Poster.UserName,
|
pr.Poster.UserName,
|
||||||
pr.Created.Format("2006-01-02 15:04:05"),
|
pr.Created.Format("2006-01-02 15:04:05"),
|
||||||
pr.Body,
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue