-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
233 lines (187 loc) · 7.27 KB
/
build.js
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
/*
Build Roobottom.com
Builds:
- all collections
- tags
- feeds
- Kanga collection data
*/
const fs = require('fs');
const path = require('path');
const util = require('util');
const { extractFrontmatter, markdownToHtml } = require('./lib/utils/markdown');
const slugify = require('slugify');
const fg = require('fast-glob'); // Include fast-glob
const generateFeed = require('./lib/utils/feed');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const mkdir = util.promisify(fs.mkdir);
// Converts a string to title case
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
// Converts a string to a URL-friendly slug
function toUrlSlug(str) {
return slugify(str.toLowerCase(), {
strict: true,
remove: /^[\d]{4}-[\d]{2}-[\d]{2}-/g
})
}
async function ensureDirectoryExists(dirPath) {
try {
await mkdir(dirPath, { recursive: true });
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
}
function findSimilarPosts(post, allPosts, limit = 5) {
if (!post.tags || !Array.isArray(post.tags)) return [];
const postTags = new Set(post.tags.map(tag => tag.toLowerCase()));
const similarPosts = allPosts
.filter(p => p.slug !== post.slug && p.tags && Array.isArray(p.tags))
.map(p => {
const pTags = new Set(p.tags.map(tag => tag.toLowerCase()));
const commonTags = new Set([...postTags].filter(tag => pTags.has(tag)));
return { post: p, commonTags: commonTags.size };
})
.filter(p => p.commonTags > 0)
.sort((a, b) => b.commonTags - a.commonTags)
.slice(0, limit)
.map(p => ({
slug: p.post.slug,
title: p.post.title,
date: p.post.date,
tags: p.post.tags,
summary: p.post.summary,
cover: p.post.cover || undefined,
coverAlt: p.post.coverAlt || undefined
}));
return similarPosts;
}
async function createCollections(collections) {
let tagsMap = new Map();
let allPosts = [];
for(let collectionObj of collections) {
const outputFilePath = path.join(__dirname, collectionObj.output);
const outputDir = path.dirname(outputFilePath);
await ensureDirectoryExists(outputDir);
const files = await fg(collectionObj.input, { cwd: __dirname });
let collection = [];
for (let filePath of files) {
console.log('Processing file:', filePath);
const content = await readFile(filePath, 'utf8');
const parsed = extractFrontmatter(content);
const html = markdownToHtml(parsed.content);
const postSlug = parsed.data.title ? toUrlSlug(parsed.data.title) : undefined;
const postData = {
slug: postSlug,
content: html,
...parsed.data
}
collection.push(postData);
allPosts.push(postData); // Add post to allPosts array
if (parsed.data.tags && Array.isArray(parsed.data.tags)) {
parsed.data.tags.forEach(tag => {
const titleCaseTag = toTitleCase(tag);
if (!tagsMap.has(titleCaseTag)) {
tagsMap.set(titleCaseTag, []); // Initialize with an empty array
}
tagsMap.get(titleCaseTag).push(postData);
});
}
}
// Store the collection data without sorting
await writeFile(outputFilePath, JSON.stringify(collection, null, 2));
}
// After all posts from all collections are processed, find similar posts
for (let collectionObj of collections) {
const outputFilePath = path.join(__dirname, collectionObj.output);
let collection = JSON.parse(await readFile(outputFilePath, 'utf8'));
const updatedCollection = collection.map(post => ({
...post,
similarPosts: findSimilarPosts(post, allPosts)
}));
// Sort the collection by date before saving
updatedCollection.sort((a, b) => new Date(b.date) - new Date(a.date));
await writeFile(outputFilePath, JSON.stringify(updatedCollection, null, 2));
}
// Write tags file
const tagsData = Array.from(tagsMap, ([title, posts]) => ({
title,
count: posts.length,
slug: toUrlSlug(title),
posts
}));
await writeFile(path.join(__dirname, 'collections/tags.json'), JSON.stringify(tagsData, null, 2));
return allPosts;
}
async function createKanga() {
let structuredData = [];
//get files from kanga, just one level deep mind
const files = await fg('src/content/kanga/*/*.md', { cwd: __dirname });
for(let file of files) {
// Extract folder name from the post's file path
const pathParts = file.split('/');
const folderName = pathParts[pathParts.length - 2];
// Check if the folder already exists in the structuredData
let folderObj = structuredData.find(f => f.title === folderName);
if (!folderObj) {
folderObj = { title: folderName, items: [] };
structuredData.push(folderObj);
}
const content = await readFile(file, 'utf8');
const parsed = extractFrontmatter(content);
const html = markdownToHtml(parsed.content);
const postSlug = parsed.data.title ? toUrlSlug(parsed.data.title) : undefined;
folderObj.items.push({
slug: postSlug,
content: html,
...parsed.data
});
}
structuredData.sort((a, b) => a.title.localeCompare(b.title));
await writeFile('collections/kanga.json', JSON.stringify(structuredData, null, 2));
}
async function createKangaExamples() {
let structuredData = {};
const files = await fg('src/content/kanga/example/*/*.{njk,md}', { cwd: __dirname });
for(let file of files) {
// Extract folder name from the post's file path
const pathParts = file.split(path.sep);
const folderName = pathParts[pathParts.length - 2];
const fileName = path.basename(file, path.extname(file));
const type = path.extname(file).slice(1)
console.log('Kanga example:', folderName, fileName);
// Check if the folder already exists in the structuredData
if (!structuredData[folderName]) {
structuredData[folderName] = [];
}
const content = await readFile(file, 'utf8');
const parsed = extractFrontmatter(content);
if(parsed.data.css) {
console.log('parsed css', parsed.data.css)
}
structuredData[folderName].push({
slug: fileName,
type: type,
...parsed.data,
title: parsed.data.title,
content: parsed.content
});
}
await writeFile('collections/kanga-examples.json', JSON.stringify(structuredData, null, 2));
}
async function build() {
const allPosts = await createCollections([
{
input: 'src/content/articles/*.md',
output: 'collections/articles.json'
}
]);
await createKanga();
await createKangaExamples();
await generateFeed(allPosts); // Call the feed generation function
}
build();