0

I am confused with some syntax in YAML. Say I have this code:

description:
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
  labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
  esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
  in culpa qui officia deserunt mollit anim id est laborum.

It works completely fine, setting the description to the text (or string) provided. However, I read the docs and it recommends using > with multi-line strings instead, such as:

description: >
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
  labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
  nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit
  esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
  in culpa qui officia deserunt mollit anim id est laborum.

Why is this? And is there any problem with not using this? Thanks!

radio
  • 93
  • 7
  • Asked and answered here? https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines Seems to be about whether the line breaks are retained in the string or "folded." – Marc Jun 06 '23 at 23:51
  • I do understand what `>`, `>-` and `|` mean, however I want to know the difference between using `>` and just not using anything at all. – radio Jun 06 '23 at 23:54
  • Right. And what I'm saying is that it's about whether the line breaks are retained. If you use the `>`, the line breaks will be "folded" (e.g., removed), whereas if you omit the `>`, your line breaks will be retained. – Marc Jun 07 '23 at 00:02
  • Hm, how strange. For me the line breaks are removed without the `>` too. Must be something I am doing. – radio Jun 07 '23 at 01:13
  • 1
    @radio: in case it helps: https://yaml-multiline.info/ – hakre Jun 07 '23 at 01:49
  • Thank you very much. I was just trying to figure out if there was a problem with omitting the `>` altogether. – radio Jun 07 '23 at 01:55

1 Answers1

1

Having nothing after the colon is "plain" style, which means you're not using a string at all; what comes on the following (indented) lines is more YAML syntax and can be any YAML object(s), not just a string.

Using a > after the colon gives you a block string -- all the following (indented) lines are simply concatenated into one string (after removing the indent) and then a single newline is appended.

The difference is what happens if the text contains any characters meaningful to YAML, such a : or # or " or ' or - (or some others as well). Without the > they'll be treated as the corresponding YAML syntax and give some structured stuff. With the >, they'll just be part of the string.

If you are expecting/intending a block string, it is better to use > even if leaving it off would end up with the same string. Some future edit might add a : somewhere in the string and suddenly you'd unexpectedly have a YAML object instead of a simple string.

The question linked in the comment has a lot more detail on what happens in various cases.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226