-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_blogs.cjs
More file actions
75 lines (57 loc) · 2.74 KB
/
Copy pathpatch_blogs.cjs
File metadata and controls
75 lines (57 loc) · 2.74 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
const fs = require('fs');
const path = require('path');
const BLOG_DIR = path.join(__dirname, 'src', 'pages', 'blog');
const files = fs.readdirSync(BLOG_DIR);
let modifiedCount = 0;
for (const file of files) {
if (!file.endsWith('.tsx')) continue;
const filePath = path.join(BLOG_DIR, file);
let originalCode = fs.readFileSync(filePath, 'utf-8');
// If it already has it, skip
if (originalCode.includes('AuthorBio')) {
console.log(`Skipping ${file} - already modified.`);
continue;
}
let resultcode = originalCode;
// 1. Add Imports
const importStats = `import { AuthorBio } from '../../components/AuthorBio';\nimport { BlogSchema } from '../../components/BlogSchema';\n`;
// Find the last import
const lastImportIndex = resultcode.lastIndexOf('import ');
if (lastImportIndex !== -1) {
const nextLineIndex = resultcode.indexOf('\n', lastImportIndex);
resultcode = resultcode.substring(0, nextLineIndex + 1) + importStats + resultcode.substring(nextLineIndex + 1);
} else {
resultcode = importStats + resultcode;
}
// 2. Extract Data
// Title
const titleMatch = resultcode.match(/<h1[^>]*>([^<]+)<\/h1>/);
let title = 'Blog Post';
if (titleMatch) title = titleMatch[1].trim();
// Description
const descMatch = resultcode.match(/<p className="text-xl sm:text-2xl[^>]*>([^<]+)<\/p>/);
let description = '';
if (descMatch) description = descMatch[1].trim();
// Date
// <span className="w-1.5 h-1.5 rounded-full bg-zinc-700"></span>\s*<span>(.*?)<\/span>
const dateMatch = resultcode.match(/<span>(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[^<]*?<\/span>/);
let date = 'Jan 1, 2024';
if (dateMatch) date = dateMatch[0].replace(/<\/?span>/g, '').trim();
// URL path
const urlMatch = resultcode.match(/currentPath="([^"]+)"/);
let urlPath = `/blog`;
if (urlMatch) urlPath = urlMatch[1];
// 3. Inject components
// Right before <BlogFooter Use AuthorBio
const footerInjectLocation = resultcode.indexOf('<BlogFooter');
if (footerInjectLocation !== -1) {
const schemaString = `\n <BlogSchema title="${title.replace(/"/g, '"')}" description="${description.replace(/"/g, '"')}" datePublished="${date}" url="${urlPath}" />\n <AuthorBio />\n `;
resultcode = resultcode.substring(0, footerInjectLocation) + schemaString + resultcode.substring(footerInjectLocation);
}
if (resultcode !== originalCode) {
fs.writeFileSync(filePath, resultcode, 'utf-8');
console.log(`Successfully modified ${file}`);
modifiedCount++;
}
}
console.log(`Finished processing. Modified ${modifiedCount} files.`);