-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
525 lines (429 loc) · 17.4 KB
/
script.js
File metadata and controls
525 lines (429 loc) · 17.4 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Check for dark mode preference
function setThemePreference() {
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark" || (savedTheme === null && prefersDarkScheme)) {
document.body.classList.add("dark-theme");
}
}
// Add theme toggle button
function createThemeToggle() {
const themeToggle = document.createElement("button");
themeToggle.className = "theme-toggle";
themeToggle.setAttribute("aria-label", "Toggle dark mode");
themeToggle.textContent = "🌓";
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark-theme");
localStorage.setItem(
"theme",
document.body.classList.contains("dark-theme") ? "dark" : "light",
);
});
document.body.appendChild(themeToggle);
}
// Fetch and parse posts
async function loadPosts() {
try {
const response = await fetch("/posts.json");
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const posts = await response.json();
return posts;
} catch (error) {
console.error("Error loading posts:", error);
return [];
}
}
// Clear all active filter states
function clearAllActiveStates() {
// Clear tag active states
document.querySelectorAll(".tag").forEach((t) => {
t.classList.remove("active");
});
// Clear timeline active states
document.querySelectorAll(".year-label, .month-letter").forEach((el) => {
el.classList.remove("active");
});
}
// Render posts
function renderPosts(posts, filterTag = null, searchTerm = null, isExactMatch = false) {
const postsContainer = document.getElementById("posts-list");
postsContainer.innerHTML = "";
const filteredPosts = filterTag ? posts.filter((post) => post.tags.includes(filterTag)) : posts;
if (filteredPosts.length === 0) {
postsContainer.innerHTML = "<p>No posts found.</p>";
return;
}
filteredPosts.forEach((post) => {
const postElement = document.createElement("div");
postElement.className = "post-item";
const dateElement = document.createElement("span");
dateElement.className = "post-date";
dateElement.textContent = new Date(post.date).toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
const titleElement = document.createElement("h3");
titleElement.className = "post-title";
const linkElement = document.createElement("a");
linkElement.href = post.url;
// Highlight search matches in title if search term is provided
if (searchTerm) {
const titleText = post.title;
// For exact matches or regular matches, create appropriate regex
let searchRegex;
if (isExactMatch) {
// Simple highlighting for exact phrase matches
searchRegex = new RegExp(searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi');
} else {
// Try to highlight whole words first, fall back to any match
searchRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b|${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`, 'gi');
}
if (searchRegex.test(titleText)) {
linkElement.innerHTML = titleText.replace(searchRegex, match => `<span class="highlight-match">${match}</span>`);
} else {
linkElement.textContent = titleText;
}
} else {
linkElement.textContent = post.title;
}
const tagsElement = document.createElement("div");
tagsElement.className = "post-tags";
post.tags.forEach((tag) => {
const tagElement = document.createElement("span");
tagElement.className = "post-tag";
// Highlight search matches in tags
if (searchTerm) {
// Create appropriate regex for tag highlighting
let searchRegex;
if (isExactMatch) {
// For exact phrase matches
searchRegex = new RegExp(searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi');
} else {
// For word boundary matches
searchRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b|${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}`, 'gi');
}
if (searchRegex.test(tag)) {
tagElement.innerHTML = tag.replace(searchRegex, match => `<span class="highlight-match">${match}</span>`);
} else {
tagElement.textContent = tag;
}
} else {
tagElement.textContent = tag;
}
tagsElement.appendChild(tagElement);
});
titleElement.appendChild(linkElement);
postElement.appendChild(dateElement);
postElement.appendChild(titleElement);
postElement.appendChild(tagsElement);
// Extract and add TL;DR if available
if (post.content) {
const tldrPattern = /<p><strong>TL;DR:<\/strong>([\s\S]*?)(?=<\/p>)/i;
const tldrMatch = post.content.match(tldrPattern);
if (tldrMatch && tldrMatch[1]) {
const tldrContent = tldrMatch[1].replace(/<!--more-->/g, "").trim();
const tldrElement = document.createElement("p");
tldrElement.className = "post-tldr";
// Highlight search matches in TL;DR
if (searchTerm) {
// Create appropriate regex for TL;DR highlighting
let searchRegex;
if (isExactMatch) {
// For exact phrase matches
searchRegex = new RegExp(searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi');
} else {
// For word boundary matches
searchRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'gi');
}
const highlightedTldr = "<strong>TL;DR:</strong>" + tldrContent.replace(searchRegex, match => `<span class="highlight-match">${match}</span>`);
tldrElement.innerHTML = highlightedTldr;
} else {
tldrElement.innerHTML = "<strong>TL;DR:</strong>" + tldrContent;
}
postElement.appendChild(tldrElement);
}
// Add excerpt showing the search match if not in TL;DR
if (searchTerm) {
// Strip HTML for plain text searching
const plainContent = post.content.replace(/<[^>]*>/g, '');
// Create regex for finding matches
let searchRegex;
if (isExactMatch) {
searchRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'i');
} else {
searchRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'i');
}
// Find the position of the first match in content
const match = plainContent.search(searchRegex);
// If match found in content (not just in title or tags)
if (match >= 0) {
// Calculate start and end positions for the excerpt
const excerptStart = Math.max(0, match - 50);
const excerptEnd = Math.min(plainContent.length, match + searchTerm.length + 70);
// Extract the excerpt
let excerpt = plainContent.substring(excerptStart, excerptEnd);
// Add ellipsis at the start/end if needed
if (excerptStart > 0) excerpt = '...' + excerpt;
if (excerptEnd < plainContent.length) excerpt = excerpt + '...';
// Create highlight regex for display
let highlightRegex;
if (isExactMatch) {
highlightRegex = new RegExp(searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'gi');
} else {
highlightRegex = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'gi');
}
// Highlight the search term in the excerpt
excerpt = excerpt.replace(highlightRegex, match => `<span class="highlight-match">${match}</span>`);
// Create and append the excerpt element
const excerptElement = document.createElement('p');
excerptElement.className = 'search-match-excerpt';
excerptElement.innerHTML = `<strong>Found:</strong> ${excerpt}`;
postElement.appendChild(excerptElement);
}
}
}
postsContainer.appendChild(postElement);
});
}
// Render tags
function renderTags(posts) {
const tagsContainer = document.getElementById("tags");
tagsContainer.innerHTML = "";
// Extract unique tags
const allTags = posts.flatMap((post) => post.tags);
const uniqueTags = [...new Set(allTags)].sort();
uniqueTags.forEach((tag) => {
const tagElement = document.createElement("span");
tagElement.className = "tag";
tagElement.textContent = tag;
tagElement.addEventListener("click", () => {
// Check if this tag is already active
const isActive = tagElement.classList.contains("active");
// Clear active state from all tags
document.querySelectorAll(".tag").forEach((t) => {
t.classList.remove("active");
});
// If tag wasn't active, activate it and filter posts
// If it was already active, leave it deactivated and show all posts
if (!isActive) {
tagElement.classList.add("active");
renderPosts(posts, tag);
} else {
// Show all posts when unselecting a tag
clearAllActiveStates();
renderPosts(posts);
}
});
tagsContainer.appendChild(tagElement);
});
// Add event listener to show all button
document.getElementById("show-all-btn").addEventListener("click", () => {
clearAllActiveStates();
renderPosts(posts);
});
}
// Generate posts.json from markdown files
function generatePostsJson() {
// This function is for demonstration purposes
// In a real implementation, this would be done by a build script
console.log("Posts data loaded from JSON file");
}
// Toggle tags visibility
function setupTagsToggle() {
const toggleButton = document.getElementById("toggle-tags");
const tagsContainer = document.getElementById("tags-container");
const chevron = toggleButton.querySelector(".chevron");
// Make entire header clickable
document.querySelector(".tag-header").addEventListener("click", () => {
tagsContainer.classList.toggle("collapsed");
// Rotate chevron when expanded/collapsed
if (tagsContainer.classList.contains("collapsed")) {
chevron.style.transform = "rotate(0deg)";
} else {
chevron.style.transform = "rotate(90deg)";
}
});
}
// Generate timeline strip
function renderTimeline(posts) {
const timelineContainer = document.getElementById("timeline-strip");
if (!timelineContainer) return;
// Extract dates and organize by year/month
const dateMap = new Map();
posts.forEach((post) => {
const date = new Date(post.date);
const year = date.getFullYear();
const month = date.getMonth();
if (!dateMap.has(year)) {
dateMap.set(year, new Set());
}
dateMap.get(year).add(month);
});
// Sort years in descending order
const years = [...dateMap.keys()].sort((a, b) => b - a);
// Create timeline elements
years.forEach((year) => {
const yearContainer = document.createElement("div");
yearContainer.className = "year-container";
const yearLabel = document.createElement("span");
yearLabel.className = "year-label";
yearLabel.textContent = year;
yearLabel.setAttribute("data-year", year);
yearLabel.addEventListener("click", () => {
// Toggle year selection
const isActive = yearLabel.classList.contains("active");
// Clear all active states
document.querySelectorAll(".year-label, .month-dot").forEach((el) => {
el.classList.remove("active");
});
if (!isActive) {
yearLabel.classList.add("active");
// Filter posts by year
const filteredPosts = posts.filter((post) => {
const postDate = new Date(post.date);
return postDate.getFullYear() === year;
});
renderPosts(filteredPosts);
} else {
// Show all posts
clearAllActiveStates();
renderPosts(posts);
}
});
const monthLetters = document.createElement("div");
monthLetters.className = "month-letters";
// Create letters only for months that have posts
const monthsWithPosts = [...dateMap.get(year)].sort((a, b) => a - b);
monthsWithPosts.forEach((month) => {
const monthElement = document.createElement("div");
monthElement.className = "month-letter has-posts";
monthElement.setAttribute("data-month", month);
monthElement.setAttribute("data-year", year);
// Get first letter and full month name for enhanced tooltip
const monthFull = new Date(year, month).toLocaleString("default", { month: "long" });
monthElement.textContent = monthFull[0];
monthElement.title = `${monthFull} ${year}`;
monthElement.setAttribute("data-month-name", monthFull);
monthElement.addEventListener("click", () => {
// Toggle month selection
const isActive = monthElement.classList.contains("active");
// Clear all active states
document.querySelectorAll(".year-label, .month-letter").forEach((el) => {
el.classList.remove("active");
});
if (!isActive) {
monthElement.classList.add("active");
yearLabel.classList.add("active");
// Filter posts by month and year
const filteredPosts = posts.filter((post) => {
const postDate = new Date(post.date);
return postDate.getFullYear() === year && postDate.getMonth() === month;
});
renderPosts(filteredPosts);
} else {
// Show all posts
clearAllActiveStates();
renderPosts(posts);
}
});
monthLetters.appendChild(monthElement);
});
yearContainer.appendChild(yearLabel);
yearContainer.appendChild(monthLetters);
timelineContainer.appendChild(yearContainer);
});
}
// Search functionality
function setupSearch(posts) {
const searchInput = document.getElementById('search-input');
if (!searchInput) return;
let searchTimeout;
// Function to perform the search
const performSearch = (searchTerm) => {
const originalSearchTerm = searchTerm.trim();
searchTerm = originalSearchTerm.toLowerCase();
if (!searchTerm) {
clearAllActiveStates();
renderPosts(posts);
return;
}
// Check if the search is for an exact phrase (enclosed in quotes)
const isExactMatch = /^".*"$/.test(originalSearchTerm);
let exactSearchTerm = '';
if (isExactMatch) {
// Remove the quotes for searching
exactSearchTerm = searchTerm.substring(1, searchTerm.length - 1);
}
// Filter posts that match the search term in title, content, or tags
const filteredPosts = posts.filter(post => {
// Strip HTML tags for plain text search in content
const plainContent = post.content ? post.content.replace(/<[^>]*>/g, '') : '';
// For exact matches (with quotes)
if (isExactMatch) {
// Create word boundary regex pattern for the exact term
const exactWordPattern = new RegExp(`\\b${exactSearchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'i');
// Check for word boundary matches in each area
const titleMatch = exactWordPattern.test(post.title.toLowerCase());
const tagMatch = post.tags.some(tag => exactWordPattern.test(tag.toLowerCase()));
const contentMatch = exactWordPattern.test(plainContent.toLowerCase());
return titleMatch || tagMatch || contentMatch;
} else {
// For regular searches (without quotes), use word boundary
const wordPattern = new RegExp(`\\b${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')}\\b`, 'i');
// Check for whole word matches
const titleMatch = wordPattern.test(post.title.toLowerCase());
const tagMatch = post.tags.some(tag => wordPattern.test(tag.toLowerCase()));
const contentMatch = wordPattern.test(plainContent.toLowerCase());
return titleMatch || tagMatch || contentMatch;
}
});
// Use original search term for display, but handle quotes for highlighting
const highlightTerm = isExactMatch ? exactSearchTerm : searchTerm;
renderPosts(filteredPosts, null, highlightTerm, isExactMatch);
// Show message when no results
const postsContainer = document.getElementById("posts-list");
if (filteredPosts.length === 0) {
postsContainer.innerHTML = `<p>No posts found matching <strong>${isExactMatch ? `"${exactSearchTerm}"` : searchTerm}</strong>. Try a different search term.</p>`;
}
};
// Event handler for input (typing)
searchInput.addEventListener('input', (e) => {
clearTimeout(searchTimeout);
// Debounce search to prevent excessive rendering on each keystroke
searchTimeout = setTimeout(() => {
performSearch(e.target.value);
}, 300); // 300ms debounce delay
});
// Event handler for Enter key press
searchInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
clearTimeout(searchTimeout);
performSearch(searchInput.value);
}
});
// Add clear button functionality
const clearButton = document.querySelector('.search-clear');
if (clearButton) {
clearButton.addEventListener('click', () => {
searchInput.value = '';
clearAllActiveStates();
renderPosts(posts);
searchInput.focus();
});
}
}
// Initialize
document.addEventListener("DOMContentLoaded", async () => {
setThemePreference();
createThemeToggle();
const posts = await loadPosts();
renderPosts(posts);
renderTags(posts);
renderTimeline(posts);
setupTagsToggle();
setupSearch(posts);
});