Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add markdown glamour styles #245

Merged
merged 9 commits into from
Apr 16, 2024
101 changes: 101 additions & 0 deletions cmd/overmind-theme-dark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"document": {
"block_prefix": "\n",
"block_suffix": "\n",
"color": "#bababa",
"indent": 2
},
"block_quote": {
"italic": true,
"indent": 1,
"indent_token": "│ "
},
"paragraph": {},
"list": {
"level_indent": 2
},
"heading": {
"bold": true,
"color": "#fafafa",
"block_suffix": "\n"
},
"h1": {
"background_color": "#7a70eb",
"color": "#ffffff",
"prefix": " ",
"suffix": " "
},
"h2": {},
"h3": {
"color": "#8c8c8c"
},
"h4": {
"prefix": "#### "
},
"h5": {
"prefix": "##### "
},
"h6": {
"prefix": "###### ",
"bold": false
},
"text": {},
"strikethrough": {
"crossed_out": true
},
"emph": {
"italic": true
},
"strong": {
"bold": true
},
"hr": {
"color": "#bababa",
"format": "\n--------\n"
},
"item": {
"block_prefix": "• "
},
"enumeration": {
"block_prefix": ". "
},
"task": {
"ticked": "[✓] ",
"unticked": "[ ] "
},
"link": {
"color": "#688ede",
"underline": true,
"block_prefix": "(",
"block_suffix": ")"
},
"link_text": {
"bold": true
},
"image": {
"color": "#688ede",
"underline": true,
"block_prefix": "(",
"block_suffix": ")"
},
"image_text": {
"color": "#688ede"
},
"code": {},
"code_block": {
"margin": 4,
"theme": "solarized-light"
},
"table": {
"center_separator": "┼",
"column_separator": "│",
"row_separator": "─"
},
"definition_list": {},
"definition_term": {},
"definition_description": {
"block_prefix": "\n🠶 "
},
"html_block": {},
"html_span": {}
}
101 changes: 101 additions & 0 deletions cmd/overmind-theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"document": {
"block_prefix": "\n",
"block_suffix": "\n",
"color": "#383838",
"indent": 2
},
"block_quote": {
"italic": true,
"indent": 1,
"indent_token": "│ "
},
"paragraph": {},
"list": {
"level_indent": 2
},
"heading": {
"bold": true,
"color": "#141414",
"block_suffix": "\n"
},
"h1": {
"background_color": "#655add",
"color": "#ffffff",
"prefix": " ",
"suffix": " "
},
"h2": {},
"h3": {
"color": "#616161"
},
"h4": {
"prefix": "#### "
},
"h5": {
"prefix": "##### "
},
"h6": {
"prefix": "###### ",
"bold": false
},
"text": {},
"strikethrough": {
"crossed_out": true
},
"emph": {
"italic": true
},
"strong": {
"bold": true
},
"hr": {
"color": "#383838",
"format": "\n--------\n"
},
"item": {
"block_prefix": "• "
},
"enumeration": {
"block_prefix": ". "
},
"task": {
"ticked": "[✓] ",
"unticked": "[ ] "
},
"link": {
"color": "#4f81ee",
"underline": true,
"block_prefix": "(",
"block_suffix": ")"
},
"link_text": {
"bold": true
},
"image": {
"color": "#4f81ee",
"underline": true,
"block_prefix": "(",
"block_suffix": ")"
},
"image_text": {
"color": "#4f81ee"
},
"code": {},
"code_block": {
"margin": 4,
"theme": "solarized-light"
},
"table": {
"center_separator": "┼",
"column_separator": "│",
"row_separator": "─"
},
"definition_list": {},
"definition_term": {},
"definition_description": {
"block_prefix": "\n🠶 "
},
"html_block": {},
"html_span": {}
}
28 changes: 25 additions & 3 deletions cmd/theme.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cmd

import (
_ "embed"
"fmt"

"github.com/charmbracelet/glamour"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)

type LogoPalette struct {
Expand Down Expand Up @@ -116,10 +120,28 @@ var addedLineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(ColorPalette.
var deletedLineStyle = lipgloss.NewStyle().Background(lipgloss.Color(ColorPalette.Light.BgDanger)).Foreground(lipgloss.Color(ColorPalette.Light.LabelControl))
var containerStyle = lipgloss.NewStyle().PaddingLeft(2).PaddingTop(2)

//go:embed overmind-theme.json
var overmindTheme []byte

//go:embed overmind-theme-dark.json
var overmindThemeDark []byte

func markdownToString(markdown string) string {
r, _ := glamour.NewTermRenderer(
glamour.WithAutoStyle(),
themeToUse := overmindTheme
hasDarkBackground := termenv.HasDarkBackground()
if hasDarkBackground {
themeToUse = overmindThemeDark
}

r, err := glamour.NewTermRenderer(
glamour.WithStylesFromJSONBytes(themeToUse),
)
out, _ := r.Render(markdown)
if err != nil {
panic(fmt.Errorf("failed to initialize terminal renderer: %w", err))
}
out, err := r.Render(markdown)
if err != nil {
panic(fmt.Errorf("failed to render markdown: %w", err))
}
return out
}
15 changes: 15 additions & 0 deletions cmd/theme_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"testing"
)

func TestMarkdownToString(t *testing.T) {
markdown := `# some random markdown`
expectedOutput := "\n\x1b[38;2;255;255;255;48;2;121;112;235;1m\x1b[0m\x1b[38;2;255;255;255;48;2;121;112;235;1m\x1b[0m \x1b[38;2;255;255;255;48;2;121;112;235;1m \x1b[0m\x1b[38;2;255;255;255;48;2;121;112;235;1msome random\x1b[0m\x1b[38;2;255;255;255;48;2;121;112;235;1m markdown\x1b[0m\x1b[38;2;255;255;255;48;2;121;112;235;1m \x1b[0m\x1b[38;2;186;186;186m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[38;2;186;186;186m \x1b[0m\x1b[0m\n\x1b[0m\n"

got := markdownToString(markdown)
if got != expectedOutput {
t.Errorf("Expected %q, but got %q", expectedOutput, got)
}
}
Loading