Skip to content

Commit 569ce94

Browse files
authored
fix(blog): use data attributes for lightbox year/slug (#59)
1 parent dff4c92 commit 569ce94

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

src/layouts/PostLayout.astro

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ const postUrl = `https://www.codingwithcalvin.net/${slug}/`;
3131
3232
// Get the image URL for OG tags - use the src property from ImageMetadata
3333
const ogImageUrl = image?.src;
34+
35+
// Extract year for lightbox original image paths
36+
const year = date.getFullYear();
3437
---
3538

3639
<BaseLayout title={title} description={description} image={ogImageUrl} type="article">
37-
<article class="py-12">
40+
<article class="py-12" data-year={year} data-slug={slug}>
3841
<div class="container mx-auto px-4 max-w-3xl relative md:pl-8">
3942
<div class="absolute left-0 top-0 bottom-0 w-3 bg-gradient-to-b from-primary via-primary/50 to-transparent hidden md:block"></div>
4043
<div class="absolute left-3 top-0 bottom-0 w-1 bg-gradient-to-b from-[#FFB833] via-[#FFB833]/50 to-transparent hidden md:block"></div>
@@ -123,9 +126,10 @@ const ogImageUrl = image?.src;
123126
if (e.key === 'Escape') lightbox.classList.remove('active');
124127
});
125128

126-
// Get the current post slug from the URL
127-
const pathParts = window.location.pathname.split('/').filter(Boolean);
128-
const slug = pathParts[pathParts.length - 1] || pathParts[pathParts.length - 2];
129+
// Get year and slug from data attributes
130+
const article = document.querySelector('article[data-year][data-slug]');
131+
const year = article?.dataset.year;
132+
const slug = article?.dataset.slug;
129133

130134
// Make prose images clickable to open lightbox with original
131135
document.querySelectorAll('.prose img').forEach((img) => {
@@ -141,27 +145,23 @@ const ogImageUrl = image?.src;
141145
// The optimized path is like /_astro/filename.hash.webp
142146
// We need to map it back to /originals/YEAR/SLUG/filename.png
143147
const match = src.match(/\/_astro\/([^.]+)\./);
144-
if (match) {
148+
if (match && year && slug) {
145149
const baseName = match[1];
146-
// Try common extensions
147-
const year = window.location.pathname.match(/\/(\d{4})\//)?.[1];
148-
if (year && slug) {
149-
// Try to load the original
150-
const originalPath = `/originals/${year}/${slug}/${baseName}.png`;
151-
lightboxImg.src = originalPath;
152-
lightboxImg.alt = alt;
153-
lightbox.classList.add('active');
154-
155-
// Fallback to optimized if original fails
150+
// Try to load the original
151+
const originalPath = `/originals/${year}/${slug}/${baseName}.png`;
152+
lightboxImg.src = originalPath;
153+
lightboxImg.alt = alt;
154+
lightbox.classList.add('active');
155+
156+
// Fallback to optimized if original fails
157+
lightboxImg.onerror = () => {
158+
// Try jpg
159+
lightboxImg.src = `/originals/${year}/${slug}/${baseName}.jpg`;
156160
lightboxImg.onerror = () => {
157-
// Try jpg
158-
lightboxImg.src = `/originals/${year}/${slug}/${baseName}.jpg`;
159-
lightboxImg.onerror = () => {
160-
// Fall back to optimized version
161-
lightboxImg.src = src;
162-
};
161+
// Fall back to optimized version
162+
lightboxImg.src = src;
163163
};
164-
}
164+
};
165165
} else {
166166
// Use the src as-is if we can't parse it
167167
lightboxImg.src = src;

0 commit comments

Comments
 (0)