-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-blog-images.js
More file actions
207 lines (175 loc) ยท 6.49 KB
/
Copy pathvalidate-blog-images.js
File metadata and controls
207 lines (175 loc) ยท 6.49 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
#!/usr/bin/env node
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Blog posts directory
const postsDir = path.join(__dirname, 'src', 'content', 'posts');
// Centralized assets directory
const assetsDir = path.join(__dirname, 'src', 'assets', 'images');
// Results object
const results = {
valid: [],
issues: [],
summary: {
totalPosts: 0,
postsWithIssues: 0,
totalImages: 0,
imagesUsingCentralizedAssets: 0,
missingColocatedImages: 0
}
};
function getAllBlogPosts() {
const entries = fs.readdirSync(postsDir, { withFileTypes: true });
return entries
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
}
function extractImageImports(content) {
// Regular expression to match Image component imports
const imageImportRegex = /<Image\s+src=\{import\(['"`]([^'"`]+)['"`]\)\}/g;
const imports = [];
let match;
while ((match = imageImportRegex.exec(content)) !== null) {
imports.push({
fullMatch: match[0],
imagePath: match[1],
line: content.substring(0, match.index).split('\n').length
});
}
return imports;
}
function checkImageExists(imagePath, blogPostDir) {
return fs.existsSync(path.join(blogPostDir, imagePath));
}
function checkCentralizedImageExists(imagePath) {
// Convert relative path to centralized path
const centralizedPath = imagePath.replace(/^\.\.\/\.\.\/assets\/images\//, '');
return fs.existsSync(path.join(assetsDir, centralizedPath));
}
function getColocatedImagePath(centralizedPath) {
// Extract just the filename from centralized path
return centralizedPath.replace(/^\.\.\/\.\.\/assets\/images\//, './');
}
function validateBlogPost(blogPostName) {
const blogPostDir = path.join(postsDir, blogPostName);
const indexFile = path.join(blogPostDir, 'index.mdx');
if (!fs.existsSync(indexFile)) {
return {
blogPost: blogPostName,
error: 'No index.mdx file found',
images: []
};
}
const content = fs.readFileSync(indexFile, 'utf8');
const imageImports = extractImageImports(content);
const validation = {
blogPost: blogPostName,
totalImages: imageImports.length,
issues: [],
validImages: []
};
imageImports.forEach(imageImport => {
const { imagePath, line, fullMatch } = imageImport;
results.summary.totalImages++;
if (imagePath.startsWith('../../assets/images/')) {
// Using centralized assets - this is an issue
results.summary.imagesUsingCentralizedAssets++;
const colocatedPath = getColocatedImagePath(imagePath);
const colocatedExists = checkImageExists(colocatedPath, blogPostDir);
const centralizedExists = checkCentralizedImageExists(imagePath);
validation.issues.push({
line,
currentPath: imagePath,
issue: 'Using centralized assets instead of colocated images',
colocatedPath,
colocatedExists,
centralizedExists,
recommendation: colocatedExists
? `Change to: <Image src={import('${colocatedPath}')} ...`
: `Move image from centralized assets to blog folder and use: <Image src={import('${colocatedPath}')} ...`
});
if (!colocatedExists) {
results.summary.missingColocatedImages++;
}
} else if (imagePath.startsWith('./')) {
// Using colocated images - check if they exist
const imageExists = checkImageExists(imagePath, blogPostDir);
if (imageExists) {
validation.validImages.push({
line,
path: imagePath,
status: 'Valid colocated image'
});
} else {
validation.issues.push({
line,
currentPath: imagePath,
issue: 'Colocated image file not found',
recommendation: 'Ensure the image file exists in the blog post folder'
});
}
} else {
// Other path patterns
validation.issues.push({
line,
currentPath: imagePath,
issue: 'Unexpected image path pattern',
recommendation: 'Use colocated images with ./ prefix'
});
}
});
return validation;
}
function generateReport() {
console.log('\n๐ BLOG IMAGE VALIDATION REPORT');
console.log('=====================================\n');
const blogPosts = getAllBlogPosts();
results.summary.totalPosts = blogPosts.length;
blogPosts.forEach(blogPost => {
const validation = validateBlogPost(blogPost);
if (validation.error) {
console.log(`โ ${blogPost}: ${validation.error}`);
results.issues.push(validation);
return;
}
if (validation.issues.length > 0) {
results.summary.postsWithIssues++;
results.issues.push(validation);
console.log(`โ ๏ธ ${blogPost}`);
console.log(` Total images: ${validation.totalImages}`);
console.log(` Issues found: ${validation.issues.length}`);
console.log(` Valid images: ${validation.validImages.length}\n`);
validation.issues.forEach((issue, index) => {
console.log(` Issue ${index + 1} (Line ${issue.line}):`);
console.log(` ๐ธ ${issue.issue}`);
console.log(` ๐ธ Current: ${issue.currentPath}`);
if (issue.colocatedPath) {
console.log(` ๐ธ Colocated exists: ${issue.colocatedExists ? 'โ
' : 'โ'}`);
console.log(` ๐ธ Centralized exists: ${issue.centralizedExists ? 'โ
' : 'โ'}`);
}
console.log(` ๐ธ Recommendation: ${issue.recommendation}\n`);
});
} else {
results.valid.push(validation);
console.log(`โ
${blogPost} - All ${validation.totalImages} images properly colocated`);
}
});
// Summary
console.log('\n๐ SUMMARY');
console.log('===========');
console.log(`Total blog posts: ${results.summary.totalPosts}`);
console.log(`Posts with issues: ${results.summary.postsWithIssues}`);
console.log(`Posts valid: ${results.valid.length}`);
console.log(`Total images: ${results.summary.totalImages}`);
console.log(`Images using centralized assets: ${results.summary.imagesUsingCentralizedAssets}`);
console.log(`Missing colocated images: ${results.summary.missingColocatedImages}`);
if (results.summary.postsWithIssues === 0) {
console.log('\n๐ All blog posts are using properly colocated images!');
} else {
console.log('\nโ ๏ธ Issues found. Please review and fix the problems above.');
}
}
// Run the validation
generateReport();