diff --git a/glamour.go b/glamour.go index ad52cea..4395c26 100644 --- a/glamour.go +++ b/glamour.go @@ -28,8 +28,10 @@ const ( PinkStyle = "pink" ) -const defaultWidth = 80 -const highPriority = 1000 +const ( + defaultWidth = 80 + highPriority = 1000 +) // A TermRendererOption sets an option on a TermRenderer. type TermRendererOption func(*TermRenderer) error diff --git a/glamour_test.go b/glamour_test.go index 1540310..0448b3a 100644 --- a/glamour_test.go +++ b/glamour_test.go @@ -2,6 +2,7 @@ package glamour import ( "bytes" + "errors" "io" "os" "strings" @@ -43,7 +44,7 @@ func TestTermRendererWriter(t *testing.T) { // generate if generate { - err := os.WriteFile(testFile, b, 0644) + err := os.WriteFile(testFile, b, 0o644) if err != nil { t.Fatal(err) } @@ -167,6 +168,46 @@ func TestStyles(t *testing.T) { } } +// TestCustomStyle checks the expected errors with custom styling. We need to +// support built-in styles and custom style sheets. +func TestCustomStyle(t *testing.T) { + md := "testdata/example.md" + tests := []struct { + name string + stylePath string + err error + expected string + }{ + {name: "style exists", stylePath: "testdata/custom.style", err: nil, expected: "testdata/custom.style"}, + {name: "style doesn't exist", stylePath: "testdata/notfound.style", err: os.ErrNotExist, expected: AutoStyle}, + {name: "style is empty", stylePath: "", err: nil, expected: AutoStyle}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("GLAMOUR_STYLE", tc.stylePath) + g, err := NewTermRenderer( + WithEnvironmentConfig(), + ) + if !errors.Is(err, tc.err) { + t.Fatal(err) + } + if !errors.Is(tc.err, os.ErrNotExist) { + w, err := NewTermRenderer(WithStylePath(tc.expected)) + if err != nil { + t.Fatal(err) + } + text, _ := os.ReadFile(md) + want, err := w.RenderBytes(text) + got, err := g.RenderBytes(text) + if !bytes.Equal(want, got) { + t.Error("Wrong style used") + } + } + }) + } +} + func TestRenderHelpers(t *testing.T) { in, err := os.ReadFile(markdown) if err != nil { diff --git a/testdata/custom.style b/testdata/custom.style new file mode 100644 index 0000000..ed18a4b --- /dev/null +++ b/testdata/custom.style @@ -0,0 +1,183 @@ +{ + "document": { + "block_prefix": "\n", + "block_suffix": "\n", + "color": "252", + "margin": 2 + }, + "block_quote": { + "indent": 1, + "indent_token": "│ " + }, + "paragraph": {}, + "list": { + "level_indent": 2 + }, + "heading": { + "block_suffix": "\n", + "color": "39", + "bold": true + }, + "h1": { + "prefix": " ", + "suffix": " ", + "color": "228", + "background_color": "63", + "bold": true + }, + "h6": { + "prefix": " ", + "color": "35", + "bold": false + }, + "text": {}, + "strikethrough": { + "crossed_out": true + }, + "emph": { + "italic": true + }, + "strong": { + "bold": true + }, + "hr": { + "color": "240", + "format": "\n--------\n" + }, + "item": { + "block_prefix": "• " + }, + "enumeration": { + "block_prefix": ". " + }, + "task": { + "ticked": "[✓] ", + "unticked": "[ ] " + }, + "link": { + "color": "30", + "underline": true + }, + "link_text": { + "color": "35", + "bold": true + }, + "image": { + "color": "212", + "underline": true + }, + "image_text": { + "color": "243", + "format": "Image: {{.text}} →" + }, + "code": { + "prefix": " ", + "suffix": " ", + "color": "203", + "background_color": "236" + }, + "code_block": { + "color": "244", + "margin": 2, + "chroma": { + "text": { + "color": "#C4C4C4" + }, + "error": { + "color": "#F1F1F1", + "background_color": "#F05B5B" + }, + "comment": { + "color": "#676767" + }, + "comment_preproc": { + "color": "#FF875F" + }, + "keyword": { + "color": "#00AAFF" + }, + "keyword_reserved": { + "color": "#FF5FD2" + }, + "keyword_namespace": { + "color": "#FF5F87" + }, + "keyword_type": { + "color": "#6E6ED8" + }, + "operator": { + "color": "#EF8080" + }, + "punctuation": { + "color": "#E8E8A8" + }, + "name": { + "color": "#C4C4C4" + }, + "name_builtin": { + "color": "#FF8EC7" + }, + "name_tag": { + "color": "#B083EA" + }, + "name_attribute": { + "color": "#7A7AE6" + }, + "name_class": { + "color": "#F1F1F1", + "underline": true, + "bold": true + }, + "name_constant": {}, + "name_decorator": { + "color": "#FFFF87" + }, + "name_exception": {}, + "name_function": { + "color": "#00D787" + }, + "name_other": {}, + "literal": {}, + "literal_number": { + "color": "#6EEFC0" + }, + "literal_date": {}, + "literal_string": { + "color": "#C69669" + }, + "literal_string_escape": { + "color": "#AFFFD7" + }, + "generic_deleted": { + "color": "#FD5B5B" + }, + "generic_emph": { + "italic": true + }, + "generic_inserted": { + "color": "#00D787" + }, + "generic_strong": { + "bold": true + }, + "generic_subheading": { + "color": "#777777" + }, + "background": { + "background_color": "#373737" + } + } + }, + "table": { + "center_separator": "┼", + "column_separator": "│", + "row_separator": "─" + }, + "definition_list": {}, + "definition_term": {}, + "definition_description": { + "block_prefix": "\n🠶 " + }, + "html_block": {}, + "html_span": {} +} diff --git a/testdata/empty.style b/testdata/empty.style new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/testdata/empty.style @@ -0,0 +1 @@ +{}