Skip to content

Commit 565bcfb

Browse files
committed
Update CLI args for script related to Markdown links
1 parent 490f687 commit 565bcfb

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

tools/github-to-astro.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@ import { release } from 'node:os';
99
// So that they are not being transformed by this script
1010
const ICU4X_NON_VERSION_SPECIFIC_FILES = [
1111
"README.md",
12-
"quickstart.md"
12+
"quickstart.md",
13+
"index.md"
1314
];
1415

1516
/**
1617
* class to represent the values passed from the CLI through to the helper methods
1718
*/
1819
class Context {
19-
icu4xVersion: string;
20-
icu4xVersionStr: string;
20+
icu4xRef: string;
21+
webDirName: string;
2122
sitePrefix: string;
2223

2324
constructor(argsMap: {
24-
icu4xVersion: string;
25-
icu4xVersionStr: string;
25+
icu4xRef: string;
26+
webDirName: string;
2627
sitePrefix: string
2728
}) {
28-
this.icu4xVersion = argsMap["icu4xVersion"];
29-
this.icu4xVersionStr = argsMap["icu4xVersionStr"];
29+
this.icu4xRef = argsMap["icu4xRef"];
30+
this.webDirName = argsMap["webDirName"];
3031
this.sitePrefix = argsMap["sitePrefix"];
3132
}
3233
}
@@ -78,16 +79,16 @@ function transformMdBody(body: string, ctx: Context) {
7879

7980
// convert Markdown links that work in Github (relative paths) into full URIs
8081
// that Astro JS needs, including the ICU4X prefix
81-
let { icu4xVersion, icu4xVersionStr, sitePrefix } = ctx;
82+
let { icu4xRef, webDirName, sitePrefix } = ctx;
8283

8384
// in a relative link to any other file, format the URL to the Github blob
8485
replacementBody = replacementBody.replace(
8586
/\]\((?!http)([^\)]*)(?<!.md)\)/g,
86-
"](" + "https://github.com/unicode-org/icu4x/tree/icu%40" + icu4xVersion + "/tutorials/$1)"
87+
"](" + "https://github.com/unicode-org/icu4x/tree/" + encodeURIComponent(icu4xRef) + "/tutorials/$1)"
8788
);
8889

8990
// in a relative link to a Markdown file, get rid of the trailing `.md`
90-
replacementBody = replacementBody.replace(/(\[.*\])\((?!http)(.*)\.md\)/g, "$1(" + sitePrefix + "/" + icu4xVersionStr + "/$2)");
91+
replacementBody = replacementBody.replace(/(\[.*\])\((?!http)(.*)\.md\)/g, "$1(" + sitePrefix + "/" + webDirName + "/tutorials" + "/$2)");
9192

9293
return replacementBody;
9394
}
@@ -112,15 +113,6 @@ function icu4xGfmToAstroMd(content: string, inFilePath: string, ctx: Context) {
112113
return frontMatter + "\n" + replacementContent;
113114
}
114115

115-
/**
116-
* Convert a semver string into a string necessary for URIs within the URL
117-
* @param version Semantic version string
118-
* @returns The semantic version string, with dots replaced with underscores
119-
*/
120-
function getUriVersionStr(version: string) {
121-
return version.replace(/\./g, "_");
122-
}
123-
124116
/**
125117
* Read an input GH Markdown file and write the transformed AstroJS content to the output file
126118
* @param inFilePath String of input file path
@@ -170,7 +162,7 @@ function printHelp() {
170162
console.log("Convert ICU4X Github repo Markdown tutorials to Astro MDX files");
171163
console.log();
172164
console.log("Usage:");
173-
console.log("\tnpx tsx -- --inDir=<input-dir> --outDir=<output-dir> --icu4xTag=<ICU4X-semver> --sitePrefx=<site-prefix-str-else-emptystr>");
165+
console.log("\tnpx tsx -- --inDir=<input-dir> --outDir=<output-dir> --icu4xRef=<ICU4X-git-ref> --webDirName=<version-based-dir-name> --sitePrefix=<site-prefix-str-else-emptystr> --astroVersion=<semver>");
174166
}
175167

176168
/**
@@ -188,7 +180,10 @@ function parseCLIArgs() {
188180
type: "string",
189181
short: "o",
190182
},
191-
icu4xVersion: {
183+
icu4xRef: {
184+
type: "string",
185+
},
186+
webDirName: {
192187
type: "string",
193188
},
194189
// site prefix, as used by static site generator tools.
@@ -208,7 +203,8 @@ function parseCLIArgs() {
208203
values: {
209204
inDir: values["inDir"] ?? (() => {throw new Error("Need inDir")})(),
210205
outDir: values["outDir"] ?? (() => {throw new Error("Need outDir")})(),
211-
icu4xVersion: values["icu4xVersion"] ?? (() => {throw new Error("Need icu4xVersion")})(),
206+
icu4xRef: values["icu4xRef"] ?? (() => {throw new Error("Need icu4xRef")})(),
207+
webDirName: values["webDirName"] ?? (() => {throw new Error("Need webDirName")})(),
212208
sitePrefix: values["sitePrefix"] ?? (() => {throw new Error("Need sitePrefix")})(),
213209
astroVersion: values["astroVersion"] ?? (() => {throw new Error("Need astroVersion")})(),
214210
}
@@ -230,12 +226,11 @@ try {
230226

231227
const inputDirPath: string = values["inDir"];
232228
const outputDirPath = values["outDir"];
233-
const icu4xVersion = values["icu4xVersion"];
229+
const icu4xRef = values["icu4xRef"];
234230
const sitePrefix = values["sitePrefix"];
235-
236-
const icu4xVersionStr = getUriVersionStr(icu4xVersion);
231+
const webDirName = values["webDirName"];
237232

238-
const context = new Context({icu4xVersion, icu4xVersionStr, sitePrefix});
233+
const context = new Context({icu4xRef, webDirName, sitePrefix});
239234

240235
await convertDirFiles(inputDirPath, outputDirPath, context);
241236

0 commit comments

Comments
 (0)