diff --git a/internal/textutils/consecutive_newlines.go b/internal/textutils/consecutive_newlines.go index dc66d9b..8f395c6 100644 --- a/internal/textutils/consecutive_newlines.go +++ b/internal/textutils/consecutive_newlines.go @@ -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")) @@ -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{} diff --git a/internal/textutils/consecutive_newlines_test.go b/internal/textutils/consecutive_newlines_test.go index 2bbb434..0b91a5d 100644 --- a/internal/textutils/consecutive_newlines_test.go +++ b/internal/textutils/consecutive_newlines_test.go @@ -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), ) } }) @@ -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") @@ -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") @@ -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") @@ -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) diff --git a/internal/textutils/escape_multiline.go b/internal/textutils/escape_multiline.go index 3b5fcf8..3f08c4b 100644 --- a/internal/textutils/escape_multiline.go +++ b/internal/textutils/escape_multiline.go @@ -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 diff --git a/internal/textutils/escape_multiline_test.go b/internal/textutils/escape_multiline_test.go index 9629d89..82e2358 100644 --- a/internal/textutils/escape_multiline_test.go +++ b/internal/textutils/escape_multiline_test.go @@ -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)) diff --git a/plugin/base/base.go b/plugin/base/base.go index 172ea8c..94e964d 100644 --- a/plugin/base/base.go +++ b/plugin/base/base.go @@ -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 } diff --git a/plugin/commonmark/render_blockquote.go b/plugin/commonmark/render_blockquote.go index 261a58f..588e89e 100644 --- a/plugin/commonmark/render_blockquote.go +++ b/plugin/commonmark/render_blockquote.go @@ -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') diff --git a/plugin/commonmark/render_heading.go b/plugin/commonmark/render_heading.go index f307c63..a350e8b 100644 --- a/plugin/commonmark/render_heading.go +++ b/plugin/commonmark/render_heading.go @@ -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) diff --git a/plugin/commonmark/render_link.go b/plugin/commonmark/render_link.go index dd29ac1..633d6bb 100644 --- a/plugin/commonmark/render_link.go +++ b/plugin/commonmark/render_link.go @@ -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 diff --git a/plugin/commonmark/render_list.go b/plugin/commonmark/render_list.go index 3a1463e..289b537 100644 --- a/plugin/commonmark/render_list.go +++ b/plugin/commonmark/render_list.go @@ -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 diff --git a/plugin/commonmark/testdata/GoldenFiles/blockquote.in.html b/plugin/commonmark/testdata/GoldenFiles/blockquote.in.html index 592a464..7a939ea 100644 --- a/plugin/commonmark/testdata/GoldenFiles/blockquote.in.html +++ b/plugin/commonmark/testdata/GoldenFiles/blockquote.in.html @@ -27,6 +27,16 @@

End Line

+
+

+ Start Line +


+ +


+ End Line +

+
+
diff --git a/plugin/commonmark/testdata/GoldenFiles/blockquote.out.md b/plugin/commonmark/testdata/GoldenFiles/blockquote.out.md index e9f42a9..4afc770 100644 --- a/plugin/commonmark/testdata/GoldenFiles/blockquote.out.md +++ b/plugin/commonmark/testdata/GoldenFiles/blockquote.out.md @@ -15,6 +15,10 @@ > > End Line +> Start Line +> +> End Line + > Paragraph 1 diff --git a/plugin/commonmark/testdata/GoldenFiles/list.in.html b/plugin/commonmark/testdata/GoldenFiles/list.in.html index 700543f..45c5412 100644 --- a/plugin/commonmark/testdata/GoldenFiles/list.in.html +++ b/plugin/commonmark/testdata/GoldenFiles/list.in.html @@ -231,6 +231,15 @@


End Line

+
  • +

    + Start Line +


    + +


    + End Line +

    +
  • diff --git a/plugin/commonmark/testdata/GoldenFiles/list.out.md b/plugin/commonmark/testdata/GoldenFiles/list.out.md index 3fd37d7..1b2452e 100644 --- a/plugin/commonmark/testdata/GoldenFiles/list.out.md +++ b/plugin/commonmark/testdata/GoldenFiles/list.out.md @@ -164,6 +164,9 @@ text between +- Start Line + + End Line - Start Line End Line