Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package markdown facilitates rendering markdown in the terminal. | ||
package markdown | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/charmbracelet/glamour" | ||
) | ||
|
||
// WithoutIndentation is a rendering option that removes indentation from the markdown rendering. | ||
func WithoutIndentation() glamour.TermRendererOption { | ||
overrides := []byte(` | ||
{ | ||
"document": { | ||
"margin": 0 | ||
}, | ||
"code_block": { | ||
"margin": 0 | ||
} | ||
}`) | ||
|
||
return glamour.WithStylesFromJSONBytes(overrides) | ||
} | ||
|
||
// WithoutWrap is a rendering option that set the character limit for soft wraping the markdown rendering. | ||
func WithWrap(w int) glamour.TermRendererOption { | ||
return glamour.WithWordWrap(w) | ||
} | ||
|
||
func WithTheme(theme string) glamour.TermRendererOption { | ||
style := os.Getenv("GLAMOUR_STYLE") | ||
if style == "" || style == "auto" { | ||
switch theme { | ||
case "light", "dark": | ||
style = theme | ||
default: | ||
style = "notty" | ||
} | ||
} | ||
return glamour.WithStylePath(style) | ||
} | ||
|
||
// WithBaseURL is a rendering option that sets the base URL to use when rendering relative URLs. | ||
func WithBaseURL(u string) glamour.TermRendererOption { | ||
return glamour.WithBaseURL(u) | ||
} | ||
|
||
// Render the markdown string according to the specified rendering options. | ||
// By default emoji are rendered and new lines are preserved. | ||
func Render(text string, opts ...glamour.TermRendererOption) (string, error) { | ||
// Glamour rendering preserves carriage return characters in code blocks, but | ||
// we need to ensure that no such characters are present in the output. | ||
text = strings.ReplaceAll(text, "\r\n", "\n") | ||
opts = append(opts, glamour.WithEmoji(), glamour.WithPreservedNewLines()) | ||
tr, err := glamour.NewTermRenderer(opts...) | ||
if err != nil { | ||
return "", err | ||
} | ||
return tr.Render(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package markdown | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Render(t *testing.T) { | ||
t.Setenv("GLAMOUR_STYLE", "") | ||
tests := []struct { | ||
name string | ||
text string | ||
theme string | ||
}{ | ||
{ | ||
name: "light style", | ||
text: "some text", | ||
theme: "light", | ||
}, | ||
{ | ||
name: "dark style", | ||
text: "some text", | ||
theme: "dark", | ||
}, | ||
{ | ||
name: "notty style", | ||
text: "some text", | ||
theme: "none", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := Render(tt.text, WithTheme(tt.theme)) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters