Repository navigation
Expand file tree
/
Copy pathgraphDataManager.js
More file actions
269 lines (246 loc) · 7.57 KB
/
Copy pathgraphDataManager.js
File metadata and controls
269 lines (246 loc) · 7.57 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
import piml from 'piml';
import { loadTagIndex, tagPath } from './tagIndex';
import { normalizeTag } from './tagSlug.mjs';
const TAG_COLOR = '#4b5563'; // Gray for tags
const POST_COLOR = '#f87171'; // Red/Primary for posts
const APP_COLOR = '#34d399'; // Emerald for apps
const PROJECT_COLOR = '#60a5fa'; // Blue for projects
const LOG_COLOR = '#fbbf24'; // Amber for discovery logs
// Pre-index builder: reads tags straight from each source. Used only when
// the generated tag index can't be loaded.
const fetchLegacyGraphData = async () => {
const nodes = [];
const links = [];
const tagMap = new Map(); // Tag -> NodeID
const linkKeys = new Set();
// Helper to add tag node and link
const addTagLink = (itemId, tag) => {
if (!tag) return;
const normalizedTag = tag.trim().toLowerCase();
// If tag node doesn't exist, create it
if (!tagMap.has(normalizedTag)) {
const tagNodeId = `tag-${normalizedTag}`;
tagMap.set(normalizedTag, tagNodeId);
nodes.push({
id: tagNodeId,
name: tag, // Keep original case for display
group: 'tag',
color: TAG_COLOR,
val: 1, // Base size
});
}
// Link item to tag (once, even if a category repeats a tag)
const linkKey = `${itemId}|${tagMap.get(normalizedTag)}`;
if (linkKeys.has(linkKey)) return;
linkKeys.add(linkKey);
links.push({
source: itemId,
target: tagMap.get(normalizedTag),
});
// Increase tag node size (popularity)
const tagNode = nodes.find((n) => n.id === tagMap.get(normalizedTag));
if (tagNode) {
tagNode.val += 0.5;
}
};
try {
// 1. Fetch Posts
const postsRes = await fetch('/posts/posts.json');
if (postsRes.ok) {
const posts = await postsRes.json();
posts.forEach((post) => {
const id = `post-${post.slug}`;
nodes.push({
id,
slug: post.slug,
name: post.title,
group: 'post',
color: POST_COLOR,
val: 2,
desc: post.description,
});
// Link Tags
if (post.tags && Array.isArray(post.tags)) {
post.tags.forEach((tag) => addTagLink(id, tag));
}
// Link Category
if (post.category) {
addTagLink(id, post.category);
}
});
}
// 2. Fetch Apps
const appsRes = await fetch('/apps/apps.json');
if (appsRes.ok) {
const appsData = await appsRes.json();
// Apps are grouped by category key
Object.entries(appsData).forEach(([category, catData]) => {
if (catData.apps && Array.isArray(catData.apps)) {
catData.apps.forEach((app) => {
const id = `app-${app.slug}`;
nodes.push({
id,
slug: app.slug,
to: app.to, // Some apps might have custom paths
name: app.title,
group: 'app',
color: APP_COLOR,
val: 2,
desc: app.description,
});
// Link Category as Tag
addTagLink(id, category);
});
}
});
}
// 3. Fetch Projects
const projectsRes = await fetch('/projects/projects.piml');
if (projectsRes.ok) {
const pimlText = await projectsRes.text();
const parsed = piml.parse(pimlText);
let projectList = [];
if (parsed.projects && Array.isArray(parsed.projects)) {
projectList = parsed.projects;
} else if (Array.isArray(parsed)) {
projectList = parsed;
}
projectList.forEach((proj) => {
const id = `project-${proj.slug}`;
nodes.push({
id,
slug: proj.slug,
name: proj.title,
group: 'project',
color: PROJECT_COLOR,
val: 3, // Projects are big
desc: proj.description,
});
// Technologies -> Tags
if (proj.technologies) {
const techs =
typeof proj.technologies === 'string'
? proj.technologies.split(',')
: proj.technologies;
if (Array.isArray(techs)) {
techs.forEach((t) => addTagLink(id, t));
}
}
// Type -> Tag
if (proj.type) {
addTagLink(id, proj.type);
}
});
}
} catch (error) {
console.error('Failed to build knowledge graph:', error);
}
return { nodes, links };
};
const COLOR = {
post: POST_COLOR,
log: LOG_COLOR,
project: PROJECT_COLOR,
app: APP_COLOR,
tag: TAG_COLOR,
};
const BASE_VAL = { post: 2, log: 2, project: 3, app: 2 };
/**
* Builds the graph from the shared tag index (public/tags/index.piml), where
* tags are already canonical across posts, logs and projects. Apps are not in
* the index, so they still come from apps.json and link to their category.
* Tags used by a single item are left out: they add dots without connecting
* anything.
*/
export const fetchGraphData = async () => {
let index;
try {
index = await loadTagIndex();
} catch (error) {
console.warn('Tag index unavailable, building the graph from sources:', error);
return fetchLegacyGraphData();
}
const nodes = [];
const links = [];
const tagNodes = new Map(); // slug -> node
const linkKeys = new Set();
const tagNode = (slug, label, description) => {
if (!tagNodes.has(slug)) {
const node = {
id: `tag-${slug}`,
slug,
name: label || slug,
group: 'tag',
color: TAG_COLOR,
val: 1,
desc: description || '',
// app categories aren't in the index, so they have no tag page
path: index.bySlug.has(slug) ? tagPath(slug) : null,
count: 0,
};
tagNodes.set(slug, node);
}
return tagNodes.get(slug);
};
const link = (itemId, slug, label, description) => {
const key = `${itemId}|${slug}`;
if (linkKeys.has(key)) return;
linkKeys.add(key);
const node = tagNode(slug, label, description);
node.count += 1;
links.push({ source: itemId, target: node.id });
};
index.items.forEach((item) => {
if (!COLOR[item.type]) return;
nodes.push({
id: item.id,
slug: item.id.split('/').pop(),
name: item.title,
group: item.type,
color: COLOR[item.type],
val: BASE_VAL[item.type],
desc: item.description,
path: item.path,
});
item.tags.forEach((slug) => {
const tag = index.bySlug.get(slug);
link(item.id, slug, tag?.label, tag?.description);
});
});
try {
const res = await fetch('/apps/apps.json');
if (res.ok) {
const appsData = await res.json();
Object.entries(appsData).forEach(([category, catData]) => {
const slug = index.resolve(category) || normalizeTag(category);
const known = index.bySlug.get(slug);
(catData.apps || []).forEach((app) => {
const id = `app/${app.slug}`;
nodes.push({
id,
slug: app.slug,
name: app.title,
group: 'app',
color: APP_COLOR,
val: BASE_VAL.app,
desc: app.description,
path: app.to || `/apps/${app.slug}`,
});
link(id, slug, known?.label || catData.name || category, known?.description);
});
});
}
} catch (error) {
console.warn('Apps could not be added to the graph:', error);
}
// drop one-off tags and their links
const kept = new Set();
tagNodes.forEach((node) => {
if (node.count < 2) return;
node.val = 1 + node.count * 0.5;
kept.add(node.id);
nodes.push(node);
});
const keptLinks = links.filter((l) => kept.has(l.target));
return { nodes, links: keptLinks };
};