Skip to content

Commit

Permalink
fix: First-line folding for block scalars (fixes #422)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 24, 2023
1 parent 5af5d3d commit 443e3aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/stringify/stringifyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ interface StringifyScalar {
type?: string
}

const getFoldOptions = (ctx: StringifyContext): FoldOptions => ({
indentAtStart: ctx.indentAtStart,
const getFoldOptions = (
ctx: StringifyContext,
isBlock: boolean
): FoldOptions => ({
indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
lineWidth: ctx.options.lineWidth,
minContentWidth: ctx.options.minContentWidth
})
Expand Down Expand Up @@ -132,7 +135,7 @@ function doubleQuotedString(value: string, ctx: StringifyContext) {
str = start ? str + json.slice(start) : json
return implicitKey
? str
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx))
: foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx, false))
}

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

function quotedString(value: string, ctx: StringifyContext) {
Expand Down Expand Up @@ -254,7 +257,7 @@ function blockString(
`${start}${value}${end}`,
indent,
FOLD_BLOCK,
getFoldOptions(ctx)
getFoldOptions(ctx, true)
)
return `${header}\n${indent}${body}`
}
Expand Down Expand Up @@ -318,7 +321,7 @@ function plainString(
}
return implicitKey
? str
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx))
: foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx, false))
}

export function stringifyString(
Expand Down
26 changes: 26 additions & 0 deletions tests/doc/foldFlowLines.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as YAML from 'yaml'
import { foldFlowLines as fold } from 'yaml/util'
import { source } from '../_utils'

const FOLD_FLOW = 'flow'
const FOLD_QUOTED = 'quoted'
Expand Down Expand Up @@ -250,6 +251,31 @@ describe('double-quoted', () => {
})
})

describe('block scalar', () => {
test('eemeli/yaml#422', () => {
const obj = {
'nginx.ingress.kubernetes.io/configuration-snippet': source`
location ~* ^/sites/aaaaaaa.aa/files/(.+) {
return 302 https://process.aaaaaaa.aa/sites/aaaaaaa.aa/files/$1;
}
location ~* ^/partner-application/cls/(.+) {
return 301 https://process.aaaaaaa.aa/partner-application/cls/$1$is_args$args;
}
`
}
expect(YAML.stringify(obj)).toBe(source`
nginx.ingress.kubernetes.io/configuration-snippet: >
location ~* ^/sites/aaaaaaa.aa/files/(.+) {
return 302 https://process.aaaaaaa.aa/sites/aaaaaaa.aa/files/$1;
}
location ~* ^/partner-application/cls/(.+) {
return 301 https://process.aaaaaaa.aa/partner-application/cls/$1$is_args$args;
}
`)
})
})

describe('end-to-end', () => {
const foldOptions = { lineWidth: 20, minContentWidth: 0 }

Expand Down

0 comments on commit 443e3aa

Please # to comment.