Skip to content

Commit f6aab8f

Browse files
committedJan 4, 2025·
restructure use of TrimConsecutiveNewlines func
1 parent d64b97a commit f6aab8f

13 files changed

+47
-30
lines changed
 

‎internal/textutils/consecutive_newlines.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,6 @@ import (
55
"unicode/utf8"
66
)
77

8-
// TODO: replace "TrimConsecutiveNewlines" with "TrimConsecutiveNewlines+TrimUnnecessaryHardLineBreaks" in the codebase
9-
10-
func Alternative_TrimConsecutiveNewlines(content []byte) []byte {
11-
out := trimConsecutiveNewlines(content)
12-
13-
return out
14-
}
15-
func TrimConsecutiveNewlines(content []byte) []byte {
16-
content = trimConsecutiveNewlines(content)
17-
content = TrimUnnecessaryHardLineBreaks(content)
18-
19-
return content
20-
}
21-
228
func TrimUnnecessaryHardLineBreaks(content []byte) []byte {
239
content = bytes.ReplaceAll(content, []byte(" \n\n"), []byte("\n\n"))
2410
content = bytes.ReplaceAll(content, []byte(" \n \n"), []byte("\n\n"))
@@ -28,7 +14,7 @@ func TrimUnnecessaryHardLineBreaks(content []byte) []byte {
2814
return content
2915
}
3016

31-
func trimConsecutiveNewlines(input []byte) []byte {
17+
func TrimConsecutiveNewlines(input []byte) []byte {
3218
var result []byte
3319
newlineCount := 0
3420
spaceBuffer := []byte{}

‎internal/textutils/consecutive_newlines_test.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ func TestTrimConsecutiveNewlines(t *testing.T) {
7171

7272
for _, tt := range tests {
7373
t.Run(tt.name, func(t *testing.T) {
74-
got := string(TrimConsecutiveNewlines([]byte(tt.input)))
75-
if got != tt.expected {
74+
output := TrimConsecutiveNewlines([]byte(tt.input))
75+
output = TrimUnnecessaryHardLineBreaks(output)
76+
77+
if string(output) != tt.expected {
7678
t.Errorf("\ninput: %q\nexpected: %q\ngot: %q",
77-
tt.input, tt.expected, got,
79+
tt.input, tt.expected, string(output),
7880
)
7981
}
8082
})
@@ -85,7 +87,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
8587
const N = 1000
8688

8789
t.Run("no newlines", func(t *testing.T) {
88-
var expectedAverage float64 = 4
90+
var expectedAverage float64 = 1
8991

9092
actualAverage := testing.AllocsPerRun(N, func() {
9193
input := []byte("abc")
@@ -97,7 +99,7 @@ func TestTrimConsecutiveNewlines_Allocs(t *testing.T) {
9799
}
98100
})
99101
t.Run("exactly two newlines", func(t *testing.T) {
100-
var expectedAverage float64 = 4
102+
var expectedAverage float64 = 1
101103

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

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

126128
actualAverage := testing.AllocsPerRun(N, func() {
127129
input := bytes.Repeat([]byte("abc\n\n\n\n\n\nabc"), 1000)

‎internal/textutils/escape_multiline.go

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ var (
1515

1616
// EscapeMultiLine deals with multiline content inside a link or a heading.
1717
func EscapeMultiLine(content []byte) []byte {
18-
content = bytes.TrimSpace(content)
19-
content = Alternative_TrimConsecutiveNewlines(content)
20-
if len(content) == 0 {
21-
return content
22-
}
23-
2418
parts := bytes.Split(content, newlineBreak)
2519
if len(parts) == 1 {
2620
return content

‎internal/textutils/escape_multiline_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ line4`,
8181

8282
for _, test := range tests {
8383
t.Run(test.Name, func(t *testing.T) {
84-
output := EscapeMultiLine([]byte(test.Text))
84+
input := TrimConsecutiveNewlines([]byte(test.Text))
85+
output := EscapeMultiLine(input)
8586

8687
if string(output) != test.Expected {
8788
t.Errorf("expected '%s' but got '%s'", test.Expected, string(output))

‎plugin/base/base.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (b *base) postRenderTrimContent(ctx converter.Context, result []byte) []byt
125125

126126
// Remove too many newlines
127127
result = textutils.TrimConsecutiveNewlines(result)
128+
result = textutils.TrimUnnecessaryHardLineBreaks(result)
128129

129130
return result
130131
}

‎plugin/commonmark/render_blockquote.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (c *commonmark) renderBlockquote(ctx converter.Context, w converter.Writer,
1919
}
2020

2121
content = textutils.TrimConsecutiveNewlines(content)
22+
content = textutils.TrimUnnecessaryHardLineBreaks(content)
2223
content = textutils.PrefixLines(content, []byte{'>', ' '})
2324

2425
w.WriteRune('\n')

‎plugin/commonmark/render_heading.go

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ func (c *commonmark) renderHeading(ctx converter.Context, w converter.Writer, n
115115
}
116116

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

120123
width := getUnderlineWidth(content, 3)

‎plugin/commonmark/render_link.go

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func (c *commonmark) renderLink(ctx converter.Context, w converter.Writer, n *ht
8989

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

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

9497
l.before = leftExtra

‎plugin/commonmark/render_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (c commonmark) renderListContainer(ctx converter.Context, w converter.Write
8686
w.WriteString(getPrefix(i))
8787

8888
item = textutils.TrimConsecutiveNewlines(item)
89-
// item = escape.UnEscaper(item)
89+
item = textutils.TrimUnnecessaryHardLineBreaks(item)
9090
item = ctx.UnEscapeContent(item)
9191

9292
// An item might have different lines that each

‎plugin/commonmark/testdata/GoldenFiles/blockquote.in.html

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
<p>End Line</p>
2828
</blockquote>
2929

30+
<blockquote>
31+
<p>
32+
Start Line
33+
<br /><br /><br />
34+
<span> </span>
35+
<br /><br /><br />
36+
End Line
37+
</p>
38+
</blockquote>
39+
3040

3141
<!--large blockquote-->
3242
<blockquote>

‎plugin/commonmark/testdata/GoldenFiles/blockquote.out.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
>
1616
> End Line
1717
18+
> Start Line
19+
>
20+
> End Line
21+
1822
<!--large blockquote-->
1923

2024
> Paragraph 1

‎plugin/commonmark/testdata/GoldenFiles/list.in.html

+9
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@
231231
<br /><br /><br />
232232
<p>End Line</p>
233233
</li>
234+
<li>
235+
<p>
236+
Start Line
237+
<br /><br /><br />
238+
<span> </span>
239+
<br /><br /><br />
240+
End Line
241+
</p>
242+
</li>
234243
</ul>
235244

236245

‎plugin/commonmark/testdata/GoldenFiles/list.out.md

+3
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ text between
164164

165165
<!-- with breaks -->
166166

167+
- Start Line
168+
169+
End Line
167170
- Start Line
168171

169172
End Line

0 commit comments

Comments
 (0)
Please sign in to comment.