@@ -28,7 +28,7 @@ async function parseString(str, options = {}) {
2828 try {
2929 const result = yaml . parseWithPointers ( encodedContent , { attachComments : options ?. keepComments || false } ) ;
3030 options . yamlComments = result . comments ;
31- const obj = result . data ;
31+ const obj = normalizeYamlBlockScalarNewlines ( result . data ) ;
3232 if ( typeof obj === 'object' ) {
3333 return obj ;
3434 } else {
@@ -338,6 +338,35 @@ function addQuotesToRefInString(yamlString) {
338338 return yamlString . replace ( / ( \$ r e f : \s * ) ( [ ^ " ' \s > ] + ) / g, '$1"$2"' ) ;
339339}
340340
341+ /**
342+ * Normalize a parser artifact where block scalars with indentation indicators
343+ * gain a spurious leading newline during YAML parsing.
344+ * This keeps genuine blank first lines intact by only stripping a single
345+ * leading newline when the string also has a trailing newline.
346+ * @param {* } value - Parsed YAML value.
347+ * @returns {* } Normalized value.
348+ */
349+ function normalizeYamlBlockScalarNewlines ( value ) {
350+ if ( typeof value === 'string' ) {
351+ if ( value . startsWith ( '\n' ) && ! value . startsWith ( '\n\n' ) && value . endsWith ( '\n' ) ) {
352+ return value . slice ( 1 ) ;
353+ }
354+ return value ;
355+ }
356+
357+ if ( Array . isArray ( value ) ) {
358+ return value . map ( item => normalizeYamlBlockScalarNewlines ( item ) ) ;
359+ }
360+
361+ if ( value && typeof value === 'object' ) {
362+ for ( const key of Object . keys ( value ) ) {
363+ value [ key ] = normalizeYamlBlockScalarNewlines ( value [ key ] ) ;
364+ }
365+ }
366+
367+ return value ;
368+ }
369+
341370/**
342371 * Analyze the OpenAPI document.
343372 * @param {object } oaObj - The OpenAPI document as a JSON object.
0 commit comments