Skip to content

Commit

Permalink
restructure use of TrimConsecutiveNewlines func
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesKaufmann committed Jan 4, 2025
1 parent d64b97a commit f6aab8f
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 30 deletions.
16 changes: 1 addition & 15 deletions internal/textutils/consecutive_newlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ import (
"unicode/utf8"
)

// TODO: replace "TrimConsecutiveNewlines" with "TrimConsecutiveNewlines+TrimUnnecessaryHardLineBreaks" in the codebase

func Alternative_TrimConsecutiveNewlines(content []byte) []byte {
out := trimConsecutiveNewlines(content)

return out
}
func TrimConsecutiveNewlines(content []byte) []byte {
content = trimConsecutiveNewlines(content)
content = TrimUnnecessaryHardLineBreaks(content)

return content
}

func TrimUnnecessaryHardLineBreaks(content []byte) []byte {
content = bytes.ReplaceAll(content, []byte(" \n\n"), []byte("\n\n"))
content = bytes.ReplaceAll(content, []byte(" \n \n"), []byte("\n\n"))
Expand All @@ -28,7 +14,7 @@ func TrimUnnecessaryHardLineBreaks(content []byte) []byte {
return content
}

func trimConsecutiveNewlines(input []byte) []byte {
func TrimConsecutiveNewlines(input []byte) []byte {
var result []byte
newlineCount := 0
spaceBuffer := []byte{}
Expand Down
16 changes: 9 additions & 7 deletions internal/textutils/consecutive_newlines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ func TestTrimConsecutiveNewlines(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := string(TrimConsecutiveNewlines([]byte(tt.input)))
if got != tt.expected {
output := TrimConsecutiveNewlines([]byte(tt.input))
output = TrimUnnecessaryHardLineBreaks(output)

if string(output) != tt.expected {
t.Errorf("\ninput: %q\nexpected: %q\ngot: %q",
tt.input, tt.expected, got,
tt.input, tt.expected, string(output),
)
}
})
Expand All @@ -85,7 +87,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
const N = 1000

t.Run("no newlines", func(t *testing.T) {
var expectedAverage float64 = 4
var expectedAverage float64 = 1

actualAverage := testing.AllocsPerRun(N, func() {
input := []byte("abc")
Expand All @@ -97,7 +99,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
}
})
t.Run("exactly two newlines", func(t *testing.T) {
var expectedAverage float64 = 4
var expectedAverage float64 = 1

actualAverage := testing.AllocsPerRun(N, func() {
input := []byte("abc\n\nabc")
Expand All @@ -109,7 +111,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
}
})
t.Run("three newlines", func(t *testing.T) {
var expectedAverage float64 = 4
var expectedAverage float64 = 1

actualAverage := testing.AllocsPerRun(N, func() {
input := []byte("abc\n\n\nabc")
Expand All @@ -121,7 +123,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
}
})
t.Run("many newlines", func(t *testing.T) {
var expectedAverage float64 = 19
var expectedAverage float64 = 16

actualAverage := testing.AllocsPerRun(N, func() {
input := bytes.Repeat([]byte("abc\n\n\n\n\n\nabc"), 1000)
Expand Down
6 changes: 0 additions & 6 deletions internal/textutils/escape_multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ var (

// EscapeMultiLine deals with multiline content inside a link or a heading.
func EscapeMultiLine(content []byte) []byte {
content = bytes.TrimSpace(content)
content = Alternative_TrimConsecutiveNewlines(content)
if len(content) == 0 {
return content
}

parts := bytes.Split(content, newlineBreak)
if len(parts) == 1 {
return content
Expand Down
3 changes: 2 additions & 1 deletion internal/textutils/escape_multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ line4`,

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
output := EscapeMultiLine([]byte(test.Text))
input := TrimConsecutiveNewlines([]byte(test.Text))
output := EscapeMultiLine(input)

if string(output) != test.Expected {
t.Errorf("expected '%s' but got '%s'", test.Expected, string(output))
Expand Down
1 change: 1 addition & 0 deletions plugin/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (b *base) postRenderTrimContent(ctx converter.Context, result []byte) []byt

// Remove too many newlines
result = textutils.TrimConsecutiveNewlines(result)
result = textutils.TrimUnnecessaryHardLineBreaks(result)

return result
}
Expand Down
1 change: 1 addition & 0 deletions plugin/commonmark/render_blockquote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (c *commonmark) renderBlockquote(ctx converter.Context, w converter.Writer,
}

content = textutils.TrimConsecutiveNewlines(content)
content = textutils.TrimUnnecessaryHardLineBreaks(content)
content = textutils.PrefixLines(content, []byte{'>', ' '})

w.WriteRune('\n')
Expand Down
3 changes: 3 additions & 0 deletions plugin/commonmark/render_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (c *commonmark) renderHeading(ctx converter.Context, w converter.Writer, n
}

if c.HeadingStyle == HeadingStyleSetext && level < 3 {
// Note: We don't want to use `TrimUnnecessaryHardLineBreaks` here,
// since `EscapeMultiLine` also takes care of newlines.
content = textutils.TrimConsecutiveNewlines(content)
content = textutils.EscapeMultiLine(content)

width := getUnderlineWidth(content, 3)
Expand Down
3 changes: 3 additions & 0 deletions plugin/commonmark/render_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (c *commonmark) renderLink(ctx converter.Context, w converter.Writer, n *ht

leftExtra, trimmed, rightExtra := textutils.SurroundingSpaces(content)

// Note: We don't want to use `TrimUnnecessaryHardLineBreaks` here,
// since `EscapeMultiLine` also takes care of newlines.
trimmed = textutils.TrimConsecutiveNewlines(trimmed)
trimmed = textutils.EscapeMultiLine(trimmed)

l.before = leftExtra
Expand Down
2 changes: 1 addition & 1 deletion plugin/commonmark/render_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c commonmark) renderListContainer(ctx converter.Context, w converter.Write
w.WriteString(getPrefix(i))

item = textutils.TrimConsecutiveNewlines(item)
// item = escape.UnEscaper(item)
item = textutils.TrimUnnecessaryHardLineBreaks(item)
item = ctx.UnEscapeContent(item)

// An item might have different lines that each
Expand Down
10 changes: 10 additions & 0 deletions plugin/commonmark/testdata/GoldenFiles/blockquote.in.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
<p>End Line</p>
</blockquote>

<blockquote>
<p>
Start Line
<br /><br /><br />
<span> </span>
<br /><br /><br />
End Line
</p>
</blockquote>


<!--large blockquote-->
<blockquote>
Expand Down
4 changes: 4 additions & 0 deletions plugin/commonmark/testdata/GoldenFiles/blockquote.out.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
>
> End Line
> Start Line
>
> End Line
<!--large blockquote-->

> Paragraph 1
Expand Down
9 changes: 9 additions & 0 deletions plugin/commonmark/testdata/GoldenFiles/list.in.html
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@
<br /><br /><br />
<p>End Line</p>
</li>
<li>
<p>
Start Line
<br /><br /><br />
<span> </span>
<br /><br /><br />
End Line
</p>
</li>
</ul>


Expand Down
3 changes: 3 additions & 0 deletions plugin/commonmark/testdata/GoldenFiles/list.out.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ text between

<!-- with breaks -->

- Start Line

End Line
- Start Line

End Line
Expand Down

0 comments on commit f6aab8f

Please sign in to comment.