MCPcopy Create free account
hub / github.com/LevelUpWeb3/app / processChallengeMarkdownFiles

Function processChallengeMarkdownFiles

scripts/processChallengeMarkdown.js:12–52  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

10const outputDirectory = path.join(process.cwd(), "public/data/challenges");
11
12const processChallengeMarkdownFiles = async () => {
13 const fileNames = fs.readdirSync(markdownDirectory);
14
15 const allChallengesDataPromises = fileNames.map(async (fileName) => {
16 const fullPath = path.join(markdownDirectory, fileName);
17
18 // Check if fullPath is a file to prevent EISDIR error
19 if (fs.statSync(fullPath).isFile()) {
20 const id = fileName.replace(/\.mdx$/, "");
21 const fileContents = fs.readFileSync(fullPath, "utf8");
22
23 const { data } = matter(fileContents);
24 if (!data.name) return null;
25
26 const challengeData = {
27 id,
28 ...data,
29 };
30 const individualOutputPath = path.join(outputDirectory, `${id}.mdx`);
31
32 if (!fs.existsSync(outputDirectory))
33 fs.mkdirSync(outputDirectory, { recursive: true });
34 fs.writeFileSync(individualOutputPath, fileContents);
35
36 return challengeData;
37 }
38 });
39
40 const allChallengesData = await Promise.all(allChallengesDataPromises);
41
42 const markdownData = allChallengesData
43 .filter((data) => data)
44 .sort(({ index: a }, { index: b }) => {
45 return a - b;
46 });
47
48 fs.writeFileSync(
49 path.join(JSONDirectory, "markdownData.json"),
50 JSON.stringify(markdownData, null, 2),
51 ); // Pretty print JSON
52};
53
54processChallengeMarkdownFiles().catch(console.error);

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected