@@ -12,7 +12,41 @@ function isNotEmpty(json: JSON): boolean {
1212 return ! isEmpty ( json ) ;
1313}
1414
15- export function jsonToMarkdown ( json : JSON , pad = 0 ) : string {
15+ export function jsonToMarkdown ( json : Readonly < JSON > ) : string {
16+ const cloned = structuredClone ( json ) as JSON ; // Copy data to avoid mutating the original object
17+ const simplified = simplifyJson ( cloned ) ;
18+ return serializeJsonToMarkdown ( simplified , 0 ) ;
19+ }
20+
21+ function simplifyJson ( json : JSON ) : JSON {
22+ if ( json === null || typeof json !== 'object' ) {
23+ return json ;
24+ }
25+
26+ if ( Array . isArray ( json ) ) {
27+ const simplified = json . map ( simplifyJson ) ;
28+ // Check if this is an array of objects with single property where value is true
29+ if ( simplified . every ( ( item ) => typeof item === 'object'
30+ && item !== null
31+ && ! Array . isArray ( item )
32+ && Object . keys ( item ) . length === 1
33+ && Object . values ( item ) [ 0 ] === true ,
34+ ) ) {
35+ const propertyNames = simplified . map ( ( item ) => Object . keys ( item as Record < string , unknown > ) [ 0 ] ) ;
36+ return propertyNames . length === 1 ? propertyNames [ 0 ] : propertyNames . join ( ', ' ) ;
37+ }
38+ return simplified ;
39+ }
40+
41+ // For objects, recursively simplify all values
42+ const result : Record < string , JSON > = { } ;
43+ for ( const [ key , value ] of Object . entries ( json ) ) {
44+ result [ key ] = simplifyJson ( value as JSON ) ;
45+ }
46+ return result ;
47+ }
48+
49+ function serializeJsonToMarkdown ( json : JSON , pad = 0 ) : string {
1650 if ( typeof json === 'string' || typeof json === 'number' || typeof json === 'boolean' ) {
1751 return String ( json ) ;
1852 }
@@ -34,21 +68,21 @@ export function jsonToMarkdown(json: JSON, pad = 0): string {
3468 // Advanced array will use bullets
3569 const indent = ' ' . repeat ( pad * 2 ) ;
3670 const singleLine = json . length === 1 && json . every ( ( item ) => {
37- const content = jsonToMarkdown ( item , 0 ) ;
71+ const content = serializeJsonToMarkdown ( item , 0 ) ;
3872 return ! content . includes ( '\n' ) ;
3973 } ) ;
4074 if ( singleLine ) {
4175 // For single-item arrays with simple content, don't add indent
4276 return json . filter ( isNotEmpty )
4377 . map ( ( value ) => {
44- const content = jsonToMarkdown ( value , 0 ) ;
78+ const content = serializeJsonToMarkdown ( value , 0 ) ;
4579 return `- ${ content } ` ;
4680 } )
4781 . join ( ' ' ) ;
4882 }
4983 return json . filter ( isNotEmpty )
5084 . map ( ( value , index ) => {
51- const content = jsonToMarkdown ( value , 0 ) ;
85+ const content = serializeJsonToMarkdown ( value , 0 ) ;
5286 const lines = content . split ( '\n' ) ;
5387 if ( lines . length === 1 ) {
5488 return `${ indent } - ${ lines [ 0 ] } ` ;
@@ -66,14 +100,16 @@ export function jsonToMarkdown(json: JSON, pad = 0): string {
66100 return Object . entries ( json )
67101 . filter ( ( [ _ , value ] ) => isNotEmpty ( value ) )
68102 . map ( ( [ key , value ] ) => {
69- const valueStr = jsonToMarkdown ( value , pad + 1 ) ;
103+ const valueStr = serializeJsonToMarkdown ( value , pad + 1 ) ;
70104 if ( ( Array . isArray ( value ) && valueStr . includes ( '\n' ) )
71105 || ( ! Array . isArray ( value ) && typeof value === 'object' && value !== null && valueStr . includes ( '\n' ) ) ) {
72106 // Multi-line arrays or objects in objects should be on new lines with proper indentation
73107 return `${ indent } ${ key } :\n${ valueStr } ` ;
74108 }
75- // For inline values, don't add indent if we're in a nested context
76- const keyIndent = pad > 0 && typeof value === 'object' && value !== null ? '' : indent ;
109+ // For inline values, don't add indent if we're in a nested context or if current object has single property with simple value
110+ const currentObjectHasSingleProperty = Object . keys ( json ) . length === 1 ;
111+ const valueIsSimple = typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' ;
112+ const keyIndent = ( pad > 0 && ( ( typeof value === 'object' && value !== null ) || ( currentObjectHasSingleProperty && valueIsSimple ) ) ) ? '' : indent ;
77113 return `${ keyIndent } ${ key } : ${ valueStr } ` ;
78114 } )
79115 . join ( '\n' ) ;
0 commit comments