-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskier.personal.tasks.mjs
More file actions
311 lines (300 loc) · 11.1 KB
/
skier.personal.tasks.mjs
File metadata and controls
311 lines (300 loc) · 11.1 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// @ts-check
import fs from 'fs';
import path from 'path';
import {
prepareOutputTask,
bundleCssTask,
copyStaticTask,
setGlobalFromMarkdownTask,
setGlobalsTask,
generateItemsTask,
generateFeedTask,
generatePagesTask,
generatePaginatedItemsTask,
generateSitemapTask
} from 'skier';
// Custom task to write a global variable to a JSON file
function writeGlobalToJsonTask(config) {
return {
name: 'write-global-to-json',
title: `Write ${config.globalVar} to ${config.outFile}`,
config,
run: async (cfg, ctx) => {
const data = ctx.globals[cfg.globalVar];
if (!data) {
ctx.logger.warn(`Global variable '${cfg.globalVar}' not found`);
return {};
}
const dir = path.dirname(cfg.outFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(cfg.outFile, JSON.stringify(data, null, 2));
ctx.logger.debug(`Wrote ${cfg.globalVar} to ${cfg.outFile}`);
return {};
}
};
}
// Custom task to aggregate all life data from split JSON files
function aggregateLifeDataTask() {
return {
name: 'aggregate-life-data',
title: 'Aggregate life data from JSON files',
run: async (cfg, ctx) => {
const lifeDir = './personal/items/life';
// Helper to safely load JSON
const loadJson = (filepath) => {
try {
return JSON.parse(fs.readFileSync(filepath, 'utf8'));
} catch (e) {
ctx.logger.warn(`Could not load ${filepath}`);
return null;
}
};
// Helper to load and merge monthly/yearly files
const loadTimeSeriesFiles = (dir, pattern) => {
if (!fs.existsSync(dir)) return [];
try {
const files = fs.readdirSync(dir)
.filter(f => f.endsWith('.json'))
.sort();
const allEntries = [];
for (const file of files) {
const data = loadJson(path.join(dir, file));
if (Array.isArray(data)) {
allEntries.push(...data);
}
}
return allEntries;
} catch (e) {
ctx.logger.warn(`Could not read directory ${dir}`);
return [];
}
};
// Load each category
const body = loadJson(path.join(lifeDir, 'body.json')) || {};
const nutrition = loadJson(path.join(lifeDir, 'nutrition.json')) || {};
const exercise = loadJson(path.join(lifeDir, 'fitness.json')) || {};
const events = loadJson(path.join(lifeDir, 'events.json')) || {};
const media = loadJson(path.join(lifeDir, 'media.json')) || {};
const travel = loadJson(path.join(lifeDir, 'travel.json')) || {};
const career = loadJson(path.join(lifeDir, 'career.json')) || {};
// Merge time-series data if directories exist
const weightHistory = loadTimeSeriesFiles(path.join(lifeDir, 'body', 'weight'));
const exerciseHistory = loadTimeSeriesFiles(path.join(lifeDir, 'fitness', 'history'));
// Combine into single life object
ctx.globals.life = {
body: {
...body,
weightHistory,
},
nutrition,
exercise: {
...exercise,
history: exerciseHistory,
},
events: events.events || [],
media,
travel,
career
};
ctx.logger.debug('Life data aggregated successfully');
return {};
}
};
}
export const tasks = [
// Clean & Create output directory
prepareOutputTask({
outDir: './public/personal',
}),
// Process main CSS (includes shared tokens)
bundleCssTask({
from: './personal/assets/styles/main',
to: './public/personal',
output: 'styles.min.css',
minify: true,
}),
// Process Jamie CSS
bundleCssTask({
from: './personal/assets/styles/jamie',
to: './public/personal',
output: 'styles.jamie.min.css',
minify: true,
}),
// Copy images/assets
copyStaticTask({
from: './personal/assets/images',
to: './public/personal',
}),
// Read and convert CHANGELOG.md to HTML for downstream use
setGlobalFromMarkdownTask({
mdPath: './CHANGELOG.md',
outputVar: 'changelogHtml',
}),
// Set some globals
setGlobalsTask({
values: {
year: (new Date()).getFullYear(),
siteUrl: 'https://james.ripixel.co.uk',
},
valuesFn: globals => {
// Extract first vX.Y.Z version from changelogHtml
const match = globals.changelogHtml && globals.changelogHtml.match(/v(\d+\.\d+\.\d+)/i);
const latestVersion = match ? match[0] : undefined;
return { latestVersion };
}
}),
// Aggregate life data from split JSON files
aggregateLifeDataTask(),
// Generate individual thought article pages
generateItemsTask({
itemsDir: './personal/items',
partialsDir: './personal/partials',
outDir: './public/personal',
outputVar: 'thoughtsList',
// Custom excerpt: strip markdown links and return full first paragraph
excerptFn: (rawMarkdown) => {
// Get first paragraph (split by double newlines)
const firstPara = rawMarkdown.split(/\n\s*\n/)[0] ?? '';
// Strip markdown links: [text](url) -> text
const stripped = firstPara.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
return stripped.trim();
},
additionalVarsFn: ({ section, title, excerpt }) => ({
page: section,
subpage: '| ' + title,
description: excerpt,
}),
}),
// Write thoughtsList to JSON for pagination
writeGlobalToJsonTask({
globalVar: 'thoughtsList',
outFile: './personal/items/thoughts.json',
}),
// Generate RSS/Atom/JSON feeds for thoughts
generateFeedTask({
articles: '${thoughtsList}',
outDir: './public/personal',
site: {
title: 'James King',
description: 'Feed for thoughts articles',
id: 'https://james.ripixel.co.uk/',
link: 'https://james.ripixel.co.uk/',
language: 'en',
favicon: 'https://james.ripixel.co.uk/favicon.ico',
copyright: `All rights reserved ${(new Date()).getFullYear()}, James King`,
feedLinks: {
json: 'https://james.ripixel.co.uk/json.json',
atom: 'https://james.ripixel.co.uk/atom.xml',
},
author: {
name: 'James King',
email: 'ripixel+feed@gmail.com',
link: 'https://james.ripixel.co.uk/',
},
},
}),
// Generate paginated thoughts listing
generatePaginatedItemsTask({
dataFile: './personal/items/thoughts.json',
itemsPerPage: 10,
template: './personal/templates/thoughts.html',
partialsDir: './personal/partials',
outDir: './public/personal',
basePath: '/thoughts',
outputVar: 'thoughts',
paginationVar: 'pagination',
additionalVarsFn: () => ({
page: 'thoughts',
description: 'A peak inside my brain you ask? Reader, beware...',
}),
}),
// Generate paginated life-fitness pages
generatePaginatedItemsTask({
dataFile: './personal/items/life/fitness.json',
dataKey: 'timeline',
itemsPerPage: 10,
template: './personal/templates/life-fitness.html',
partialsDir: './personal/partials',
outDir: './public/personal',
basePath: '/life-fitness',
outputVar: 'activities',
paginationVar: 'pagination',
additionalVarsFn: () => {
const fitnessData = JSON.parse(fs.readFileSync('./personal/items/life/fitness.json', 'utf8'));
return {
page: 'life',
description: 'Running, walking, and generally trying to stay active.',
events: fitnessData.events,
};
},
}),
// Generate paginated life-media pages
generatePaginatedItemsTask({
dataFile: './personal/items/life/media.json',
dataKey: 'timeline',
itemsPerPage: 10,
template: './personal/templates/life-media.html',
partialsDir: './personal/partials',
outDir: './public/personal',
basePath: '/life-media',
outputVar: 'items',
paginationVar: 'pagination',
additionalVarsFn: () => ({
page: 'life',
description: 'What I\'m watching, reading, listening to, and playing.',
}),
}),
// Generate HTML pages
generatePagesTask({
pagesDir: './personal/pages',
partialsDir: './personal/partials',
outDir: './public/personal',
additionalVarsFn: ({ currentPage, ...vars }) => {
const pageMapping = {
'index': 'home',
'life-body': 'life',
'life-career': 'life',
'life-travel': 'life',
'life-music': 'life',
};
const page = pageMapping[currentPage] || currentPage;
return {
page,
description: (() => {
switch (page) {
case 'thoughts':
return 'A peak inside my brain you ask? Reader, beware...';
case 'profile':
return `So who am I? I'm James King, a Software Engineer from Nottinghamshire. You want some more info?`;
case 'projects':
return "Shall we take a look at some projects I've done?";
case 'changelog':
return 'What changes have happened to this site?';
case 'home':
return "I'm James King, and I make things for the web";
case 'life':
return "What's going on in my life? Fitness, media, and upcoming events.";
default:
return vars.description || '';
}
})(),
};
},
}),
// Generate sitemap.xml
generateSitemapTask({
outDir: './public/personal',
scanDir: './public/personal',
siteUrl: 'https://james.ripixel.co.uk',
}),
{
name: 'Copy robots.txt',
run: async () => {
const robotsPath = './personal/assets/robots.txt';
const copyToPath = './public/personal/robots.txt';
fs.copyFileSync(robotsPath, copyToPath);
}
}
];