@@ -24,8 +24,8 @@ function normalizeLang(input: string): SupportedLanguage {
2424}
2525
2626function renderFenceInfo ( lang : SupportedLanguage , rawLang : string ) : string {
27- if ( lang === "typescript" || lang === "javascript" ) return "typescript index.ts " ;
28- if ( lang === "python" ) return "python main.py " ;
27+ if ( lang === "typescript" || lang === "javascript" ) return "typescript Typescript/Javascript " ;
28+ if ( lang === "python" ) return "python Python " ;
2929 return `${ lang } ${ toTitleCase ( rawLang ) } ` ;
3030}
3131
@@ -92,6 +92,10 @@ function injectParamsIntoObjectLiteral(
9292 }
9393 }
9494 }
95+ // For JS/TS single-line objects, remove any trailing comma before the closing brace
96+ if ( lang !== "python" ) {
97+ updated = updated . replace ( / , \s * } / , " }" ) ;
98+ }
9599 return updated ;
96100}
97101
@@ -136,29 +140,67 @@ function applyOverridesToSource(
136140 overrides ?: Record < string , unknown >
137141) : string {
138142 if ( ! overrides || Object . keys ( overrides ) . length === 0 ) return src ;
139- // Heuristic: find the first object literal in a create(...) call and inject params
140- const createCall = / ( i n v o c a t i o n s \. ( c r e a t e | n e w ) \( ) ( [ \s \S ] * ?) ( \) ) / m;
141- const match = src . match ( createCall ) ;
142- if ( ! match ) return src ;
143- const before = src . slice ( 0 , match . index ?? 0 ) ;
144- const after = src . slice ( ( match . index ?? 0 ) + match [ 0 ] . length ) ;
145- const inside = match [ 3 ] ?? "" ;
146- const objectMatch = inside . match ( / \{ [ \s \S ] * ?\} / m) ;
147- if ( objectMatch ) {
148- const objBefore = inside . slice ( 0 , objectMatch . index ! ) ;
149- const obj = objectMatch [ 0 ] ;
150- const objAfter = inside . slice ( ( objectMatch . index || 0 ) + objectMatch [ 0 ] . length ) ;
151- const injected = injectParamsIntoObjectLiteral ( lang , obj , overrides ) ;
152- const newInside = objBefore + injected + objAfter ;
153- return before + match [ 1 ] + newInside + match [ 4 ] + after ;
143+ let transformed = src ;
144+
145+ // Heuristic: find the first object literal in a X.create(...) call and inject params (excluding special keys like 'log')
146+ const { log : _logOverride , ...injectionOverrides } = overrides as Record < string , unknown > ;
147+ if ( Object . keys ( injectionOverrides ) . length > 0 ) {
148+ const createCall = / ( \b \w + \. ( c r e a t e | n e w ) \( ) ( [ \s \S ] * ?) ( \) ) / m;
149+ const match = transformed . match ( createCall ) ;
150+ if ( match ) {
151+ const before = transformed . slice ( 0 , match . index ?? 0 ) ;
152+ const after = transformed . slice ( ( match . index ?? 0 ) + match [ 0 ] . length ) ;
153+ const inside = match [ 3 ] ?? "" ;
154+ const objectMatch = inside . match ( / \{ [ \s \S ] * ?\} / m) ;
155+ if ( objectMatch ) {
156+ const objBefore = inside . slice ( 0 , objectMatch . index ! ) ;
157+ const obj = objectMatch [ 0 ] ;
158+ const objAfter = inside . slice ( ( objectMatch . index || 0 ) + objectMatch [ 0 ] . length ) ;
159+ const injected = injectParamsIntoObjectLiteral ( lang , obj , injectionOverrides ) ;
160+ const newInside = objBefore + injected + objAfter ;
161+ transformed = before + match [ 1 ] + newInside + match [ 4 ] + after ;
162+ } else if ( lang === "python" ) {
163+ const injectedArgs = injectParamsIntoPythonArgs ( inside , injectionOverrides ) ;
164+ const needsNewlineBeforeParen = / \n / . test ( injectedArgs ) && ! / \n \s * $ / . test ( injectedArgs ) ;
165+ const joiner = needsNewlineBeforeParen ? "\n" : "" ;
166+ transformed = before + match [ 1 ] + injectedArgs + joiner + match [ 4 ] + after ;
167+ } else {
168+ // JS/TS with no object literal: insert one and inject
169+ const injectedObj = injectParamsIntoObjectLiteral ( lang , "{}" , injectionOverrides ) ;
170+ transformed = before + match [ 1 ] + injectedObj + match [ 4 ] + after ;
171+ }
172+ }
154173 }
155- if ( lang === "python" ) {
156- const injectedArgs = injectParamsIntoPythonArgs ( inside , overrides ) ;
157- const needsNewlineBeforeParen = / \n / . test ( injectedArgs ) && ! / \n \s * $ / . test ( injectedArgs ) ;
158- const joiner = needsNewlineBeforeParen ? "\n" : "" ;
159- return before + match [ 1 ] + injectedArgs + joiner + match [ 4 ] + after ;
174+
175+ // Apply log override if present: replace argument of the last console.log/print call
176+ const logExpr = ( overrides as Record < string , unknown > ) ?. log ;
177+ if ( typeof logExpr === "string" && logExpr . trim ( ) . length > 0 ) {
178+ if ( lang === "python" ) {
179+ const regex = / p r i n t \( ( [ ^ \) ] * ) \) / g;
180+ let lastMatch : RegExpExecArray | null = null ;
181+ let m : RegExpExecArray | null ;
182+ while ( ( m = regex . exec ( transformed ) ) !== null ) lastMatch = m ;
183+ if ( lastMatch ) {
184+ const start = lastMatch . index ;
185+ const end = start + lastMatch [ 0 ] . length ;
186+ transformed =
187+ transformed . slice ( 0 , start ) + `print(${ logExpr } )` + transformed . slice ( end ) ;
188+ }
189+ } else {
190+ const regex = / c o n s o l e \. l o g \( ( [ ^ \) ] * ) \) / g;
191+ let lastMatch : RegExpExecArray | null = null ;
192+ let m : RegExpExecArray | null ;
193+ while ( ( m = regex . exec ( transformed ) ) !== null ) lastMatch = m ;
194+ if ( lastMatch ) {
195+ const start = lastMatch . index ;
196+ const end = start + lastMatch [ 0 ] . length ;
197+ transformed =
198+ transformed . slice ( 0 , start ) + `console.log(${ logExpr } )` + transformed . slice ( end ) ;
199+ }
200+ }
160201 }
161- return src ;
202+
203+ return transformed ;
162204}
163205
164206function parseOverridesString ( raw : string ) : Record < string , unknown > | undefined {
@@ -318,7 +360,7 @@ function slugifyEndpoint(method: string, endpoint: string): string {
318360async function writeSnippetFile ( filePath : string , blocks : string ) {
319361 const dir = path . dirname ( filePath ) ;
320362 await mkdir ( dir , { recursive : true } ) ;
321- const content = `<CodeGroup dropdown >\n${ blocks . trim ( ) } \n</CodeGroup>\n` ;
363+ const content = `<CodeGroup>\n${ blocks . trim ( ) } \n</CodeGroup>\n` ;
322364 await writeFile ( filePath , content , "utf8" ) ;
323365}
324366
@@ -403,7 +445,7 @@ async function processFile(file: string, spec: any) {
403445 if ( inline ) overrides = { ...( overrides || { } ) , ...inline } ;
404446 }
405447 const blocks = extractCodeSamples ( spec , endpoint , method , overrides ) . trim ( ) ;
406- return `<CodeGroup dropdown >\n${ blocks } \n</CodeGroup>` ;
448+ return `<CodeGroup>\n${ blocks } \n</CodeGroup>` ;
407449 } ) ;
408450
409451 if ( changed ) {
0 commit comments