--- import { getCollection } from 'astro:content'; import { siteConfig } from '@/config'; import { generatePageSEO } from '@/utils/seo'; import { shouldShowPost, sortPostsByDate, extractTags } from '@/utils/markdown'; import { optimizePostImagePath } from '@/utils/images'; import BaseLayout from '@/layouts/BaseLayout.astro'; import PostCard from '@/components/PostCard.astro'; import Tags from '@/components/Tags.astro'; import Pagination from '@/components/Pagination.astro'; import Icon from '@/components/Icon.astro'; // Get special content for posts page let postsPageContent; try { postsPageContent = await getCollection('special', ({ slug }) => slug === 'posts'); } catch (error) { // Fallback if special collection doesn't exist or posts.md doesn't exist postsPageContent = []; } const postsPageData = postsPageContent.length > 0 ? postsPageContent[0] : null; // Get query parameters const url = new URL(Astro.request.url); const selectedTag = url.searchParams.get('tag'); // Get all posts const allPosts = await getCollection('posts'); // Filter posts based on environment const isDev = import.meta.env.DEV; const visiblePosts = allPosts.filter(post => shouldShowPost(post, isDev)); // Sort posts by date (newest first) const sortedPosts = sortPostsByDate(visiblePosts); // Filter by tag if specified const filteredPosts = selectedTag ? sortedPosts.filter(post => post.data.tags?.includes(selectedTag)) : sortedPosts; // Pagination const postsPerPage = siteConfig.postOptions.postsPerPage; const totalPosts = filteredPosts.length; const totalPages = Math.ceil(totalPosts / postsPerPage); const currentPage = 1; // This is the first page const startIndex = 0; const endIndex = Math.min(postsPerPage, totalPosts); const paginatedPosts = filteredPosts.slice(startIndex, endIndex); // Extract all unique tags const allTags = extractTags(sortedPosts); // Generate SEO data const pageTitle = selectedTag ? `Posts tagged "${selectedTag}"` : (postsPageData?.data.title || 'All Posts'); const pageDescription = selectedTag ? `All posts tagged with "${selectedTag}"` : (postsPageData?.data.description || `Browse all ${totalPosts} posts on ${siteConfig.title}`); const seoData = { title: `${pageTitle} | ${siteConfig.title}`, description: pageDescription, canonical: Astro.url.href, ogType: 'website' as const }; // Pagination info const baseUrl = selectedTag ? `/posts/tag/${encodeURIComponent(selectedTag)}` : '/posts'; const pagination = { currentPage, totalPages, hasNext: currentPage < totalPages, hasPrev: false, nextUrl: totalPages > 1 ? '/posts/2' : undefined, prevUrl: undefined }; --- {paginatedPosts.length > 0 && (() => { const firstPost = paginatedPosts[0]; if (!firstPost.data.image || typeof firstPost.data.image === 'string' && firstPost.data.image.startsWith('http')) return null; const showCoverImages = siteConfig.postOptions.showPostCardCoverImages; const shouldShowCoverImage = ['all', 'posts', 'featured-and-posts'].includes(showCoverImages); return shouldShowCoverImage ? ( ) : null; })()}

{selectedTag ? ( <> Showing {filteredPosts.length} posts tagged with #{selectedTag} ) : ( <>Explore all {totalPosts} posts )}

{selectedTag && ( )}
{siteConfig.postOptions.tags && allTags.length > 0 && ( )}
{paginatedPosts.length > 0 ? ( <>
{paginatedPosts.map((post, index) => ( ))}
) : selectedTag ? (

No posts found

There are no posts tagged with "{selectedTag}". Try browsing other tags or view all posts.

) : (

No posts yet

This blog is just getting started. Check back soon for new content!

{isDev && (

Dev Mode: Add some posts to the content/posts/ directory to see them here.

)}
)}