Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
502 lines (427 loc) · 17.3 KB
/
Copy pathapp.js
File metadata and controls
502 lines (427 loc) · 17.3 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Configuration
const GITHUB_RAW_URL = 'https://raw.githubusercontent.com/promptslab/Awesome-Prompt-Engineering/main/README.md';
const CACHE_KEY = 'awesome_prompt_cache';
const CACHE_DURATION = 1000 * 60 * 30; // 30 minutes
// Initialize app
document.addEventListener('DOMContentLoaded', () => {
loadContent();
setupSmoothScroll();
});
// Setup smooth scrolling for navigation
function setupSmoothScroll() {
const sidebarLinks = document.querySelectorAll('.sidebar-link');
sidebarLinks.forEach(link => {
link.addEventListener('click', function (e) {
e.preventDefault();
// Remove active class from all links
sidebarLinks.forEach(l => l.classList.remove('active'));
// Add active class to clicked link
this.classList.add('active');
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
const offset = 100; // Account for navbar
const targetPosition = targetElement.offsetTop - offset;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
}
});
});
// Update active state on scroll
window.addEventListener('scroll', updateActiveLink);
}
// Update active link based on scroll position
function updateActiveLink() {
const sidebarLinks = document.querySelectorAll('.sidebar-link');
const sections = document.querySelectorAll('.category-section');
let currentSection = '';
sections.forEach(section => {
const sectionTop = section.offsetTop - 150;
const sectionBottom = sectionTop + section.offsetHeight;
if (window.scrollY >= sectionTop && window.scrollY < sectionBottom) {
currentSection = section.getAttribute('id');
}
});
sidebarLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${currentSection}`) {
link.classList.add('active');
}
});
}
// Load content from GitHub or cache
async function loadContent() {
try {
// Check cache first
const cached = getCache();
if (cached) {
displayContent(cached);
return;
}
let markdown = '';
// Try local file first (for development)
try {
const localResponse = await fetch('data.md');
if (localResponse.ok) {
markdown = await localResponse.text();
}
} catch (e) {
console.log('Local file not found, fetching from GitHub...');
}
// Fallback to GitHub
if (!markdown) {
const response = await fetch(GITHUB_RAW_URL);
if (!response.ok) throw new Error('Failed to fetch README');
markdown = await response.text();
}
const parsedContent = parseMarkdown(markdown);
// Cache the results
setCache(parsedContent);
displayContent(parsedContent);
} catch (error) {
console.error('Error loading content:', error);
showError();
}
}
// Parse markdown content - comprehensive parser
function parseMarkdown(markdown) {
const sections = {};
const lines = markdown.split('\n');
let currentSection = null;
let currentSubsection = null;
let currentContent = [];
let inTable = false;
let tableHeaders = [];
let tableRows = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const trimmed = line.trim();
// Skip header/badges area and emoji-only lines
if (i < 50 && (trimmed.startsWith('<') || trimmed.startsWith('```') || !trimmed)) {
continue;
}
// Skip emoji-only lines (emoji can be 1-4 bytes)
if (trimmed.length <= 4 && /^[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]+$/u.test(trimmed)) {
continue;
}
// Main section headers (## Papers, ## Tools, etc.)
if (trimmed.startsWith('## ') &&
!trimmed.includes('Table of Contents') &&
!trimmed.includes('How to Contribute')) {
// Save previous section
if (currentSection) {
saveSectionContent(sections, currentSection, currentSubsection, currentContent, inTable, tableHeaders, tableRows);
}
// Remove ## and clean up emoji and extra whitespace
currentSection = trimmed
.replace('## ', '')
.replace(/[📄🛠️🔌📊🤖🔍👩🏫📚🎥🤝💬🔧💻💾🧠]/g, '')
.trim();
currentSubsection = null;
currentContent = [];
inTable = false;
tableHeaders = [];
tableRows = [];
sections[currentSection] = { type: 'section', subsections: {}, content: [] };
continue;
}
// Subsection headers (- **Something**:)
if (currentSection && trimmed.match(/^-\s*\*\*(.+?)\*\*:?\s*$/)) {
// Save previous subsection
if (currentSubsection) {
saveSectionContent(sections, currentSection, currentSubsection, currentContent, inTable, tableHeaders, tableRows);
}
const match = trimmed.match(/^-\s*\*\*(.+?)\*\*:?\s*$/);
currentSubsection = match[1];
currentContent = [];
inTable = false;
tableHeaders = [];
tableRows = [];
continue;
}
// Table detection
if (currentSection && trimmed.startsWith('|')) {
if (!inTable) {
inTable = true;
tableHeaders = trimmed.split('|').map(h => h.trim()).filter(h => h);
} else if (trimmed.includes('---')) {
// Separator line, skip
continue;
} else {
// Table row
const cells = trimmed.split('|').map(c => c.trim()).filter(c => c);
tableRows.push(cells);
}
continue;
} else if (inTable && !trimmed.startsWith('|')) {
// End of table - save it immediately
if (tableHeaders.length > 0 && tableRows.length > 0) {
if (!currentSubsection) {
sections[currentSection].table = { headers: tableHeaders, rows: tableRows };
console.log('Saved table for section:', currentSection, 'with', tableRows.length, 'rows');
}
}
inTable = false;
tableHeaders = [];
tableRows = [];
}
// Regular content (links, text, etc.)
if (currentSection && trimmed && !trimmed.startsWith('#')) {
currentContent.push(trimmed);
}
}
// Save last section
if (currentSection) {
saveSectionContent(sections, currentSection, currentSubsection, currentContent, inTable, tableHeaders, tableRows);
}
return sections;
}
// Helper to save section content
function saveSectionContent(sections, section, subsection, content, inTable, tableHeaders, tableRows) {
// Save table at section level if no subsection
if (!subsection && inTable && tableHeaders.length > 0) {
sections[section].table = { headers: tableHeaders, rows: tableRows };
console.log('Saved table for section:', section, 'with', tableRows.length, 'rows');
}
// Save to appropriate target
const target = subsection
? (sections[section].subsections[subsection] = sections[section].subsections[subsection] || { content: [], table: null })
: sections[section];
// Save subsection table
if (subsection && inTable && tableHeaders.length > 0) {
target.table = { headers: tableHeaders, rows: tableRows };
}
// Save content
if (content.length > 0) {
if (!target.content) target.content = [];
target.content = target.content.concat(content);
}
}
// Display all content
function displayContent(sections) {
const container = document.getElementById('contentContainer');
if (!container) return;
console.log('Sections found:', Object.keys(sections)); // Debug
let html = '';
// Define section order and icons
const sectionConfig = {
'Papers': { id: 'papers', icon: '📄' },
'Tools & Code': { id: 'tools', icon: '🛠️' },
'Apis': { id: 'apis', icon: '🔌' },
'Datasets': { id: 'datasets', icon: '📊' },
'Models': { id: 'models', icon: '🤖' },
'AI Content Detectors': { id: 'detectors', icon: '🔍' },
'Educational': { id: 'educational', icon: '📚' },
'Courses': { id: 'courses', icon: '👩🏫' },
'Tutorials': { id: 'tutorials', icon: '📚' },
'Videos': { id: 'videos', icon: '🎥' },
'Books': { id: 'books', icon: '📖' },
'Communities': { id: 'communities', icon: '🤝' }
};
Object.entries(sections).forEach(([sectionName, sectionData]) => {
console.log('Processing section:', sectionName); // Debug
const config = sectionConfig[sectionName];
if (!config) {
console.log('Skipping section (not in config):', sectionName);
return; // Skip sections not in our config
}
html += `
<div class="category-section" id="${config.id}">
<div class="category-header">
<span class="category-icon">${config.icon}</span>
<h2>${sectionName}</h2>
</div>
`;
// Check if this section has a table
if (sectionData.table) {
html += renderTable(sectionData.table);
}
// Check if section has subsections
if (Object.keys(sectionData.subsections).length > 0) {
html += '<div class="resource-grid">';
Object.entries(sectionData.subsections).forEach(([subName, subData]) => {
html += `<h3 style="grid-column: 1/-1; color: var(--text-secondary); font-size: 1.25rem; margin: 1.5rem 0 1rem; border-bottom: 2px solid var(--border-color); padding-bottom: 0.75rem;">${subName}</h3>`;
const subContent = renderContent(subData.content || []);
if (subContent) {
html += subContent;
} else {
console.log('No content for subsection:', subName);
}
});
html += '</div>';
} else if (sectionData.content && sectionData.content.length > 0) {
// No subsections, render content directly only if there's content
html += '<div class="resource-grid">';
const mainContent = renderContent(sectionData.content || []);
if (mainContent) {
html += mainContent;
} else {
console.log('No content for section:', sectionName);
}
html += '</div>';
}
// If section only has a table and no content, that's fine - table already rendered above
html += '</div>';
});
container.innerHTML = html;
}
// Render table
function renderTable(tableData) {
let html = '<div class="table-container"><table class="content-table"><thead><tr>';
tableData.headers.forEach(header => {
html += `<th>${header}</th>`;
});
html += '</tr></thead><tbody>';
tableData.rows.forEach(row => {
html += '<tr>';
row.forEach(cell => {
// Parse markdown formatting
let parsedCell = cell
// Remove bold markers
.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
// Parse links with double brackets
.replace(/\[\[(.+?)\]\]\((.+?)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>')
// Parse regular markdown links
.replace(/\[(.+?)\]\((.+?)\)/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>');
html += `<td>${parsedCell}</td>`;
});
html += '</tr>';
});
html += '</tbody></table></div>';
return html;
}
// Render content items
function renderContent(items) {
let html = '';
if (!items || items.length === 0) {
return html;
}
console.log('Rendering', items.length, 'items');
items.forEach((item, index) => {
const trimmed = item.trim();
if (!trimmed) return;
// Skip emoji-only items
if (trimmed.length <= 4 && /^[\u{1F300}-\u{1F9FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]+$/u.test(trimmed)) {
return;
}
if (index < 3) {
console.log('Sample item:', trimmed.substring(0, 100));
}
// Parse different types of content
if (trimmed.startsWith('-')) {
// Remove the leading dash and spaces
const content = trimmed.replace(/^-\s*/, '');
// Check if it's a link
const linkMatch = content.match(/\[(.+?)\]\((.+?)\)(.*)$/);
if (linkMatch) {
const title = linkMatch[1];
const url = linkMatch[2];
let description = linkMatch[3] ? linkMatch[3].trim() : '';
// Extract metadata
let year = '';
let source = '';
const yearMatch = description.match(/\[(\d{4})\]/);
const sourceMatch = description.match(/\(([^)]+)\)\s*$/);
if (yearMatch) year = yearMatch[1];
if (sourceMatch) source = sourceMatch[1];
// Clean description
description = description
.replace(/\[(\d{4})\]/g, '')
.replace(/\(([^)]+)\)\s*$/g, '')
.replace(/^[,\s-]+/, '')
.trim();
html += createResourceCard({ title, url, description, year, source });
} else {
// Plain text item (no link)
if (content) {
html += `<div class="resource-card"><p>${content}</p></div>`;
}
}
} else {
// Direct text (no dash), might be from courses/tutorials sections
const linkMatch = trimmed.match(/\[(.+?)\]\((.+?)\)(.*)$/);
if (linkMatch) {
const title = linkMatch[1];
const url = linkMatch[2];
let description = linkMatch[3] ? linkMatch[3].trim() : '';
// Clean description
description = description
.replace(/^[,\s]+by\s+/, 'by ')
.trim();
html += createResourceCard({ title, url, description });
} else if (trimmed) {
// Plain text
html += `<div class="resource-card"><p>${trimmed}</p></div>`;
}
}
});
return html;
}
// Create resource card HTML
function createResourceCard(resource) {
const metaInfo = [];
if (resource.year) metaInfo.push(`📅 ${resource.year}`);
if (resource.source) metaInfo.push(`📍 ${resource.source}`);
return `
<div class="resource-card">
<h3><a href="${resource.url}" target="_blank" rel="noopener noreferrer">${resource.title}</a></h3>
${resource.description ? `<p>${resource.description}</p>` : ''}
${metaInfo.length > 0 ? `
<div class="resource-meta">
${metaInfo.join(' • ')}
</div>
` : ''}
</div>
`;
}
// Show error state
function showError() {
const container = document.getElementById('contentContainer');
if (container) {
container.innerHTML = `
<div class="empty-state">
<h3>Oops! Something went wrong</h3>
<p>Unable to load resources. Please try again later.</p>
<button onclick="location.reload()" style="
margin-top: 1rem;
padding: 0.75rem 1.5rem;
background: var(--primary-color);
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 1rem;
">Retry</button>
</div>
`;
}
}
// Cache management
function getCache() {
try {
const cached = localStorage.getItem(CACHE_KEY);
if (!cached) return null;
const { data, timestamp } = JSON.parse(cached);
const now = Date.now();
if (now - timestamp > CACHE_DURATION) {
localStorage.removeItem(CACHE_KEY);
return null;
}
return data;
} catch (error) {
console.error('Cache error:', error);
return null;
}
}
function setCache(data) {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify({
data,
timestamp: Date.now()
}));
} catch (error) {
console.error('Cache error:', error);
}
}