|
| 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 | +} |
0 commit comments