Skip to content

Commit

Permalink
Fix bugs with styling when markdown blocks were indented
Browse files Browse the repository at this point in the history
  • Loading branch information
bakks committed Jan 7, 2024
1 parent d6d6e34 commit 722e0d0
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ func (this *StyleCodeblocksWriter) Write(p []byte) (n int, err error) {
this.state = STATE_ONE_TICK
} else if char == '\n' {
toWrite.WriteByte(char)
} else if char == ' ' || char == '\t' {
toWrite.WriteByte(char)
} else {
this.state = STATE_NORMAL
toWrite.WriteByte(char)
Expand Down Expand Up @@ -354,6 +356,7 @@ func (this *StyleCodeblocksWriter) Write(p []byte) (n int, err error) {
case STATE_THREE_TICKS:
if char == '\n' {
this.state = STATE_BLOCK_NEWLINE
toWrite.Write([]byte("\n"))
this.blockBuffer = new(bytes.Buffer)
} else {
// append to suffix
Expand Down Expand Up @@ -381,6 +384,9 @@ func (this *StyleCodeblocksWriter) Write(p []byte) (n int, err error) {
this.state = STATE_BLOCK_NEWLINE
toWrite.WriteByte(char)
this.blockBuffer.WriteByte(char)
} else if char == ' ' || char == '\t' {
toWrite.WriteByte(char)
this.blockBuffer.WriteByte(char)
} else {
this.state = STATE_BLOCK
toWrite.WriteByte(char)
Expand Down
96 changes: 96 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

// Sanity test
func TestStyleCodeblocksWriter(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "", "")
Expand Down Expand Up @@ -36,3 +39,96 @@ func TestStyleCodeblocksWriter(t *testing.T) {
fmt.Println(buffer.String())
}
}

// We want to now test several patterns:
// 1. 3 backticks, no language
// 2. 3 backticks, with language
// 3. 1 backtick inlined
// 4. 1 backtick inlined, at start of line
// 5. 3 backticks, indented

func Test3BackticksNoLanguage(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "NORMAL", "HIGHLIGHT")

testStr := `Hello
` + "```" + `
console.log('Hi');
` + "```" + `
Foo`

writer.Write([]byte(testStr))

expected := "Hello\n\nconsole.log('Hi');\r\x1b[38;5;231mconsole.log('Hi');\x1b[0m\nNORMAL\nFoo"

// assert buffer equals expected
assert.Equal(t, expected, buffer.String())
}

func Test3BackticksWithLanguage(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "NORMAL", "HIGHLIGHT")

testStr := `Hello
` + "```javascript" + `
console.log(1);
` + "```" + `
Foo`

writer.Write([]byte(testStr))

expected := "Hello\n\nconsole.log(1);\r\x1b[38;5;148mconsole\x1b[0m\x1b[38;5;231m.\x1b[0m\x1b[38;5;148mlog\x1b[0m\x1b[38;5;231m(\x1b[0m\x1b[38;5;141m1\x1b[0m\x1b[38;5;231m);\x1b[0m\nNORMAL\nFoo"

// assert buffer equals expected
assert.Equal(t, expected, buffer.String())
}

func Test1BacktickInlined(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "NORMAL", "HIGHLIGHT")

testStr := "Hello `console.log('Hi')` Foo"

writer.Write([]byte(testStr))

expected := "Hello HIGHLIGHTconsole.log('Hi')NORMAL Foo"

// assert buffer equals expected
assert.Equal(t, expected, buffer.String())
}

func Test1BacktickInlinedAtStartOfLine(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "NORMAL", "HIGHLIGHT")

testStr := "`console.log('Hi')` Foo"

writer.Write([]byte(testStr))

expected := "HIGHLIGHTconsole.log('Hi')NORMAL Foo"

// assert buffer equals expected
assert.Equal(t, expected, buffer.String())
}

func Test3BackticksIndented(t *testing.T) {
buffer := new(bytes.Buffer)
writer := NewStyleCodeblocksWriter(buffer, 80, "NORMAL", "HIGHLIGHT")

testStr := `Hello
` + " ```javascript" + `
console.log(1);
` + " ```" + `
Foo`

writer.Write([]byte(testStr))

expected := "Hello\n\n \n console.log(1);\r\x1b[38;5;231m \x1b[0m\x1b[38;5;148mconsole\x1b[0m\x1b[38;5;231m.\x1b[0m\x1b[38;5;148mlog\x1b[0m\x1b[38;5;231m(\x1b[0m\x1b[38;5;141m1\x1b[0m\x1b[38;5;231m);\x1b[0m\n NORMAL\nFoo"

// assert buffer equals expected
assert.Equal(t, expected, buffer.String())
}

0 comments on commit 722e0d0

Please sign in to comment.