forked from orishim/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
307 lines (266 loc) · 8.43 KB
/
Copy pathserver.js
File metadata and controls
307 lines (266 loc) · 8.43 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
import express from 'express';
import cors from 'cors';
import { readFileSync, readdirSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
// Parse markdown files on startup
let usecases = [];
let markdownContent = {}; // Store raw markdown by id
// Domain categories mapping
const DOMAIN_CATEGORIES = {
"Society": [
"Advocacy & Rights",
"Global Governance",
"Education",
"Law & Regulation",
"Public Administration",
"Philanthropy & Social Services",
"Security & Defense",
"Science & Research"
],
"Finance": [
"Alternative Money",
"Credit & Capital Formation",
"Insurance",
"Public Finance & Procurement"
],
"Consumer": [
"Retail & eCommerce",
"Services & Tasks",
"Travel",
"Social",
"Media & Entertainment",
"Gaming & Autonomous Worlds"
],
"Enterprise": [
"Business Ops",
"Supply Chain",
"Transport & Logistics",
"Productivity & Collaboration",
"Intellectual Property",
"Marketing & Advertising"
],
"Digital": [
"AI",
"Data",
"IT Infrastructure",
"Hardware & IoT"
],
"Physical": [
"Energy",
"Food & Agriculture",
"Real Estate & Housing",
"Sustainability & Regeneration",
"Health & Bio"
]
};
function parseMarkdownFile(filePath, id) {
const content = readFileSync(filePath, 'utf8');
// Store raw markdown
markdownContent[id] = content;
const text = content.replace(/\r\n/g, '\n');
// Parse YAML frontmatter
let title = id;
let sector = '';
const frontmatterMatch = text.match(/^---\n([\s\S]*?)\n---/);
if (frontmatterMatch) {
const frontmatter = frontmatterMatch[1];
const titleMatch = frontmatter.match(/^title:\s*(.+)$/m);
const sectorMatch = frontmatter.match(/^sector:\s*(.+)$/m);
if (titleMatch) title = titleMatch[1].trim();
if (sectorMatch) sector = sectorMatch[1].trim();
}
// Remove frontmatter from text for section parsing
const bodyText = text.replace(/^---\n[\s\S]*?\n---\n*/, '');
// Split into sections by ## headers
const sections = bodyText.split(/^##\s+/m);
let problemStatement = '';
let ideas = [];
let projects = [];
let resources = [];
sections.forEach(section => {
if (!section.trim()) return;
const lines = section.trim().split('\n');
const header = lines[0].trim().toLowerCase();
const sectionContent = lines.slice(1).join('\n').trim();
if (header.startsWith('the opportunity')) {
problemStatement = sectionContent.trim();
} else if (header.startsWith('ideas')) {
// Parse ideas with format: - idea name - description
sectionContent.split('\n').forEach(line => {
// Match format: - idea name - description (use " - " as delimiter to handle hyphens in names)
const match = line.match(/^-\s+(.+?)\s+-\s+(.+)$/);
if (match) {
ideas.push({
title: match[1].trim(),
description: match[2].trim()
});
}
});
} else if (header.startsWith('projects')) {
// Parse projects section
sectionContent.split('\n').forEach(line => {
const match = line.trim().match(/^-?\s*\[([^\]]+)\]\(([^)]+)\)\s*[-–—:]?\s*(.*)$/);
if (match) {
projects.push({
name: match[1].trim(),
url: match[2].trim(),
description: match[3].trim()
});
}
});
} else if (header.startsWith('resources')) {
// Parse resources with format: - [title](link) (year) - description
sectionContent.split('\n').forEach(line => {
const match = line.trim().match(/^-?\s*\[([^\]]+)\]\(([^)]+)\)\s*(?:\((\d{4})\))?\s*[-–—:]?\s*(.*)$/);
if (match) {
resources.push({
title: match[1].trim(),
link: match[2].trim(),
year: match[3] || null,
description: match[4]?.trim() || ''
});
}
});
}
});
return {
id,
title,
sector,
problemStatement,
ideas,
projects,
resources
};
}
function loadUsecases() {
const dataDir = join(__dirname, 'public', 'data');
const files = readdirSync(dataDir).filter(f => f.endsWith('.md'));
usecases = files.map(file => {
const id = file.replace('.md', '');
return parseMarkdownFile(join(dataDir, file), id);
});
console.log(`Loaded ${usecases.length} use cases`);
}
// Load on startup
loadUsecases();
// API Endpoints
// GET /api/categories - Get all categories with their domains
app.get('/api/categories', (req, res) => {
// Build category response with domain details
const categories = {};
for (const [category, domainTitles] of Object.entries(DOMAIN_CATEGORIES)) {
categories[category] = domainTitles.map(title => {
const usecase = usecases.find(uc => uc.title === title);
return {
id: usecase?.id || title.toLowerCase().replace(/[^a-z0-9]+/g, '-'),
title: title,
problemStatement: usecase?.problemStatement?.substring(0, 200) + '...' || '',
ideasCount: usecase?.ideas?.length || 0,
projectsCount: usecase?.projects?.length || 0,
resourcesCount: usecase?.resources?.length || 0
};
});
}
res.json(categories);
});
// GET /api/usecases/:id/markdown - Get raw markdown for a use case
app.get('/api/usecases/:id/markdown', (req, res) => {
const id = req.params.id;
const markdown = markdownContent[id];
if (!markdown) {
return res.status(404).json({ error: 'Use case not found' });
}
res.json({ id, markdown });
});
// GET /api/markdown - Get all raw markdown content
app.get('/api/markdown', (req, res) => {
const all = usecases.map(uc => ({
id: uc.id,
title: uc.title,
markdown: markdownContent[uc.id]
}));
res.json(all);
});
// GET /api/usecases - List all use cases (summary)
app.get('/api/usecases', (req, res) => {
const summary = usecases.map(uc => ({
id: uc.id,
title: uc.title,
sector: uc.sector,
problemStatement: uc.problemStatement.substring(0, 200) + '...',
ideasCount: uc.ideas.length,
projectsCount: uc.projects.length,
resourcesCount: uc.resources.length
}));
res.json(summary);
});
// GET /api/usecases/:id - Get full use case by ID
app.get('/api/usecases/:id', (req, res) => {
const usecase = usecases.find(uc => uc.id === req.params.id);
if (!usecase) {
return res.status(404).json({ error: 'Use case not found' });
}
res.json(usecase);
});
// GET /api/search?q=query - Search use cases
app.get('/api/search', (req, res) => {
const query = (req.query.q || '').toLowerCase().trim();
if (!query) {
return res.json([]);
}
const results = [];
usecases.forEach(uc => {
let matches = [];
// Search in title
if (uc.title.toLowerCase().includes(query)) {
matches.push({ type: 'title', text: uc.title });
}
// Search in problem statement
if (uc.problemStatement.toLowerCase().includes(query)) {
matches.push({ type: 'problem', text: uc.problemStatement.substring(0, 150) + '...' });
}
// Search in ideas
uc.ideas.forEach(idea => {
if (idea.title.toLowerCase().includes(query) || idea.description.toLowerCase().includes(query)) {
matches.push({ type: 'idea', text: idea.title });
}
});
// Search in projects
uc.projects.forEach(project => {
if (project.name.toLowerCase().includes(query) || (project.description && project.description.toLowerCase().includes(query))) {
matches.push({ type: 'project', text: project.name });
}
});
// Search in resources
uc.resources.forEach(resource => {
if (resource.title.toLowerCase().includes(query) || (resource.description && resource.description.toLowerCase().includes(query))) {
matches.push({ type: 'resource', text: resource.title });
}
});
if (matches.length > 0) {
results.push({
id: uc.id,
title: uc.title,
matches: matches.slice(0, 5) // Limit matches per use case
});
}
});
res.json(results);
});
// Serve static files from dist
app.use(express.static(join(__dirname, 'dist')));
// SPA fallback - serve index.html for all non-API routes
app.get('/{*path}', (req, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
console.log(`API available at http://localhost:${PORT}/api`);
});