--- import type { CollectionEntry } from "astro:content"; import { Image } from "astro:assets"; interface Props { project: CollectionEntry<"projects">; } const { project } = Astro.props; // Function to extract first image from markdown content function extractFirstImage(content: string): string | null { // Match markdown image syntax ![alt](url) const imageMatch = content.match(/!\[.*?\]\((.*?)\)/); if (imageMatch && imageMatch[1]) { // Check if it's a relative path (starts with ./) if (imageMatch[1].startsWith('./')) { return imageMatch[1]; } return imageMatch[1]; } return null; } // Extract the first image from the project content const firstImagePath = extractFirstImage(project.body); // Try to load the image if it exists and is a relative path let coverImage = null; if (firstImagePath && firstImagePath.startsWith('./')) { try { // Dynamically import the image from the project's directory const images = import.meta.glob<{ default: ImageMetadata }>('/src/content/projects/**/*.{png,jpg,jpeg,webp}', { eager: true }); const imagePath = `/src/content/projects/${project.slug}/${firstImagePath.replace('./', '')}`; if (images[imagePath]) { coverImage = images[imagePath].default; } } catch (e) { // Image couldn't be loaded console.error('Failed to load project image:', e); } } ---
{coverImage ? (
{project.data.title}

{project.data.title}

{project.data.description}

) : (

{project.data.title}

{project.data.description}

{project.data.demoURL && ( Demo → )} {project.data.repoURL && ( Source )}
)}
{project.data.date.toLocaleDateString('en-US', { year: 'numeric', month: 'short' })}