Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flawed line wrapping for strings with newlines #529

Closed
elmuerte opened this issue Mar 5, 2024 · 1 comment
Closed

Flawed line wrapping for strings with newlines #529

elmuerte opened this issue Mar 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@elmuerte
Copy link

elmuerte commented Mar 5, 2024

Describe the bug
Wrapping of long strings does not work correctly. After a newline is encountered in the value it will no longer wrap any more content.

Further more, if the line length becomes small enough it inserts a leading newline and doesn't even wrap at the correct line length.

To Reproduce

import yaml from "yaml";

const data = {
	foo: "bar",
	longlines:
		"first line which is long enough to be wrapped to a another line\nsecond line which is long enough to be wrapped to a another line\nthird line which is long enough to be wrapped to a another line",
};

console.log(
	yaml.stringify(data, {
		lineWidth: 40,
	}),
);

// Inserts a leading newline
console.log(
	yaml.stringify(data, {
		lineWidth: 20,
	}),
);

This will print

foo: bar
longlines: >-
  first line which is long enough to be
  wrapped to a another line

  second line which is long enough to be wrapped to a another line

  third line which is long enough to be wrapped to a another line

foo: bar
longlines: >-

  first line which
  is long enough to be
  wrapped to a another
  line

  second line which is long enough to be wrapped to a another line

  third line which is long enough to be wrapped to a another line

Expected behaviour

foo: bar
longlines: >-
  first line which is long enough to be
  wrapped to a another line

  second line which is long enough to
  be wrapped to a another line

  third line which is long enough to be
  wrapped to a another line

foo: bar
longlines: >-
  first line which
  is long enough
  to be wrapped to
  a another line

  second line which
  is long enough to
  be wrapped to a
  another line

  third line which
  is long enough to
  be wrapped to a 
  another line

Versions (please complete the following information):

  • Environment: Node.js v18.17.0
  • yaml: 2.4.0

Additional context
Add any other context about the problem here.

@elmuerte elmuerte added the bug Something isn't working label Mar 5, 2024
@eemeli eemeli closed this as completed in 46a816e Mar 6, 2024
@eemeli
Copy link
Owner

eemeli commented Mar 6, 2024

This was due to mismatching expectations in when indentation is applied to indented scalar values. For > block scalars, lines within the value that are more indented that the others are not folded, so lines starting with whitespace need to be skipped. However, by the time we're doing this, a scalar value inside a block collection will already have been indented, so after the first line we were mistakenly detecting every line as more-indented from the PoV of folding, leading to the observed results.

This also revealed a slight offset in the cutoff for the first soft wrap after a block scalar newline that's also fixed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants