-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_tutorials.js
More file actions
211 lines (179 loc) · 5.4 KB
/
generate_tutorials.js
File metadata and controls
211 lines (179 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
/**
* Generates tutorials-generated.json from MDX files in the tutorials folder.
* This file is used by the tutorials showcase page to display all tutorials.
* Usage: node generate_tutorials.js
*/
const fs = require("fs");
const path = require("path");
// Configuration
const CONTENT_DIR = path.join(__dirname, "content", "tutorials");
const LOCALES = ["en", "es"];
const OUTPUT_FILE = path.join(
__dirname,
"src",
"data",
"tutorials-generated.json",
);
/**
* Parse YAML frontmatter from MDX content.
* @param {string} content - The MDX file content
* @returns {Object} - Parsed frontmatter as an object
*/
function parseFrontmatter(content) {
const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---/);
if (!frontmatterMatch) {
return {};
}
const frontmatter = {};
const lines = frontmatterMatch[1].split("\n");
let currentKey = null;
let inArray = false;
let arrayValues = [];
for (const line of lines) {
// Check for array items (lines starting with -)
if (inArray && line.trim().startsWith("-")) {
let value = line.trim().slice(1).trim();
// Remove quotes if present
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
arrayValues.push(value);
continue;
}
// If we were in an array and hit a non-array line, save the array
if (inArray && currentKey) {
frontmatter[currentKey] = arrayValues;
inArray = false;
arrayValues = [];
}
// Check for key: value pairs
const keyMatch = line.match(/^(\w+):\s*(.*)$/);
if (keyMatch) {
const key = keyMatch[1];
let value = keyMatch[2].trim();
currentKey = key;
if (value === "" || value === "[]") {
// This might be the start of an array or empty value
if (value === "[]") {
frontmatter[key] = [];
} else {
inArray = true;
arrayValues = [];
}
} else {
// Clean up the value (remove quotes)
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
// Handle null values
if (value.toLowerCase() === "null") {
value = null;
}
frontmatter[key] = value;
}
}
}
// Don't forget the last array if we ended in one
if (inArray && currentKey) {
frontmatter[currentKey] = arrayValues;
}
return frontmatter;
}
/**
* Get all MDX files in a specific directory.
* @param {string} dir - The directory to scan
* @returns {Array} - Array of file info objects
*/
function getMdxFiles(dir) {
if (!fs.existsSync(dir)) {
return [];
}
const files = [];
const entries = fs.readdirSync(dir);
for (const entry of entries) {
if (path.extname(entry) === ".mdx") {
files.push({
filename: entry,
id: path.basename(entry, ".mdx"),
path: path.join(dir, entry),
});
}
}
return files;
}
/**
* Process a single tutorial MDX file.
* @param {Object} fileInfo - File information object
* @returns {Object} - Processed tutorial data
*/
function processTutorial(fileInfo) {
const content = fs.readFileSync(fileInfo.path, "utf-8");
const frontmatter = parseFrontmatter(content);
// Parse sidebar_position as int if present
let sidebarPosition = frontmatter.sidebar_position;
if (
sidebarPosition !== undefined &&
sidebarPosition !== "" &&
sidebarPosition !== null
) {
const parsed = parseInt(sidebarPosition, 10);
sidebarPosition = isNaN(parsed) ? null : parsed;
} else {
sidebarPosition = null;
}
// Process image path
let imagePath = frontmatter.image || null;
if (imagePath && imagePath.startsWith("../")) {
const filename = path.basename(imagePath);
const slug = path.basename(path.dirname(imagePath));
imagePath = `/img/tutorials/${slug}/${filename}`;
}
return {
id: fileInfo.id,
title: frontmatter.title || fileInfo.id,
description: frontmatter.description || "",
tags: frontmatter.tags || [],
image: imagePath,
sidebar_position: sidebarPosition,
};
}
/**
* Main function to generate tutorials JSON.
*/
function main() {
console.log("📚 Generating tutorials data...");
const tutorialsByLocale = {};
for (const locale of LOCALES) {
const localeDir = path.join(CONTENT_DIR, locale);
console.log(`Checking ${locale} tutorials in ${localeDir}`);
const mdxFiles = getMdxFiles(localeDir);
console.log(`Found ${mdxFiles.length} tutorial files for ${locale}`);
const tutorials = mdxFiles.map(processTutorial);
// Sort by sidebar_position first, then by title
tutorials.sort((a, b) => {
const posA = a.sidebar_position !== null ? a.sidebar_position : 9999;
const posB = b.sidebar_position !== null ? b.sidebar_position : 9999;
if (posA !== posB) {
return posA - posB;
}
return a.title.localeCompare(b.title);
});
tutorialsByLocale[locale] = tutorials;
}
// Ensure output directory exists
const outputDir = path.dirname(OUTPUT_FILE);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// Write the JSON file
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(tutorialsByLocale, null, 2));
console.log(`✅ Generated ${OUTPUT_FILE}`);
}
main();