testmark-go

refrain from making double linebreaks when patching with content that ends in a linebreak.

commit 62acfdb7662a5c9af3d7a10f15cae79c0708d72c · parent 43602084be · browse this tree
Eric Myhre, 2021-09-19 16:19:08 UTC

The vagaries of split behavior were not helpful here.

Unconditionally sheering the last entry of the line slice would also be
incorrect, because we generally have a tolerant view of data that
doesn't end in line breaks.  (We just have no choice but to normalize
it to ending in a line break due to the nature of markdown codeblocks.)
diff --git a/patch.go b/patch.go
index 6465ece..b505249 100644
--- a/patch.go
+++ b/patch.go
@@ -43,6 +43,10 @@ func Patch(oldDoc *Document, hunks ...Hunk) (newDoc *Document) {
 			// Split our new hunk's body into lines, ready to append to the total content lines.
 			// The rest... copy it into 'hunk', actually, it's a local variable and it makes the code slightly more DRY.
 			newBodyLines = bytes.Split(newHunk.Body, sigilLineBreak)
+			// If the last byte was a linebreak, the split will tend to exaggerate it a bit, so let's trim that back down.
+			if len(newHunk.Body) > 0 && newHunk.Body[len(newHunk.Body)-1] == '\n' {
+				newBodyLines = newBodyLines[0 : len(newBodyLines)-1]
+			}
 			hunk.BlockTag = newHunk.BlockTag
 
 			// Yeet from newHunks, as it's now handled.