-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathbuild.blogs.tags.mjs
More file actions
287 lines (241 loc) · 7.78 KB
/
build.blogs.tags.mjs
File metadata and controls
287 lines (241 loc) · 7.78 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
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
277
278
279
280
281
282
283
284
285
286
287
import chalk from 'chalk';
import fs from 'fs/promises';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const BLOG_DIR = path.join(__dirname, 'blog');
const OUTPUT_DIR = path.join(__dirname, 'static/content');
const OUTPUT_FILE = path.join(OUTPUT_DIR, 'blogtags.json');
// Tag configuration with priority and category
// DO NOT ADD NEW TAGS HERE UNTIL CONFIRMED BY PROJECT LEAD AND MARKETING LEAD
const TAG_CONFIG = {
// Priority 0 - Featured (for showcasing blogs)
Featured: { priority: 0, category: 'Featured' },
// Priority 1 - Product
Product: { priority: 1, category: 'Product' },
// Priority 2 - Key Features
'Key Features': { priority: 2, category: 'Key Features' },
// Priority 3 - Technical
Technical: { priority: 3, category: 'Technical' },
// Priority 4 - Programs
Programs: { priority: 4, category: 'Programs' },
// Priority 5 - Thought Leadership
'Thought Leadership': { priority: 5, category: 'Thought Leadership' },
// Priority 6 - Case Studies
'Case Studies': { priority: 6, category: 'Case Studies' },
// Priority 7 - Ecosystem
Ecosystem: { priority: 7, category: 'Ecosystem' },
// Priority 8 - Partnerships
Partnerships: { priority: 8, category: 'Partnerships' },
// Priority 9 - Maker Monday
'Maker Monday': { priority: 9, category: 'Maker Monday' },
// Priority 10 - Deep Dives
'Deep Dives': { priority: 10, category: 'Deep Dives' },
// Priority 11 - Push 101
'Push 101': { priority: 11, category: 'Push 101' },
};
// Parse frontmatter from markdown content
const parseFrontmatter = (content) => {
const frontmatterRegex = /^---\n([\s\S]*?)\n---/;
const match = content.match(frontmatterRegex);
if (!match) return null;
const frontmatter = {};
const lines = match[1].split('\n');
let currentKey = null;
let currentValue = '';
let inArray = false;
for (const line of lines) {
// Check if line starts a new key-value pair
if (line.includes(':') && !inArray) {
// Save previous key-value if exists
if (currentKey) {
frontmatter[currentKey] = currentValue.trim();
}
const colonIndex = line.indexOf(':');
currentKey = line.substring(0, colonIndex).trim();
let value = line.substring(colonIndex + 1).trim();
// Check if value starts and ends with array on same line
if (value.startsWith('[') && value.endsWith(']')) {
currentValue = value;
inArray = false;
} else if (value.startsWith('[')) {
// Array spans multiple lines
inArray = true;
currentValue = value;
} else {
currentValue = value;
inArray = false;
}
} else if (inArray) {
// Continue building array value
currentValue += ' ' + line.trim();
if (line.includes(']')) {
inArray = false;
}
}
}
// Save last key-value
if (currentKey) {
frontmatter[currentKey] = currentValue.trim();
}
// Parse arrays
for (const key in frontmatter) {
const value = frontmatter[key];
if (value.startsWith('[') && value.endsWith(']')) {
const arrayContent = value.slice(1, -1);
frontmatter[key] = arrayContent
.split(',')
.map((item) => item.trim().replace(/^['"]|['"]$/g, ''))
.filter((item) => item.length > 0);
} else {
// Remove quotes from string values
frontmatter[key] = value.replace(/^['"]|['"]$/g, '');
}
}
return frontmatter;
};
// Convert tag name to URL slug
const tagToSlug = (tag) => {
return tag
.toLowerCase()
.replace(/[^\w\s-]/g, '') // Remove special characters
.replace(/\s+/g, '-') // Replace spaces with hyphens
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
.trim();
};
// Consolidate tag name (merge duplicates)
const consolidateTag = (tag) => {
const config = TAG_CONFIG[tag];
if (config && config.mergeTo) {
return config.mergeTo;
}
return tag;
};
// Get tag metadata
const getTagMetadata = (tag) => {
const config = TAG_CONFIG[tag];
if (config) {
return {
priority: config.priority,
category: config.category,
};
}
// Default for unconfigured tags
return {
priority: 99,
category: 'Other',
};
};
// Validate that a tag exists in TAG_CONFIG
const validateTag = (tag, blogDir) => {
if (!TAG_CONFIG[tag]) {
throw new Error(
`Invalid tag "${tag}" found in blog "${blogDir}".\n` +
` This tag is not defined in TAG_CONFIG.\n` +
` Please use only approved tags or get approval from Project Lead and Marketing Lead to add new tags.`
);
}
};
// Process a single blog post and extract tags
const processBlogPost = async (blogPath, blogDir) => {
try {
const indexPath = path.join(blogPath, 'index.md');
const content = await fs.readFile(indexPath, 'utf-8');
const frontmatter = parseFrontmatter(content);
if (!frontmatter) {
return [];
}
// Extract tags
let tags = [];
if (frontmatter.tags) {
if (Array.isArray(frontmatter.tags)) {
tags = frontmatter.tags;
} else if (typeof frontmatter.tags === 'string') {
tags = [frontmatter.tags];
}
}
// Validate all tags exist in TAG_CONFIG
tags.forEach((tag) => validateTag(tag, blogDir));
return tags;
} catch (error) {
// Re-throw validation errors to stop the build
if (error.message.includes('Invalid tag')) {
throw error;
}
console.error(
chalk.yellow(` ⚠️ Error processing ${blogDir}:`),
error.message
);
return [];
}
};
// Main function to build blog tags
export const buildBlogTags = async () => {
try {
console.log(chalk.cyan('\n🔍 Scanning blog directory for tags...'));
// Read all blog directories
const entries = await fs.readdir(BLOG_DIR, { withFileTypes: true });
const blogDirs = entries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)
.sort()
.reverse();
console.log(chalk.gray(`📦 Found ${blogDirs.length} total blog posts`));
// Collect all tags and count posts per tag
const tagCounts = new Map();
for (const blogDir of blogDirs) {
const blogPath = path.join(BLOG_DIR, blogDir);
const tags = await processBlogPost(blogPath, blogDir);
tags.forEach((tag) => {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
});
}
// Convert Map to array and create tag objects with metadata
const tagsArray = Array.from(tagCounts.entries())
.map(([tag, count]) => {
const metadata = getTagMetadata(tag);
return {
name: tag,
slug: tagToSlug(tag),
link: `/blog/tags/${tagToSlug(tag)}/`,
priority: metadata.priority,
category: metadata.category,
count: count,
};
})
// Sort by priority first, then alphabetically within same priority
.sort((a, b) => {
if (a.priority !== b.priority) {
return a.priority - b.priority;
}
return a.name.localeCompare(b.name);
});
console.log(
chalk.green(
` ✅ Found ${tagsArray.length} unique tags across all posts`
)
);
// Ensure output directory exists
await fs.mkdir(OUTPUT_DIR, { recursive: true });
// Write tags to JSON file
await fs.writeFile(
OUTPUT_FILE,
JSON.stringify(tagsArray, null, 2),
'utf-8'
);
console.log(
chalk.green(
`✅ Successfully generated blogtags.json with ${tagsArray.length} tags`
)
);
console.log(chalk.gray(`📄 Output: ${OUTPUT_FILE}`));
} catch (error) {
console.error(chalk.red('❌ Error building blog tags:'), error);
throw error;
}
};
// Run if executed directly
if (import.meta.url === `file://${process.argv[1]}`) {
buildBlogTags();
}