1
1
mirror of https://gitea.com/gitea/tea synced 2024-11-26 06:07:38 +01:00
tea/modules/print/markdown.go
techknowlogick b868d30434 spdx (#581)
Co-authored-by: techknowlogick <hello@techknowlogick.com>
Co-committed-by: techknowlogick <hello@techknowlogick.com>
2023-09-08 01:40:02 +00:00

59 lines
1.2 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package print
import (
"fmt"
"os"
"github.com/charmbracelet/glamour"
"golang.org/x/term"
)
// 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, baseURL string) error {
var styleOption glamour.TermRendererOption
if IsInteractive() {
styleOption = glamour.WithAutoStyle()
} else {
styleOption = glamour.WithStandardStyle("notty")
}
renderer, err := glamour.NewTermRenderer(
styleOption,
glamour.WithBaseURL(baseURL),
glamour.WithPreservedNewLines(),
glamour.WithWordWrap(getWordWrap()),
)
if err != nil {
fmt.Printf(markdown)
return err
}
out, err := renderer.Render(markdown)
if err != nil {
fmt.Printf(markdown)
return err
}
fmt.Print(out)
return nil
}
// stolen from https://github.com/charmbracelet/glow/blob/e9d728c/main.go#L152-L165
func getWordWrap() int {
fd := int(os.Stdout.Fd())
width := 80
if term.IsTerminal(fd) {
if w, _, err := term.GetSize(fd); err == nil {
width = w
}
}
if width > 120 {
width = 120
}
return width
}