-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostLayout.astro
More file actions
212 lines (186 loc) · 7.55 KB
/
PostLayout.astro
File metadata and controls
212 lines (186 loc) · 7.55 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
---
import { Image } from 'astro:assets';
import type { ImageMetadata } from 'astro';
import BaseLayout from './BaseLayout.astro';
import CategoryBadges from '../components/CategoryBadges.astro';
import YouTubeEmbed from '../components/YouTubeEmbed.astro';
import BlueskyEngagement from '../components/BlueskyEngagement.astro';
interface Props {
title: string;
subtitle?: string;
slug: string;
date: Date;
categories: string[];
description?: string;
image?: ImageMetadata;
youtube?: string;
blueskyPostId?: string;
}
const { title, subtitle, slug, date, categories, description, image, youtube, blueskyPostId } = Astro.props;
const formattedDate = date.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
timeZone: 'America/New_York'
});
const postUrl = `https://www.codingwithcalvin.net/${slug}/`;
// Get the image URL for OG tags - use the src property from ImageMetadata
const ogImageUrl = image?.src;
---
<BaseLayout title={title} description={description} image={ogImageUrl} type="article">
<article class="py-12">
<div class="container mx-auto px-4 max-w-3xl relative md:pl-8">
<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>
<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>
<header class="mb-8">
<time class="text-text-muted text-sm">{formattedDate}</time>
<h1 class="text-4xl font-heading mt-2 mb-2">{title}</h1>
{subtitle && (
<p class="text-[#FFB833] text-xl mb-4">{subtitle}</p>
)}
<CategoryBadges categories={categories} />
</header>
{youtube && (
<div class="mb-8">
<YouTubeEmbed url={youtube} title={title} />
</div>
)}
{!youtube && image && (
<div class="mb-8 rounded-lg overflow-hidden">
<Image
src={image}
alt={title}
quality={80}
format="webp"
class="w-full"
/>
</div>
)}
<div class="prose prose-lg max-w-none">
<slot />
</div>
<script>
import mermaid from 'mermaid';
mermaid.initialize({
startOnLoad: false,
theme: 'dark'
});
// Find all mermaid code blocks (Shiki uses data-language attribute)
document.querySelectorAll('pre[data-language="mermaid"]').forEach(async (pre) => {
const code = pre.textContent || '';
const div = document.createElement('div');
div.className = 'mermaid';
const { svg } = await mermaid.render('mermaid-' + Math.random().toString(36).substr(2, 9), code);
div.innerHTML = svg;
pre.replaceWith(div);
});
// Create lightbox overlay
const lightbox = document.createElement('div');
lightbox.className = 'lightbox-overlay';
lightbox.innerHTML = `
<div class="lightbox-header">
<span class="lightbox-title">Full-size image</span>
<span class="lightbox-hint">Click outside or press Esc to close</span>
</div>
<div class="lightbox-container">
<img src="" alt="" />
</div>
<button class="lightbox-close" aria-label="Close">×</button>
<div class="lightbox-footer">
<span>Click anywhere outside the image to close</span>
</div>
`;
document.body.appendChild(lightbox);
const lightboxImg = lightbox.querySelector('.lightbox-container img');
const closeBtn = lightbox.querySelector('.lightbox-close');
const container = lightbox.querySelector('.lightbox-container');
// Close lightbox on overlay click (but not container), close button, or Escape key
lightbox.addEventListener('click', (e) => {
if (e.target === lightbox || e.target === closeBtn || e.target.closest('.lightbox-header') || e.target.closest('.lightbox-footer')) {
lightbox.classList.remove('active');
}
});
// Prevent closing when clicking the container
container.addEventListener('click', (e) => e.stopPropagation());
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') lightbox.classList.remove('active');
});
// Get the current post slug from the URL
const pathParts = window.location.pathname.split('/').filter(Boolean);
const slug = pathParts[pathParts.length - 1] || pathParts[pathParts.length - 2];
// Make prose images clickable to open lightbox with original
document.querySelectorAll('.prose img').forEach((img) => {
// Skip if already wrapped in a link
if (img.parentElement?.tagName === 'A') return;
img.addEventListener('click', () => {
// Extract the filename from the optimized src
const src = img.getAttribute('src') || '';
const alt = img.getAttribute('alt') || '';
// Try to find the original image
// The optimized path is like /_astro/filename.hash.webp
// We need to map it back to /originals/YEAR/SLUG/filename.png
const match = src.match(/\/_astro\/([^.]+)\./);
if (match) {
const baseName = match[1];
// Try common extensions
const year = window.location.pathname.match(/\/(\d{4})\//)?.[1];
if (year && slug) {
// Try to load the original
const originalPath = `/originals/${year}/${slug}/${baseName}.png`;
lightboxImg.src = originalPath;
lightboxImg.alt = alt;
lightbox.classList.add('active');
// Fallback to optimized if original fails
lightboxImg.onerror = () => {
// Try jpg
lightboxImg.src = `/originals/${year}/${slug}/${baseName}.jpg`;
lightboxImg.onerror = () => {
// Fall back to optimized version
lightboxImg.src = src;
};
};
}
} else {
// Use the src as-is if we can't parse it
lightboxImg.src = src;
lightboxImg.alt = alt;
lightbox.classList.add('active');
}
});
});
</script>
<blockquote class="mt-12 border-l-4 border-primary bg-background-2 rounded-r-lg p-6 text-center">
<p class="text-text-muted italic">
This post, "{title}", first appeared on<br />
<a href={postUrl} class="text-primary hover:underline">{postUrl}</a>
</p>
</blockquote>
<hr class="mt-12 border-t border-[#FFB833]/30" />
{blueskyPostId && (
<>
<BlueskyEngagement postId={blueskyPostId} />
<hr class="mt-12 border-t border-[#FFB833]/30" />
</>
)}
<section class="mt-8">
<script
src="https://giscus.app/client.js"
data-repo="CodingWithCalvin/codingwithcalvin.net"
data-repo-id="R_kgDOIdGZXw"
data-category="Blog Post Comments"
data-category-id="DIC_kwDOIdGZX84C0RR6"
data-mapping="og:title"
data-strict="1"
data-reactions-enabled="0"
data-emit-metadata="0"
data-input-position="top"
data-theme="dark"
data-lang="en"
data-loading="lazy"
crossorigin="anonymous"
async
></script>
</section>
</div>
</article>
</BaseLayout>