diff --git a/sections.go b/sections.go index bca603b..4b26820 100644 --- a/sections.go +++ b/sections.go @@ -26,6 +26,15 @@ func splitSections(raw []byte) [][]byte { } // joinSections reassembles sections produced by splitSections. +// Inserts a newline between sections when a non-empty section lacks a +// trailing newline, so an edited section cannot inline the next heading. func joinSections(sections [][]byte) []byte { - return bytes.Join(sections, nil) + var buf bytes.Buffer + for i, s := range sections { + buf.Write(s) + if i < len(sections)-1 && len(s) > 0 && s[len(s)-1] != '\n' { + buf.WriteByte('\n') + } + } + return buf.Bytes() }