-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.js
More file actions
191 lines (162 loc) · 7.36 KB
/
Copy pathbootstrap.js
File metadata and controls
191 lines (162 loc) · 7.36 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
const fs = require('fs').promises;
const path = require('path');
const { getJSON } = require('./data-cache');
async function bootstrapCV(lang = 'nl', category = null) {
// 1. Load the base data files (cached in memory)
const globalBase = await getJSON(path.join(__dirname, './data/global.base.json'));
const i18n = await getJSON(path.join(__dirname, `./data/i18n/${lang}.json`));
// 2. Helper: Dynamic Date Formatter
const formatPeriod = (startStr, endStr) => {
const format = (str) => {
if (!str) return i18n.ui.present || 'Present';
const [year, month] = str.split('-');
const monthIndex = parseInt(month) - 1;
const monthName = (i18n.ui.months && i18n.ui.months[monthIndex]) ? i18n.ui.months[monthIndex] : month;
return `${monthName} ${year}`;
};
return `${format(startStr)} – ${format(endStr)}`;
};
// 3. Initialize data
let jobs = [...globalBase.jobs];
let education = [...globalBase.education];
let projects = [...globalBase.projects];
let profile = { ...globalBase.profile, ...i18n.profile };
let ui = { ...globalBase.ui, ...i18n.ui };
// 4. Filtering Logic
// Load the referrer/config context to determine what to display
const referrers = await getJSON(path.join(__dirname, './data/referrers.json'));
// Find the correct config (or fall back to default)
const config = referrers[category] || referrers['default'];
const allowedTags = config.show_tags || [];
// Filter out 'all' (meta-tag meaning 'no filter'), then apply remaining tags
const effectiveTags = allowedTags.filter(t => t !== 'all');
if (effectiveTags.length > 0) {
jobs = jobs.filter(j => j.tags && j.tags.some(t => effectiveTags.includes(t)));
projects = projects.filter(p => p.tags && p.tags.some(t => effectiveTags.includes(t)));
}
// 5. Apply overrides
const catClean = String(category || '').trim();
const overridePath = path.resolve(__dirname, 'data', 'overrides', catClean, `${lang}.${catClean}.json`);
// Check if override file exists asynchronously
const overrideExists = await fs.access(overridePath).then(() => true).catch(() => false);
if (catClean && catClean !== 'all' && overrideExists) {
try {
const override = JSON.parse(await fs.readFile(overridePath, 'utf8'));
if (override.profile) {
profile = { ...profile, ...override.profile };
}
if (override.jobs) {
jobs = jobs.map(j => override.jobs[j.id] ? { ...j, ...override.jobs[j.id] } : j);
}
if (override.ui) {
if (override.ui.ai_proxy && ui.ai_proxy) {
override.ui.ai_proxy = { ...ui.ai_proxy, ...override.ui.ai_proxy };
}
if (override.ui.job_group_titles) {
override.ui.job_group_titles = { ...(ui.job_group_titles || {}), ...override.ui.job_group_titles };
}
ui = { ...ui, ...override.ui };
}
} catch (e) {
console.error(`ERROR loading override: ${e.message}`);
}
}
// 6. Mapping & Translation
const translatedJobs = jobs.map(job => {
const t = (i18n.jobs && i18n.jobs[job.id]) ? i18n.jobs[job.id] : {};
// PRIORITY:
// 1. Is there a description already on the job? (set via override in step 5)
// 2. If not, use the translation from the i18n file (nl.json/en.json)
// 3. Fallback to empty array
const finalDescription = (job.description && job.description.length > 0)
? job.description
: (t.description || []);
return {
...job,
// If the override has a title use it, otherwise i18n, otherwise company name
title: job.title || t.title || job.company,
description: finalDescription,
location: job.location || "",
periodDisplay: formatPeriod(job.start, job.end)
};
});
// 7. Sort by end date descending (current jobs first)
translatedJobs.sort((a, b) => (b.end || "9999-12").localeCompare(a.end || "9999-12"));
// 8. Group by tags (optional, only when group_jobs is active on the referrer)
let grouped = false;
let jobGroups = null;
if (config.group_jobs && Array.isArray(config.job_groups) && config.job_groups.length > 0) {
grouped = true;
// Build group buckets with matchTags and empty job arrays
const groupBuckets = config.job_groups.map(group => ({
main_tag: group.main_tag,
matchTags: Array.isArray(group.tags) ? group.tags : [group.main_tag],
title: (ui.job_group_titles && ui.job_group_titles[group.main_tag]) || group.main_tag,
jobs: []
}));
const leftovers = [];
// Each job goes to the group with the most matching tags (best fit)
translatedJobs.forEach(job => {
if (!job.tags || job.tags.length === 0) {
leftovers.push(job);
return;
}
let bestBucket = null;
let bestScore = 0;
groupBuckets.forEach(bucket => {
const score = job.tags.filter(t => bucket.matchTags.includes(t)).length;
if (score > bestScore) {
bestScore = score;
bestBucket = bucket;
}
});
if (bestBucket && bestScore > 0) {
bestBucket.jobs.push(job);
} else {
leftovers.push(job);
}
});
const resolvedGroups = groupBuckets.map(bucket => ({ title: bucket.title, jobs: bucket.jobs }));
// Remaining jobs that don't fit any group
if (leftovers.length > 0) {
const miscTitle = (ui.job_group_titles && ui.job_group_titles['misc']) || 'Other Experience';
resolvedGroups.push({ title: miscTitle, jobs: leftovers });
}
jobGroups = resolvedGroups.filter(g => g.jobs.length > 0);
}
const translatedProjects = projects.map(p => {
const t = i18n.projects && i18n.projects[p.id] ? i18n.projects[p.id] : {};
return {
...p,
title: t.title || p.id,
description: t.description || ""
};
});
// Featured project (optional, configured per referrer)
let featuredProject = null;
if (config.featured_project) {
featuredProject = translatedProjects.find(p => p.id === config.featured_project) || null;
}
// Filter featured project out of the regular projects list
const finalProjects = featuredProject
? translatedProjects.filter(p => p.id !== featuredProject.id)
: translatedProjects;
return {
config: config,
ui: ui,
general: globalBase.general,
languages: globalBase.languages.map(lang => ({
...lang,
name: (i18n.ui.languages[lang.code]) ? i18n.ui.languages[lang.code].name : lang.code,
label: (i18n.ui.languages[lang.code]) ? i18n.ui.languages[lang.code].label : ''
})),
jobs: translatedJobs,
grouped: grouped,
jobGroups: jobGroups,
profile: profile,
featuredProject: featuredProject,
education: education.map(e => ({ ...e, title: i18n.education[e.id] || e.id })),
projects: finalProjects
};
}
module.exports = bootstrapCV;