---
// src/pages/projects/[slug].astro
import { getCollection } from 'astro:content';
import BaseLayout from '../../layouts/BaseLayout.astro';
// Required getStaticPaths function for dynamic routes
export async function getStaticPaths() {
try {
const projectEntries = await getCollection('projects', ({ data }) => {
// Filter out drafts in production
return import.meta.env.PROD ? !data.draft : true;
});
return projectEntries.map(entry => ({
params: { slug: entry.slug },
props: { entry },
}));
} catch (error) {
console.error('Error fetching projects:', error);
// Return empty array if collection doesn't exist or is empty
return [];
}
}
// Get the project from props
const { entry } = Astro.props;
// Format date helper
const formatDate = (date) => {
if (!date) return '';
const d = new Date(date);
return d.toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
};
// Generate datetime attribute safely
const getISODate = (date) => {
if (!date) return '';
// Handle various date formats
try {
// If already a Date object
if (date instanceof Date) {
return date.toISOString();
}
// If it's a string or number, convert to Date
return new Date(date).toISOString();
} catch (error) {
// Fallback if date is invalid
console.error('Invalid date format:', date);
return '';
}
};
// Get the Content component for rendering markdown
const { Content } = await entry.render();
---