diff --git a/glamour_test.go b/glamour_test.go index dc152a72..b6584c2e 100644 --- a/glamour_test.go +++ b/glamour_test.go @@ -3,12 +3,14 @@ package glamour import ( "bytes" "errors" + "fmt" "io" "os" + "regexp" "strings" "testing" - styles "github.com/charmbracelet/glamour/styles" + "github.com/charmbracelet/glamour/styles" "github.com/charmbracelet/x/exp/golden" ) @@ -218,3 +220,65 @@ func FuzzData(f *testing.F) { }() }) } + +func TestTableAscii(t *testing.T) { + markdown := strings.TrimSpace(` +| Header A | Header B | +| --------- | --------- | +| Cell 1 | Cell 2 | +| Cell 3 | Cell 4 | +| Cell 5 | Cell 6 | +`) + + renderer, err := NewTermRenderer( + WithStyles(styles.ASCIIStyleConfig), + WithWordWrap(80), + ) + if err != nil { + t.Fatal(err) + } + + result, err := renderer.Render(markdown) + if err != nil { + t.Fatal(err) + } + + nonAsciiRegexp := regexp.MustCompile(`[^\x00-\x7f]+`) + nonAsciiChars := nonAsciiRegexp.FindAllString(result, -1) + if len(nonAsciiChars) > 0 { + t.Errorf("Non-ASCII characters found in output: %v", nonAsciiChars) + } +} + +func ExampleASCIIStyleConfig() { + markdown := strings.TrimSpace(` +| Header A | Header B | +| --------- | --------- | +| Cell 1 | Cell 2 | +| Cell 3 | Cell 4 | +| Cell 5 | Cell 6 | +`) + + renderer, err := NewTermRenderer( + WithStyles(styles.ASCIIStyleConfig), + WithWordWrap(80), + ) + if err != nil { + return + } + + result, err := renderer.Render(markdown) + if err != nil { + return + } + result = strings.ReplaceAll(result, " ", ".") + fmt.Println(result) + + // Output: + // .............................................................................. + // ..Header.A..............................|Header.B............................. + // ..--------------------------------------|------------------------------------- + // ..Cell.1................................|Cell.2............................... + // ..Cell.3................................|Cell.4............................... + // ..Cell.5................................|Cell.6............................... +} diff --git a/styles/styles.go b/styles/styles.go index cac34dc1..8f273de4 100644 --- a/styles/styles.go +++ b/styles/styles.go @@ -122,7 +122,12 @@ var ( Margin: uintPtr(defaultMargin), }, }, - Table: ansi.StyleTable{}, + Table: ansi.StyleTable{ + StyleBlock: ansi.StyleBlock{}, // ansi.StyleBlock{Margin: uintPtr(defaultMargin)}, + CenterSeparator: stringPtr("|"), + ColumnSeparator: stringPtr("|"), + RowSeparator: stringPtr("-"), + }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n* ", },