@@ -87,12 +87,19 @@ const REPOS = SDK.map(sdk =>
8787) ;
8888
8989const OUTPUT_DIR = path . join ( process . cwd ( ) , 'snippets/samples' ) ;
90- if ( ! fs . existsSync ( OUTPUT_DIR ) ) fs . mkdirSync ( OUTPUT_DIR , { recursive : true } ) ;
9190
91+ /**
92+ * Remove all existing code sample snippets before regenerating them.
93+ * This ensures stale/unused snippets don't persist.
94+ */
95+ function cleanSnippets ( ) {
96+ if ( ! fs . existsSync ( OUTPUT_DIR ) ) return ;
9297
93- function extractLanguageFromUrl ( repoUrl ) {
94- const match = repoUrl . match ( / m e i l i s e a r c h - ( [ a - z A - Z ] + ) / ) ;
95- return match ? match [ 1 ] : 'text' ; // Default to 'text' if not found
98+ const files = fs . readdirSync ( OUTPUT_DIR ) . filter ( f => f . startsWith ( 'code_samples_' ) && f . endsWith ( '.mdx' ) ) ;
99+ for ( const file of files ) {
100+ fs . unlinkSync ( path . join ( OUTPUT_DIR , file ) ) ;
101+ }
102+ console . log ( `Cleaned ${ files . length } existing code sample snippets.` ) ;
96103}
97104
98105async function fetchYaml ( url ) {
@@ -101,7 +108,14 @@ async function fetchYaml(url) {
101108 return yaml . load ( await response . text ( ) ) ;
102109}
103110
104- async function processRepos ( ) {
111+ async function buildSnippets ( ) {
112+ // Step 1: Clean existing snippets
113+ cleanSnippets ( ) ;
114+
115+ // Step 2: Ensure output directory exists
116+ if ( ! fs . existsSync ( OUTPUT_DIR ) ) fs . mkdirSync ( OUTPUT_DIR , { recursive : true } ) ;
117+
118+ // Step 3: Fetch and aggregate all code samples
105119 const operationSnippets = { } ;
106120
107121 for ( let i = 0 ; i < REPOS . length ; i ++ ) {
@@ -127,7 +141,8 @@ async function processRepos() {
127141 }
128142 }
129143
130- // Write each sample name content to a file in the snippets folder
144+ // Step 4: Write each sample to a file in the snippets folder
145+ let count = 0 ;
131146 for ( const [ operationName , snippets ] of Object . entries ( operationSnippets ) ) {
132147 const filePath = path . join ( OUTPUT_DIR , `code_samples_${ operationName } .mdx` ) ;
133148 const content = `
@@ -148,7 +163,7 @@ ${description}
148163
149164${ codeBlocks . map ( block => {
150165 // Remove language identifier and code block markers to ensure Mintlify can parse it
151- const cleanBlock = block . replace ( / ^ [ a - z ] + \s * / , '' ) . replace ( / ` ` ` $ / , '' ) ;
166+ const cleanBlock = block . replace ( / ^ [ a - z ] + \s * / , '' ) . replace ( / \` \` \ `$ / , '' ) ;
152167 return cleanBlock ;
153168} ) . join ( '\n' ) }
154169\`\`\`` ;
@@ -164,8 +179,10 @@ ${snippet.content}
164179 ` . trim ( ) ;
165180
166181 fs . writeFileSync ( filePath , content , 'utf-8' ) ;
167- console . log ( `Saved: ${ operationName } .mdx` ) ;
182+ count ++ ;
168183 }
184+
185+ console . log ( `Generated ${ count } code sample snippets.` ) ;
169186}
170187
171- processRepos ( ) ;
188+ buildSnippets ( ) ;
0 commit comments