diff --git a/src/Data/Aeson/Yaml.hs b/src/Data/Aeson/Yaml.hs index c9070c3..e6dbad2 100644 --- a/src/Data/Aeson/Yaml.hs +++ b/src/Data/Aeson/Yaml.hs @@ -158,8 +158,14 @@ encodeLines :: Int -> [Text] -> Builder encodeLines level ls = mconcat $ (prefix :) $ - intersperse (bs "\n" <> indent level) $ map (b . Text.Encoding.encodeUtf8) ls + intersperseOn (/= "") (bs "\n" <> indent level) (bs "\n") $ map Text.Encoding.encodeUtf8 ls where + intersperseOn _ _ _ [] = [] + intersperseOn cond thenClause elseClause (x:xs) = b x : map go xs + where + go x' + | cond x' = thenClause <> b x' + | otherwise = elseClause <> b x' prefix = mconcat [ bs "|" diff --git a/test/Test/Data/Aeson/Yaml.hs b/test/Test/Data/Aeson/Yaml.hs index 55c5fc4..1a10bf4 100644 --- a/test/Test/Data/Aeson/Yaml.hs +++ b/test/Test/Data/Aeson/Yaml.hs @@ -62,6 +62,7 @@ tcDataTypes = "leadingSymbol" : "!leading symbol", "asteriskString": "*", "multiLine": "The first line is followed by the\nsecond line\n", + "multiLineWithEmptyLines": "The first line is followed by an empty line\n\nthird line\n", "multiLineWithSpaces": " This has extra\n spaces at the beginning\n", "notMultiline": "This won't be\nmulti-lined", "list": ["foo", "bar", "baz"], @@ -86,6 +87,10 @@ listEmpty: [] multiLine: | The first line is followed by the second line +multiLineWithEmptyLines: | + The first line is followed by an empty line + + third line multiLineWithSpaces: |2 This has extra spaces at the beginning