33const fs = require ( "node:fs" ) ;
44const path = require ( "node:path" ) ;
55
6+ // Regex patterns defined at top level for performance
7+ const TITLE_PATTERN = / t i t l e = " ( [ ^ " ] + ) " / ;
8+ const HREF_PATTERN = / h r e f = " ( [ ^ " ] + ) " / ;
9+ const GITHUB_PATTERN = / h t t p s : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + \/ [ ^ / ] + ) / ;
10+
611function parseRepositoriesFromMDX ( content ) {
712 const repositories = [ ] ;
813
914 // Regex to find all SampleAppCard components with title and href
1015 const sampleAppCardRegex = / < S a m p l e A p p C a r d \s + ( [ ^ > ] + ) > / g;
1116
12- let match ;
13- while ( ( match = sampleAppCardRegex . exec ( content ) ) !== null ) {
17+ let match = sampleAppCardRegex . exec ( content ) ;
18+ while ( match !== null ) {
1419 const propsString = match [ 1 ] ;
1520
1621 // Extract title and href from props
17- const titleMatch = propsString . match ( / t i t l e = " ( [ ^ " ] + ) " / ) ;
18- const hrefMatch = propsString . match ( / h r e f = " ( [ ^ " ] + ) " / ) ;
22+ const titleMatch = propsString . match ( TITLE_PATTERN ) ;
23+ const hrefMatch = propsString . match ( HREF_PATTERN ) ;
1924
2025 if ( titleMatch && hrefMatch ) {
2126 const title = titleMatch [ 1 ] ;
2227 const href = hrefMatch [ 1 ] ;
2328
2429 // Check if it's a GitHub URL and extract repo name
25- const githubMatch = href . match ( / h t t p s : \/ \/ g i t h u b \. c o m \/ ( [ ^ / ] + \/ [ ^ / ] + ) / ) ;
30+ const githubMatch = href . match ( GITHUB_PATTERN ) ;
2631
2732 if ( githubMatch ) {
2833 const repoName = githubMatch [ 1 ] ;
@@ -33,6 +38,7 @@ function parseRepositoriesFromMDX(content) {
3338 } ) ;
3439 }
3540 }
41+ match = sampleAppCardRegex . exec ( content ) ;
3642 }
3743
3844 return repositories ;
@@ -123,7 +129,7 @@ async function updateExampleDates() {
123129 ) ;
124130
125131 // Update dates for each repository
126- repoData . forEach ( ( repo ) => {
132+ for ( const repo of repoData ) {
127133 // Find the SampleAppCard with this title and update its date
128134 const titleRegex = new RegExp (
129135 `(title="${ repo . title . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) } "[\\s\\S]*?)date="[^"]*"` ,
@@ -139,7 +145,7 @@ async function updateExampleDates() {
139145 } else {
140146 console . warn ( `Could not find or update date for "${ repo . title } "` ) ;
141147 }
142- } ) ;
148+ }
143149
144150 // Write the updated content back
145151 fs . writeFileSync ( mdxPath , content , "utf8" ) ;
0 commit comments