Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 443e3aa

Browse files
committedApr 24, 2023
fix: First-line folding for block scalars (fixes #422)
1 parent 5af5d3d commit 443e3aa

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed
 

‎src/stringify/stringifyString.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ interface StringifyScalar {
1515
type?: string
1616
}
1717

18-
const getFoldOptions = (ctx: StringifyContext): FoldOptions => ({
19-
indentAtStart: ctx.indentAtStart,
18+
const getFoldOptions = (
19+
ctx: StringifyContext,
20+
isBlock: boolean
21+
): FoldOptions => ({
22+
indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
2023
lineWidth: ctx.options.lineWidth,
2124
minContentWidth: ctx.options.minContentWidth
2225
})
@@ -132,7 +135,7 @@ function doubleQuotedString(value: string, ctx: StringifyContext) {
132135
str = start ? str + json.slice(start) : json
133136
return implicitKey
134137
? str
135-
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx))
138+
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx, false))
136139
}
137140

138141
function singleQuotedString(value: string, ctx: StringifyContext) {
@@ -148,7 +151,7 @@ function singleQuotedString(value: string, ctx: StringifyContext) {
148151
"'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'"
149152
return ctx.implicitKey
150153
? res
151-
: foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx))
154+
: foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx, false))
152155
}
153156

154157
function quotedString(value: string, ctx: StringifyContext) {
@@ -254,7 +257,7 @@ function blockString(
254257
`${start}${value}${end}`,
255258
indent,
256259
FOLD_BLOCK,
257-
getFoldOptions(ctx)
260+
getFoldOptions(ctx, true)
258261
)
259262
return `${header}\n${indent}${body}`
260263
}
@@ -318,7 +321,7 @@ function plainString(
318321
}
319322
return implicitKey
320323
? str
321-
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx))
324+
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx, false))
322325
}
323326

324327
export function stringifyString(

‎tests/doc/foldFlowLines.js

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as YAML from 'yaml'
22
import { foldFlowLines as fold } from 'yaml/util'
3+
import { source } from '../_utils'
34

45
const FOLD_FLOW = 'flow'
56
const FOLD_QUOTED = 'quoted'
@@ -250,6 +251,31 @@ describe('double-quoted', () => {
250251
})
251252
})
252253

254+
describe('block scalar', () => {
255+
test('eemeli/yaml#422', () => {
256+
const obj = {
257+
'nginx.ingress.kubernetes.io/configuration-snippet': source`
258+
location ~* ^/sites/aaaaaaa.aa/files/(.+) {
259+
return 302 https://process.aaaaaaa.aa/sites/aaaaaaa.aa/files/$1;
260+
}
261+
location ~* ^/partner-application/cls/(.+) {
262+
return 301 https://process.aaaaaaa.aa/partner-application/cls/$1$is_args$args;
263+
}
264+
`
265+
}
266+
expect(YAML.stringify(obj)).toBe(source`
267+
nginx.ingress.kubernetes.io/configuration-snippet: >
268+
location ~* ^/sites/aaaaaaa.aa/files/(.+) {
269+
return 302 https://process.aaaaaaa.aa/sites/aaaaaaa.aa/files/$1;
270+
}
271+
272+
location ~* ^/partner-application/cls/(.+) {
273+
return 301 https://process.aaaaaaa.aa/partner-application/cls/$1$is_args$args;
274+
}
275+
`)
276+
})
277+
})
278+
253279
describe('end-to-end', () => {
254280
const foldOptions = { lineWidth: 20, minContentWidth: 0 }
255281

0 commit comments

Comments
 (0)
Please sign in to comment.