forked from push-protocol/push-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
276 lines (221 loc) · 9.57 KB
/
build.mjs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import chalk from 'chalk';
import fs from 'fs';
import Jimp from 'jimp';
import path from 'path';
import readline from 'readline';
import { fileURLToPath } from 'url';
// Define the starting directory
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const docsDirectory = path.join(__dirname, '/docs');
const ogPreviewDirectory = path.join(__dirname, 'static/assets/docs/previews');
const ogDirectory = __dirname;
// Function to recursively read directories and files
function walkDirectory(currentDirPath, callback) {
fs.readdir(currentDirPath, (err, files) => {
if (err) {
throw new Error(err);
}
files.forEach((name) => {
const filePath = path.join(currentDirPath, name);
const stat = fs.statSync(filePath);
if (stat.isFile()) {
// Check if the file is an .mdx file before executing the callback
if (path.extname(filePath) === '.mdx') {
callback(filePath, stat);
}
} else if (stat.isDirectory()) {
// If it's a directory, recursively call the walkDirectory function
walkDirectory(filePath, callback);
}
});
});
}
function extractFrontMatter(content) {
const frontMatterRegex = /---[\s\S]*?---/;
const match = content.match(frontMatterRegex);
if (!match) return null;
const keyValueRegex = /(\w+):\s*(.+)/g; // Capture key-value pairs
let result;
const frontMatter = {};
while ((result = keyValueRegex.exec(match[0])) !== null) {
frontMatter[result[1]] = result[2].replace(/['"]/g, '').trim();
}
return frontMatter;
}
async function checkFileContent(filePath) {
// Read the file content
const content = fs.readFileSync(filePath, 'utf8');
// Extract the front matter block
const frontMatterEndIndex = content.indexOf('---', 4); // Find the end of the front matter block
if (frontMatterEndIndex === -1) {
console.log(chalk.yellow(`No front matter found in file: ${filePath}`));
await generateFilePreview(filePath); // Call your function here
return;
}
const frontMatterText = content.slice(0, frontMatterEndIndex + 3); // Extract front matter block
// Parse the front matter content
const metadata = extractFrontMatter(frontMatterText);
if (metadata && metadata.title && metadata.image) {
// Transform title to expected image name format: lowercase with underscores
const negatedTitle = metadata.id ? metadata.id.replace(metadata.title.toLowerCase().replace(/\s+/g, '-')) : '';
const negatedTitleFormatted = (negatedTitle.trim().replace(/-/g, '_')).replace('_undefined', '');
const expectedImageName = negatedTitleFormatted + "--" + metadata.title.toLowerCase().split(' ').join('_') + '.png'; // Assuming the extension is always .png
// Extract the actual image file name from the path
const actualImageName = metadata.image.split('/').pop().replace(/['"]+/g, '');
// Compare actual image file name with expected name
if (actualImageName !== expectedImageName) {
console.log(chalk.red(`Mismatch found in file: ${filePath}`));
console.log(chalk.red(`Expected image name: '${expectedImageName}', found: '${actualImageName}'`));
await generateFilePreview(filePath); // Call your function here
}
} else {
// Case where either 'image' or 'title' metadata is not found
console.log(chalk.yellow(`Required metadata (title or image) not found in file: ${filePath}`));
await generateFilePreview(filePath); // Call your function here
}
}
function clearDirectory(directoryPath) {
const files = fs.readdirSync(directoryPath);
for (const file of files) {
fs.unlinkSync(path.join(directoryPath, file));
}
}
async function generateFilePreview(filePath) {
console.log(chalk.blue(`Generating file preview for: ${filePath}`));
// Read the MDX content
let content = fs.readFileSync(filePath, 'utf8');
// Extract the front matter from the MDX content
let title;
const frontMatterRegex = /---[\s\S]*?---/;
const frontMatterMatch = content.match(frontMatterRegex);
if (!frontMatterMatch) return;
// Extract title value from the front matter
const titleRegex = /title:\s*(.*?)(\n|$)/;
const titleMatch = frontMatterMatch[0].match(titleRegex);
if (!titleMatch) return;
const rawTitle = titleMatch[1].trim().replace(/['"]/g, ''); // Remove any single or double quotes
let rawTitleFormatted = rawTitle;
rawTitleFormatted = rawTitle.replaceAll('(', '');
rawTitleFormatted = rawTitleFormatted.replaceAll(')', '');
// Infer id from title
const inferredId = rawTitleFormatted.toLowerCase().replace(/\s+/g, '-');
// Extract id value from the front matter
const idRegex = /id:\s*(.*?)(\n|$)/;
const idMatch = frontMatterMatch[0].match(idRegex);
if (idMatch) {
const id = idMatch[1].trim().replace(/['"]/g, '').replace(inferredId, '').replace(/^-|-$/g, '').replace(/-/g, '_');
if (id) {
// If there's extra content in 'id' after removing the inferred ID, prepend it to the title
title = id + "--" + rawTitle;
} else {
title = rawTitle;
}
} else {
title = rawTitle;
}
// Construct image path
const imageName = `${title.toLowerCase().split(' ').join('_')}.png`;
const assetLocation = '/static/assets/docs/previews';
const assetDocLocation = '/assets/docs/previews';
const previewDirectory = ogDirectory + assetLocation;
const imagePath = path.join(previewDirectory, imageName);
const relativeImagePath = path.join(assetDocLocation, imageName);
// Check if the image already exists
if (!fs.existsSync(imagePath)) {
await generatePNGImage(imagePath, title);
}
// Extract front matter
const frontMatterEndIndex = content.indexOf('---', 4);
const frontMatter = content.slice(0, frontMatterEndIndex);
// Check if image tag exists
const imageTagRegex = /image:\s*['"](.*?)['"]/;
const imageTagMatch = frontMatter.match(imageTagRegex);
// If image tag exists but doesn't match the expected name, replace it
if (imageTagMatch && imageTagMatch[1] !== imageName) {
content = content.replace(imageTagRegex, `image: "${relativeImagePath}"`);
fs.writeFileSync(filePath, content);
}
// If image tag doesn't exist, add it
else if (!imageTagMatch) {
const position = frontMatterEndIndex; // End of the front matter block
const imageTag = `image: "${relativeImagePath}"\n`;
content = [content.slice(0, position), imageTag, content.slice(position)].join('');
fs.writeFileSync(filePath, content);
}
}
function wrapText(font, text, maxTextWidth) {
const words = text.split(' ');
let lines = [];
let currentLine = words[0];
for (let i = 1; i < words.length; i++) {
const word = words[i];
const width = Jimp.measureText(font, currentLine + ' ' + word);
if (width < maxTextWidth) {
currentLine += ' ' + word;
} else {
lines.push(currentLine);
currentLine = word;
}
}
lines.push(currentLine);
return lines;
}
async function generatePNGImage(imagePath, title) {
console.log(chalk.magenta(`Generating PNG image for: ${imagePath}`));
const templatePreviewImagePath = ogDirectory + '/static/assets/docs/templatepreview/docsPreviewThumnail.png';
const mainFont = Jimp.FONT_SANS_64_BLACK;
const image = await Jimp.read(templatePreviewImagePath);
const loadedMainFont = await Jimp.loadFont(mainFont);
const maxTextWidth = image.bitmap.width - 80; // 80px for padding on both sides
let mainTitle = title;
let subTitle = null;
if (title.includes('--')) {
const parts = title.split('--', 2);
subTitle = parts[0];
mainTitle = parts[1];
}
if (subTitle && subTitle.includes('_')) {
// Convert # to : for the top left title
let formattedSubTitle = subTitle.replace(/_/g, ':');
formattedSubTitle = formattedSubTitle.replaceAll('::', ':');
formattedSubTitle = formattedSubTitle.replaceAll(':', '/');
formattedSubTitle = formattedSubTitle.replaceAll('/section', '');
// load font
const subFont = Jimp.FONT_SANS_32_BLACK;
const loadedSubFont = await Jimp.loadFont(subFont);
// create transparent overlay
let overlayImage = new Jimp(1200, 630, 0x0, (err, textImage) => {
//((0x0 = 0 = rgba(0, 0, 0, 0)) = transparent)
if (err) throw err;
});
overlayImage.print(loadedSubFont, 30, 30, formattedSubTitle);
overlayImage.color([{ apply: 'xor', params: ["#cf3fad"] }]);
// Print the subTitle (top left corner)
image.blit(overlayImage, 0, 0);
}
let lines = wrapText(loadedMainFont, mainTitle, maxTextWidth);
const totalHeight = Jimp.measureTextHeight(loadedMainFont, lines.join('\n'), maxTextWidth);
// Starting Y coordinate to make the block of text vertically centered
const yStart = (image.bitmap.height - totalHeight) / 2;
const lineHeight = Jimp.measureTextHeight(loadedMainFont, "M"); // Approximating line height using letter "M"
let yOffset = 0;
for (let line of lines) {
let textWidth = Jimp.measureText(loadedMainFont, line);
const x = (image.bitmap.width - textWidth) / 2;
image.print(loadedMainFont, x, yStart + yOffset, line, maxTextWidth);
yOffset += lineHeight * 0.5 + 20;
}
image.write(imagePath);
console.log('Image generated with title:', title);
}
// Prep for deployment starts everything
const prepForDeployment = async (appEnv) => {
console.log(chalk.green('Starting Custom Deployment Prebuild...' ));
clearDirectory(ogPreviewDirectory);
walkDirectory(docsDirectory, async (filePath) => {
console.log(filePath);
await checkFileContent(filePath);
});
}
var args = process.argv.slice(2);
await prepForDeployment(args[0]);