@@ -13,7 +13,7 @@ import { gfm } from "micromark-extension-gfm";
1313import { mdxFromMarkdown } from "mdast-util-mdx" ;
1414import { gfmFromMarkdown } from "mdast-util-gfm" ;
1515import { visit } from "unist-util-visit" ;
16-
16+ import { getVariablesFromZip , variablesReplaceStream } from "./variable.js" ;
1717const IMAGE_CDN_PREFIX = "https://docs-download.pingcap.com/media/images" ;
1818export const imageCDNs = {
1919 docs : IMAGE_CDN_PREFIX + "/docs" ,
@@ -36,10 +36,38 @@ export const imageCDNs = {
3636 * @param {string[] } [options.ignore] - Specify the files to be ignored
3737 * @param {Array } [options.pipelines]
3838 */
39- export async function retrieveAllMDs ( metaInfo , destDir , options ) {
39+ export async function retrieveAllMDs (
40+ metaInfo ,
41+ destDir ,
42+ options ,
43+ defaultVariables
44+ ) {
4045 const { repo, ref, path = "" } = metaInfo ;
4146 const { ignore = [ ] , pipelines = [ ] } = options ;
4247
48+ // Get variables.json from root directory
49+ let variables = defaultVariables ;
50+ if ( ! defaultVariables ) {
51+ try {
52+ const variablesResponse = await getContent ( repo , ref , "variables.json" ) ;
53+ if ( variablesResponse . data && variablesResponse . data . content ) {
54+ const content = Buffer . from (
55+ variablesResponse . data . content ,
56+ "base64"
57+ ) . toString ( ) ;
58+ variables = JSON . parse ( content ) ;
59+ }
60+ } catch ( error ) {
61+ sig . warn (
62+ "Failed to get variables.json from root directory:" ,
63+ error . message
64+ ) ;
65+ }
66+ }
67+
68+ // Add variablesReplaceStream to pipelines
69+ const ppls = [ ...pipelines , variablesReplaceStream ( variables ) ] ;
70+
4371 const data = ( await getContent ( repo , ref , path ) ) . data ;
4472
4573 if ( Array . isArray ( data ) ) {
@@ -59,11 +87,12 @@ export async function retrieveAllMDs(metaInfo, destDir, options) {
5987 path : `${ path } /${ name } ` ,
6088 } ,
6189 nextDest ,
62- options
90+ options ,
91+ variables
6392 ) ;
6493 } else {
6594 if ( name . endsWith ( ".md" ) ) {
66- writeContent ( download_url , nextDest , pipelines ) ;
95+ writeContent ( download_url , nextDest , ppls ) ;
6796 }
6897 }
6998 } ) ;
@@ -72,7 +101,7 @@ export async function retrieveAllMDs(metaInfo, destDir, options) {
72101 writeContent (
73102 data . download_url ,
74103 destDir . endsWith ( ".md" ) ? destDir : `${ destDir } /${ data . name } ` ,
75- pipelines
104+ ppls
76105 ) ;
77106 }
78107 }
@@ -179,6 +208,8 @@ export async function retrieveTiDBMDsFromZip(
179208 // Unzip archive
180209 const zip = new AdmZip ( archiveFileName ) ;
181210 const zipEntries = zip . getEntries ( ) ;
211+ const variables = getVariablesFromZip ( zip , "/variables.json" ) ;
212+ const ppls = [ ...pipelines , variablesReplaceStream ( variables ) ] ;
182213
183214 zipEntries . forEach ( function ( zipEntry ) {
184215 // console.log(zipEntry.toString()) // outputs zip entries information
@@ -208,7 +239,7 @@ export async function retrieveTiDBMDsFromZip(
208239 writeFile (
209240 `${ destDir } /${ relativePathNameList . join ( "/" ) } ` ,
210241 zipEntry . getData ( ) ,
211- pipelines
242+ ppls
212243 ) ;
213244 } ) ;
214245 } catch ( error ) {
@@ -222,8 +253,6 @@ export async function retrieveTiDBMDsFromZip(
222253 }
223254}
224255
225- const CONST_FILE_LIST = [ "/_docHome.md" ] ;
226-
227256export async function retrieveCloudMDsFromZip (
228257 metaInfo ,
229258 destDir ,
@@ -243,15 +272,14 @@ export async function retrieveCloudMDsFromZip(
243272 // Unzip archive
244273 const zip = new AdmZip ( archiveFileName ) ;
245274 const zipEntries = zip . getEntries ( ) ;
275+ const variables = getVariablesFromZip ( zip , "/variables.json" ) ;
276+ const ppls = [ ...pipelines , variablesReplaceStream ( variables ) ] ;
246277
247278 const cloudTocZipEntry = zipEntries . find ( ( entry ) =>
248279 entry . entryName . endsWith ( `/TOC-tidb-cloud.md` )
249280 ) ;
250281
251- const cloudFileList = [
252- ...CONST_FILE_LIST ,
253- ...getFileListFromToc ( cloudTocZipEntry . getData ( ) ) ,
254- ] ;
282+ const cloudFileList = getFileListFromToc ( cloudTocZipEntry . getData ( ) ) ;
255283
256284 // console.log(cloudFileList);
257285
@@ -284,12 +312,12 @@ export async function retrieveCloudMDsFromZip(
284312 return ;
285313 }
286314 if ( relativePathInZip === `TOC-tidb-cloud.md` ) {
287- writeFile ( `${ destDir } /TOC.md` , zipEntry . getData ( ) , pipelines ) ;
315+ writeFile ( `${ destDir } /TOC.md` , zipEntry . getData ( ) , ppls ) ;
288316 } else {
289317 writeFile (
290318 `${ destDir } /${ relativePathNameList . join ( "/" ) } ` ,
291319 zipEntry . getData ( ) ,
292- pipelines
320+ ppls
293321 ) ;
294322 }
295323 } ) ;
0 commit comments