Skip to content

style(yaml): preserve newlines in folded block scalars#58

Open
eric-thelin wants to merge 1 commit into
diggsweden:mainfrom
eric-thelin:style/preserve-newlines-in-yaml-folded-block-scalars
Open

style(yaml): preserve newlines in folded block scalars#58
eric-thelin wants to merge 1 commit into
diggsweden:mainfrom
eric-thelin:style/preserve-newlines-in-yaml-folded-block-scalars

Conversation

@eric-thelin

@eric-thelin eric-thelin commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

From the official documentation of yamlfmt:

Option that will preserve newlines in folded block scalars (blocks
that start with >).

Without this option newlines in folded block scalars are always replaced with spaces. This defeats the purpose of using a folded block scalar.

Consider this YAML:

example_of_folded_block_scalar: >
  Lorem ipsum dolor sit amet,
  consectetur adipiscing elit,
  sed do eiusmod tempor incididunt
  ut labore et dolore magna aliqua.

The above is an example of a folded block scalar (as denoted by the > syntax). It is a way to break a long string into multiple lines for better readability. The newlines are interpreted as spaces.

However, without scan_folded_as_literal: true the linter would replace the newlines with spaces and place the whole text on a single line:

example_of_folded_block_scalar: >
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

This is counterproductive. If I wanted to put the whole thing on a single line, I would use a regular string and not a folded block scalar.

See: https://yaml.cc/tutorial/yaml-multiline-string.html
See: https://github.com/google/yamlfmt/blob/main/docs/config-file.md

Checklist

  • Changes are limited to a single goal (avoid scope creep)
  • I confirm that I have read any Contribution and Development guidelines (CONTRIBUTING and DEVELOPMENT) and are following their suggestions.
  • I confirm that I wrote and/or have the right to submit the contents of my Pull Request, by agreeing to the Developer Certificate of Origin, (adding a 'sign-off' to my commits).

From the official documentation of yamlfmt:

> Option that will preserve newlines in folded block scalars (blocks
> that start with >).

Without this option newlines in folded block scalars are always replaced
with spaces. This defeats the purpose of using a folded block scalar.

Consider this YAML:

	example_of_folded_block_scalar: >
	  Lorem ipsum dolor sit amet,
	  consectetur adipiscing elit,
	  sed do eiusmod tempor incididunt
	  ut labore et dolore magna aliqua.

The above is an example of a folded block scalar (as denoted by the `>`
syntax). It is a way to break a long string into multiple lines for
better readability. The newlines are interpreted as spaces.

However, without `scan_folded_as_literal: true` the linter would replace
the newlines with spaces and place the whole text on a single line:

	example_of_folded_block_scalar: >
	  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

This is counterproductive. If I wanted to put the whole thing on a
single line, I would use a regular string and not a folded block scalar.

See: https://yaml.cc/tutorial/yaml-multiline-string.html
See: https://github.com/google/yamlfmt/blob/main/docs/config-file.md
Signed-off-by: Eric Thelin <extern.eric.thelin@digg.se>
@eric-thelin eric-thelin force-pushed the style/preserve-newlines-in-yaml-folded-block-scalars branch from 1dadc7c to 7767ebf Compare June 5, 2026 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant