@@ -4,33 +4,41 @@ const fs = require('fs');
44 * Load constants from _constants.mdx file
55 */
66async function loadConstants ( constantsPath ) {
7- if ( ! fs . existsSync ( constantsPath ) ) {
8- return { } ;
9- }
7+ if ( ! fs . existsSync ( constantsPath ) ) return { } ;
108
119 const content = fs . readFileSync ( constantsPath , 'utf-8' ) ;
12-
13- // Parse the exported versions object
14- const match = content . match ( / e x p o r t \s + c o n s t \s + v e r s i o n s \s * = \s * \{ ( [ ^ } ] + ) \} / s) ;
15- if ( ! match ) return { } ;
16-
17- const versionsBlock = match [ 1 ] ;
1810 const constants = { } ;
1911
20- // Extract key-value pairs
21- const dockerTag = versionsBlock . match ( / d o c k e r T a g : \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / ) ;
22- const githubRef = versionsBlock . match ( / g i t h u b R e f : \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / ) ;
23- const helmChart = versionsBlock . match ( / h e l m C h a r t : \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / ) ;
24- const helmSource = versionsBlock . match ( / h e l m S o u r c e : \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / ) ;
25-
26- if ( dockerTag ) constants . dockerTag = dockerTag [ 1 ] ;
27- if ( githubRef ) constants . githubRef = githubRef [ 1 ] ;
28- if ( helmChart ) constants . helmChart = helmChart [ 1 ] ;
29- if ( helmSource ) constants . helmSource = helmSource [ 1 ] ;
12+ const blockRegex = / e x p o r t \s + c o n s t \s + ( \w + ) \s * = \s * \{ ( [ ^ } ] + ) \} / gs;
13+ let block ;
14+ while ( ( block = blockRegex . exec ( content ) ) !== null ) {
15+ const [ , name , body ] = block ;
16+ const obj = { } ;
17+ const propRegex = / ( \w + ) : \s * [ ' " ] ( [ ^ ' " ] + ) [ ' " ] / g;
18+ let prop ;
19+ while ( ( prop = propRegex . exec ( body ) ) !== null ) {
20+ obj [ prop [ 1 ] ] = prop [ 2 ] ;
21+ }
22+ if ( Object . keys ( obj ) . length > 0 ) constants [ name ] = obj ;
23+ }
3024
3125 return constants ;
3226}
3327
28+ function replaceConstantInterpolations ( content , constants ) {
29+ let result = content ;
30+ for ( const [ name , obj ] of Object . entries ( constants ) ) {
31+ if ( ! obj || typeof obj !== 'object' ) continue ;
32+ for ( const [ key , value ] of Object . entries ( obj ) ) {
33+ const pattern = new RegExp ( `\\$?\\{${ name } \\.${ key } \\}` , 'g' ) ;
34+ // Function replacer avoids `$&` / `$1` / etc. being treated as
35+ // backreferences if a constant value happens to contain `$`.
36+ result = result . replace ( pattern , ( ) => String ( value ) ) ;
37+ }
38+ }
39+ return result ;
40+ }
41+
3442/**
3543 * Process MDX content and convert to clean markdown.
3644 *
@@ -128,11 +136,7 @@ function processCodeBlocks(content, constants) {
128136 // Handle template literals with version interpolation
129137 code = code . replace ( / \{ ` ( [ \s \S ] * ?) ` \} / g, '$1' ) ;
130138
131- // Replace version placeholders in template literal syntax
132- code = code . replace ( / \$ \{ v e r s i o n s \. d o c k e r T a g \} / g, constants . dockerTag || 'latest' ) ;
133- code = code . replace ( / \$ \{ v e r s i o n s \. g i t h u b R e f \} / g, constants . githubRef || 'main' ) ;
134- code = code . replace ( / \$ \{ v e r s i o n s \. h e l m C h a r t \} / g, constants . helmChart || '' ) ;
135- code = code . replace ( / \$ \{ v e r s i o n s \. h e l m S o u r c e \} / g, constants . helmSource || '' ) ;
139+ code = replaceConstantInterpolations ( code , constants ) ;
136140
137141 // Clean up the code
138142 code = code . trim ( ) ;
@@ -187,17 +191,7 @@ function processContentSections(content) {
187191}
188192
189193function replaceVersionInterpolations ( content , constants ) {
190- let result = content ;
191-
192- // Match both `{versions.x}` (JSX interpolation in body text) and
193- // `${versions.x}` (template-literal interpolation inside JSX attributes).
194- // Without `\$?`, the `$` from `${...}` is left orphaned in the output.
195- result = result . replace ( / \$ ? \{ v e r s i o n s \. d o c k e r T a g \} / g, constants . dockerTag || 'latest' ) ;
196- result = result . replace ( / \$ ? \{ v e r s i o n s \. g i t h u b R e f \} / g, constants . githubRef || 'main' ) ;
197- result = result . replace ( / \$ ? \{ v e r s i o n s \. h e l m C h a r t \} / g, constants . helmChart || '' ) ;
198- result = result . replace ( / \$ ? \{ v e r s i o n s \. h e l m S o u r c e \} / g, constants . helmSource || '' ) ;
199-
200- return result ;
194+ return replaceConstantInterpolations ( content , constants ) ;
201195}
202196
203197function processImageTags ( content ) {
@@ -233,12 +227,7 @@ function processLinks(content, constants) {
233227 // Convert <Link to={`...`}>text</Link> to markdown links (template literals)
234228 result = result . replace (
235229 / < L i n k \s + t o = \{ ` ( [ ^ ` ] + ) ` \} > ( [ ^ < ] + ) < \/ L i n k > / g,
236- ( match , url , text ) => {
237- let resolvedUrl = url ;
238- resolvedUrl = resolvedUrl . replace ( / \$ \{ v e r s i o n s \. g i t h u b R e f \} / g, constants . githubRef || 'main' ) ;
239- resolvedUrl = resolvedUrl . replace ( / \$ \{ v e r s i o n s \. d o c k e r T a g \} / g, constants . dockerTag || 'latest' ) ;
240- return `[${ text } ](${ resolvedUrl } )` ;
241- }
230+ ( match , url , text ) => `[${ text } ](${ replaceConstantInterpolations ( url , constants ) } )`
242231 ) ;
243232
244233 // Convert <Link to="...">text</Link> to markdown links (static strings)
0 commit comments