--- import type { PaginationInfo } from '@/types'; import Icon from './Icon.astro'; export interface Props { pagination: PaginationInfo; baseUrl?: string; } const { pagination, baseUrl = '/posts' } = Astro.props; const { currentPage, totalPages, hasNext, hasPrev } = pagination; // Generate page numbers to show function generatePageNumbers(current: number, total: number) { const pages: (number | string)[] = []; const delta = 2; // Number of pages to show on each side of current page // Always show first page pages.push(1); // Add ellipsis if needed if (current - delta > 2) { pages.push('...'); } // Add pages around current page for (let i = Math.max(2, current - delta); i <= Math.min(total - 1, current + delta); i++) { pages.push(i); } // Add ellipsis if needed if (current + delta < total - 1) { pages.push('...'); } // Always show last page if there's more than one page if (total > 1) { pages.push(total); } return pages; } const pageNumbers = generatePageNumbers(currentPage, totalPages); const prevUrl = hasPrev ? (currentPage === 2 ? baseUrl : `${baseUrl}/${currentPage - 1}`) : undefined; const nextUrl = hasNext ? `${baseUrl}/${currentPage + 1}` : undefined; --- {totalPages > 1 && ( {hasPrev ? ( Previous ) : ( Previous )} {pageNumbers.map(page => ( typeof page === 'number' ? ( {page} ) : ( {page} ) ))} Page {currentPage} of {totalPages} {hasNext ? ( Next ) : ( Next )} )}