Skip to content

Commit 6be0a91

Browse files
committed
fix: Default to literal block scalar if folded would overflow (fixes #585)
1 parent 108f699 commit 6be0a91

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

src/stringify/stringifyString.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -246,29 +246,37 @@ function blockString(
246246
}
247247

248248
const indentSize = indent ? '2' : '1' // root is at -1
249-
let header =
250-
(literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp
249+
// Leading | or > is added later
250+
let header = (startWithSpace ? indentSize : '') + chomp
251251
if (comment) {
252252
header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '))
253253
if (onComment) onComment()
254254
}
255255

256-
if (literal) {
257-
value = value.replace(/\n+/g, `$&${indent}`)
258-
return `${header}\n${indent}${start}${value}${end}`
256+
if (!literal) {
257+
const foldedValue = value
258+
.replace(/\n+/g, '\n$&')
259+
.replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
260+
// ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
261+
.replace(/\n+/g, `$&${indent}`)
262+
let literalFallback = false
263+
const foldOptions = getFoldOptions(ctx, true)
264+
if (blockQuote !== 'folded' && type !== Scalar.BLOCK_FOLDED) {
265+
foldOptions.onOverflow = () => {
266+
literalFallback = true
267+
}
268+
}
269+
const body = foldFlowLines(
270+
`${start}${foldedValue}${end}`,
271+
indent,
272+
FOLD_BLOCK,
273+
foldOptions
274+
)
275+
if (!literalFallback) return `>${header}\n${indent}${body}`
259276
}
260-
value = value
261-
.replace(/\n+/g, '\n$&')
262-
.replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
263-
// ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
264-
.replace(/\n+/g, `$&${indent}`)
265-
const body = foldFlowLines(
266-
`${start}${value}${end}`,
267-
indent,
268-
FOLD_BLOCK,
269-
getFoldOptions(ctx, true)
270-
)
271-
return `${header}\n${indent}${body}`
277+
278+
value = value.replace(/\n+/g, `$&${indent}`)
279+
return `|${header}\n${indent}${start}${value}${end}`
272280
}
273281

274282
function plainString(

tests/doc/foldFlowLines.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,16 @@ describe('block scalar', () => {
272272
}
273273
`)
274274
})
275+
276+
test('block scalar with very long lines (#585)', () => {
277+
const str = ('a'.repeat(30) + '\n').repeat(3)
278+
expect(YAML.stringify(str, { lineWidth: 20 })).toBe(source`
279+
|
280+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
281+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
282+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
283+
`)
284+
})
275285
})
276286

277287
describe('end-to-end', () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy