On this day
| 11ty, jsNudged by org-daily-reflection (@emacsomancer's toot) and Jeremy Keith's post where he mentions his on this day page, I finally got around to making my own on this day page again. I use the 11ty static site generator, so it's static unless you have Javascript enabled. It might be good for bumping into things. I used to have an "On this day" widget back when I used Wordpress, which was fun to look at occasionally.
The code might be a little adamant about converting all the dates to America/Toronto:
11ty code for posts on this day
export default class OnThisDay { data() { return { layout: 'layouts/base', permalink: '/blog/on-this-day/', title: 'On this day' }; } async render(data) { const today = new Date(new Date().toLocaleString('en-US', { timeZone: 'America/Toronto' })); const options = { month: 'long', day: 'numeric' }; const date = today.toLocaleDateString('en-US', options); const currentMonthDay = today.toISOString().substring(5, 10); let list = data.collections._posts .filter(post => { const postDateTime = new Date(post.date).toLocaleString('en-US', { timeZone: 'America/Toronto' }); const postMonthDay = (new Date(postDateTime)).toISOString().substring(5, 10); return postMonthDay === currentMonthDay; }) .sort((a, b) => { if (a.date < b.date) return 1; if (a.date > b.date) return -1; return 0; }) .map(post => { const postDateTime = new Date(post.date).toLocaleString('en-US', { timeZone: 'America/Toronto' }); const postDate = new Date(postDateTime); const postYear = postDate.getFullYear(); return `<li>${postYear}: <a href="${post.url}">${post.data.title}</a></li>`; }) .join('\n'); list = list.length > 0 ? `<ul>${list}</ul>` : `<p>No posts were written on ${date} in previous years.</p>`; return `<section><h2>On this day</h2> <p>This page lists posts written on this day throughout the years. If you've enabled Javascript, it will show the current day. If you don't, it'll show the posts from the day I last updated this blog. You might also like to explore <a href="/blog/all">all posts</a>, <a href="/topic">a topic-based outline</a> or <a href="/blog/category">categories</a>.</p> <h3 class="date">${date}</h3> <div id="posts-container">${list}</div> <script> $(document).ready(function() { onThisDay(); }); </script> </section>`; } };
Client-side Javascript for the dynamic list
function onThisDay() { const tz = 'America/Toronto'; function getEffectiveDate() { const urlParams = new URLSearchParams(window.location.search); const dateParam = urlParams.get('date'); if (dateParam && /^\d{2}-\d{2}$/.test(dateParam)) { const currentYear = new Date().getFullYear(); const dateObj = new Date(`${currentYear}-${dateParam}T12:00:00Z`); if (dateObj.getTime()) { return { monthDay: dateParam, formatted: dateObj.toLocaleDateString('en-US', { month: 'long', day: 'numeric' }) }; } } const today = new Date(new Date().toLocaleString('en-US', { timeZone: tz })); return { monthDay: today.toISOString().substring(5, 10), // MM-DD formatted: today.toLocaleDateString('en-US', { month: 'long', day: 'numeric' }) }; } // Fetch and process the posts fetch('/blog/all/index.json') .then(response => response.json()) .then(posts => { const dateInfo = getEffectiveDate(); const dateElement = document.querySelector('h3.date'); if (dateElement) { dateElement.textContent = dateInfo.formatted; } const matchingPosts = posts.filter(post => { const postDate = new Date(post.date).toLocaleString('en-US', { timeZone: tz }); const postMonthDay = (new Date(postDate)).toISOString().substring(5, 10); return postMonthDay === dateInfo.monthDay; }); matchingPosts.sort((a, b) => { const dateA = new Date(a.date); const dateB = new Date(b.date); return dateB - dateA; }); const elem = document.getElementById('posts-container'); if (matchingPosts.length > 0) { const postsHTML = matchingPosts.map(post => { const postDate = new Date(post.date).toLocaleString('en-US', { timeZone: tz }); const postYear = new Date(postDate).getFullYear(); return `<li>${postYear}: <a href="${post.permalink}">${post.title}</a></li>`; }).join('\n'); elem.innerHTML = `<ul>${postsHTML}</ul>`; } else { elem.innerHTML = `<p>No posts were written on ${dateInfo.formatted}.</p>`; } }) .catch(error => { console.error('Error fetching posts:', error); }); }
I used to include the day's posts as a footer on the individual blog post page. That might be something to consider again.