Skip to content

Commit 94bb548

Browse files
committed
Add YAML multiline support
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent ba1b251 commit 94bb548

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"swagger-autogen": "2.23.7",
3535
"swagger-ui-express": "5.0.1",
3636
"uuid": "10.0.0",
37-
"winston": "3.17.0"
37+
"winston": "3.17.0",
38+
"yaml": "^2.3.4"
3839
},
3940
"devDependencies": {
4041
"@types/cookie": "0.6.0",
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import * as yaml from 'yaml'
2+
3+
/**
4+
* Processes multiline strings in YAML to ensure they are properly formatted
5+
* @param yamlContent - The YAML content as string
6+
* @returns Processed YAML content with proper multiline formatting
7+
*/
8+
export const processMultilineInYaml = (yamlContent: string): string => {
9+
try {
10+
// Parse the YAML to get the object structure
11+
const parsed = yaml.parse(yamlContent)
12+
13+
// Convert back to YAML with proper multiline formatting
14+
const processed = yaml.stringify(parsed, {
15+
// Use literal block scalar (|) for multiline strings
16+
blockQuote: 'literal',
17+
// Preserve line breaks
18+
lineWidth: 0,
19+
// Use double quotes for strings that need escaping
20+
doubleQuotedAsJSON: false,
21+
// Use literal block scalar for multiline strings
22+
defaultStringType: 'QUOTE_DOUBLE',
23+
})
24+
25+
return processed
26+
} catch (error) {
27+
// If parsing fails, return original content
28+
console.warn('Failed to process multiline YAML:', error)
29+
return yamlContent
30+
}
31+
}
32+
33+
/**
34+
* Processes form values to ensure multiline strings are properly handled
35+
* @param values - The form values object
36+
* @returns Processed values with multiline strings properly formatted
37+
*/
38+
export const processMultilineInFormValues = (values: any): any => {
39+
if (!values || typeof values !== 'object') {
40+
return values
41+
}
42+
43+
const processed = { ...values }
44+
45+
const processObject = (obj: any): any => {
46+
if (typeof obj === 'string') {
47+
// Check if this string should be multiline
48+
if (obj.includes('\n') || obj.length > 80) {
49+
return obj
50+
}
51+
return obj
52+
}
53+
54+
if (Array.isArray(obj)) {
55+
return obj.map(processObject)
56+
}
57+
58+
if (obj && typeof obj === 'object') {
59+
const result: any = {}
60+
for (const [key, value] of Object.entries(obj)) {
61+
result[key] = processObject(value)
62+
}
63+
return result
64+
}
65+
66+
return obj
67+
}
68+
69+
return processObject(processed)
70+
}
71+
72+
/**
73+
* Detects if a string value should be treated as multiline
74+
* @param value - The string value to check
75+
* @returns true if the value should be multiline
76+
*/
77+
export const shouldBeMultiline = (value: string): boolean => {
78+
if (!value || typeof value !== 'string') {
79+
return false
80+
}
81+
82+
// Check for newlines
83+
if (value.includes('\n')) {
84+
return true
85+
}
86+
87+
// Check for long strings
88+
if (value.length > 80) {
89+
return true
90+
}
91+
92+
// Check for multiline indicators
93+
const multilineIndicators = [
94+
'#cloud-config',
95+
'#!/',
96+
'---',
97+
'```',
98+
'BEGIN',
99+
'END',
100+
'-----BEGIN',
101+
'-----END',
102+
]
103+
104+
return multilineIndicators.some(indicator => value.includes(indicator))
105+
}

src/endpoints/forms/formSync/utils/onValuesChange.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { OpenAPIV2 } from 'openapi-types'
22
import { TFormName } from 'src/localTypes/forms'
33
import { removeEmptyFormValues, renameBrokenFieldBack } from './removeAndRename'
44
import { normalizeValuesForQuotas } from './normalizeQuotas'
5+
import { processMultilineInFormValues } from './multilineHandler'
56

67
export const onValuesChange = ({
78
values,
@@ -15,5 +16,6 @@ export const onValuesChange = ({
1516
const cleanSchema = removeEmptyFormValues(values, persistedKeys)
1617
const fixedCleanSchema = renameBrokenFieldBack(cleanSchema)
1718
const quotasFixedSchema = normalizeValuesForQuotas(fixedCleanSchema, properties)
18-
return quotasFixedSchema
19+
const multilineProcessedSchema = processMultilineInFormValues(quotasFixedSchema)
20+
return multilineProcessedSchema
1921
}

src/endpoints/forms/formSync/utils/onYamlChange.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { OpenAPIV2 } from 'openapi-types'
22
import { renameBrokenFieldBackToFormAgain } from './removeAndRename'
33
import { normalizeValuesForQuotasToNumber } from './normalizeQuotas'
4+
import { processMultilineInYaml } from './multilineHandler'
45

56
export const onYamlChange = ({
67
values,

0 commit comments

Comments
 (0)