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

Prevent breaking strings mid-word #518

Open
mmkal opened this issue Feb 1, 2024 · 1 comment
Open

Prevent breaking strings mid-word #518

mmkal opened this issue Feb 1, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@mmkal
Copy link

mmkal commented Feb 1, 2024

We yamlify markdown, which often contains long-ish URLs. We would like to have the yaml not break those URLs up, so the yaml is easier to read, and so they can be searched for in our repo.

Example:

const obj = {
  "mystring": "[this is an svg of something or other](https://cdn.abc.com/2022/11/18/1668788169568.abcde-abcdefhi-302x300.svg)",
}

yaml.stringify(obj)

output:

mystring: "[this is an svg of something or
  other](https://cdn.abc.com/2022/11/18/1668788169568.abcde-abcdefhi-302x300.sv\
  g)"

Describe the solution you'd like

An option like:

yaml.scalarOptions.str.fold.breakOn = 'whitespace'

Or maybe it could allow a list of regexes, in priority order, where the default (matching behaviour now, presumably) is:

// prefer to break on whitespace, failing that, break on non-word characters, failing that break on any character.
yaml.scalarOptions.str.fold.breakOn = [/\S/, /\W/, /./]

And people like us would update it to

// prefer to break on whitespace, failing that, no matching break characters could be found , so don't break, even at `lineWidth`.
yaml.scalarOptions.str.fold.breakOn = [/\S/]

Describe alternatives you've considered

We don't want to disable or set scalarOptions.fold.lineWidth to something very high, because that'll effectively turn off folding completely. We do want folding, but we just want it to only break on whitespace.

Additional context

This doesn't just apply to markdown, we also have this problem in other strings that use long format parameters.

@mmkal mmkal added the enhancement New feature or request label Feb 1, 2024
@eemeli
Copy link
Owner

eemeli commented Feb 13, 2024

Would using the option singleQuote: true work for you? The problematic behaviour is specific to double-quoted scalars, so:

YAML.stringify(obj, { singleQuote: true })
mystring: '[this is an svg of something or
  other](https://cdn.abc.com/2022/11/18/1668788169568.abcde-abcdefhi-302x300.svg)'

If that's not sufficient, you could submit a PR for an option like doubleQuotedAllowFoldEscape that defaults to true, but it'd be nice if that could be avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants