glamour

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 14 Imported by: 470

README

Glamour

Glamour Title Treatment
Latest Release GoDoc Build Status Coverage Status Go ReportCard

Stylesheet-based markdown rendering for your CLI apps.

Glamour dark style example

glamour lets you render markdown documents & templates on ANSI compatible terminals. You can create your own stylesheet or simply use one of the stylish defaults.

Usage

import "github.com/charmbracelet/glamour"

in := `# Hello World

This is a simple example of Markdown rendering with Glamour!
Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too.

Bye!
`

out, err := glamour.Render(in, "dark")
fmt.Print(out)
Hello World example
Custom Renderer
import "github.com/charmbracelet/glamour"

r, _ := glamour.NewTermRenderer(
    // detect background color and pick either the default dark or light theme
    glamour.WithAutoStyle(),
    // wrap output at specific width (default is 80)
    glamour.WithWordWrap(40),
)

out, err := r.Render(in)
fmt.Print(out)

Styles

You can find all available default styles in our gallery. Want to create your own style? Learn how!

There are a few options for using a custom style:

  1. Call glamour.Render(inputText, "desiredStyle")
  2. Set the GLAMOUR_STYLE environment variable to your desired default style or a file location for a style and call glamour.RenderWithEnvironmentConfig(inputText)
  3. Set the GLAMOUR_STYLE environment variable and pass glamour.WithEnvironmentConfig() to your custom renderer

Glamourous Projects

Check out these projects, which use glamour:

  • Glow, a markdown renderer for the command-line.
  • GitHub CLI, GitHub’s official command line tool.
  • GitLab CLI, GitLab's official command line tool.
  • Gitea CLI, Gitea's official command line tool.
  • Meteor, an easy-to-use, plugin-driven metadata collection framework.

Feedback

We’d love to hear your thoughts on this project. Feel free to drop us a note!

License

MIT


Part of Charm.

The Charm logo

Charm热爱开源 • Charm loves open source

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(in string, stylePath string) (string, error)

Render initializes a new TermRenderer and renders a markdown with a specific style.

func RenderBytes

func RenderBytes(in []byte, stylePath string) ([]byte, error)

RenderBytes initializes a new TermRenderer and renders a markdown with a specific style.

func RenderWithEnvironmentConfig added in v0.2.0

func RenderWithEnvironmentConfig(in string) (string, error)

RenderWithEnvironmentConfig initializes a new TermRenderer and renders a markdown with a specific style defined by the GLAMOUR_STYLE environment variable.

Types

type TermRenderer

type TermRenderer struct {
	// contains filtered or unexported fields
}

TermRenderer can be used to render markdown content, posing a depth of customization and styles to fit your needs.

func NewTermRenderer

func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error)

NewTermRenderer returns a new TermRenderer the given options.

func (*TermRenderer) Close

func (tr *TermRenderer) Close() error

Close must be called after writing to TermRenderer. You can then retrieve the rendered markdown by calling Read.

func (*TermRenderer) Read

func (tr *TermRenderer) Read(b []byte) (int, error)

func (*TermRenderer) Render

func (tr *TermRenderer) Render(in string) (string, error)

Render returns the markdown rendered into a string.

func (*TermRenderer) RenderBytes

func (tr *TermRenderer) RenderBytes(in []byte) ([]byte, error)

RenderBytes returns the markdown rendered into a byte slice.

func (*TermRenderer) Write

func (tr *TermRenderer) Write(b []byte) (int, error)

type TermRendererOption

type TermRendererOption func(*TermRenderer) error

A TermRendererOption sets an option on a TermRenderer.

func WithAutoStyle added in v0.2.0

func WithAutoStyle() TermRendererOption

WithAutoStyle sets a TermRenderer's styles with either the standard dark or light style, depending on the terminal's background color at run-time.

func WithBaseURL

func WithBaseURL(baseURL string) TermRendererOption

WithBaseURL sets a TermRenderer's base URL.

func WithColorProfile added in v0.2.0

func WithColorProfile(profile termenv.Profile) TermRendererOption

WithColorProfile sets the TermRenderer's color profile (TrueColor / ANSI256 / ANSI).

func WithEmoji added in v0.3.0

func WithEmoji() TermRendererOption

WithEmoji sets a TermRenderer's emoji rendering.

func WithEnvironmentConfig added in v0.2.0

func WithEnvironmentConfig() TermRendererOption

WithEnvironmentConfig sets a TermRenderer's styles based on the GLAMOUR_STYLE environment variable.

func WithPreservedNewLines added in v0.4.0

func WithPreservedNewLines() TermRendererOption

WithPreservedNewlines preserves newlines from being replaced.

func WithStandardStyle

func WithStandardStyle(style string) TermRendererOption

WithStandardStyle sets a TermRenderer's styles with a standard (builtin) style.

func WithStylePath

func WithStylePath(stylePath string) TermRendererOption

WithStylePath sets a TermRenderer's style from stylePath. stylePath is first interpreted as a filename. If no such file exists, it is re-interpreted as a standard style.

func WithStyles

func WithStyles(styles ansi.StyleConfig) TermRendererOption

WithStyles sets a TermRenderer's styles.

func WithStylesFromJSONBytes

func WithStylesFromJSONBytes(jsonBytes []byte) TermRendererOption

WithStylesFromJSONBytes sets a TermRenderer's styles by parsing styles from jsonBytes.

func WithStylesFromJSONFile

func WithStylesFromJSONFile(filename string) TermRendererOption

WithStylesFromJSONFile sets a TermRenderer's styles from a JSON file.

func WithWordWrap

func WithWordWrap(wordWrap int) TermRendererOption

WithWordWrap sets a TermRenderer's word wrap.

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL