Насторил работу блога через Backend
This commit is contained in:
parent
014439d565
commit
edd730b438
33 changed files with 1019 additions and 200 deletions
|
|
@ -7,49 +7,28 @@ import BlogCard from '@components/blog/BlogCard.astro';
|
|||
import Pagination from '@components/base/Pagination.astro';
|
||||
import CTA from '@components/base/CTA.astro';
|
||||
import SearchModal from '@components/base/SearchModal.astro';
|
||||
import { getCollection } from 'astro:content';
|
||||
import { getPosts, getAllCategories } from '@lib/pb';
|
||||
|
||||
export const prerender = true;
|
||||
|
||||
export async function getStaticPaths() {
|
||||
const posts = await getCollection('blog');
|
||||
const POSTS_PER_PAGE = 6;
|
||||
const totalPages = Math.ceil(posts.length / POSTS_PER_PAGE);
|
||||
|
||||
return Array.from({ length: totalPages }, (_, i) => ({
|
||||
params: { page: String(i + 2) }, // Начинаем со 2-й страницы (1-я это /blog/)
|
||||
}));
|
||||
}
|
||||
|
||||
const posts = await getCollection('blog');
|
||||
|
||||
// Сортируем посты по дате (новые сверху)
|
||||
const sortedPosts = posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
|
||||
export const prerender = false;
|
||||
|
||||
const POSTS_PER_PAGE = 6;
|
||||
const currentPage = Number(Astro.params.page) || 1;
|
||||
const totalPages = Math.ceil(sortedPosts.length / POSTS_PER_PAGE);
|
||||
|
||||
const startIndex = (currentPage - 1) * POSTS_PER_PAGE;
|
||||
const endIndex = startIndex + POSTS_PER_PAGE;
|
||||
const paginatedPosts = sortedPosts.slice(startIndex, endIndex);
|
||||
const { posts, total, totalPages } = await getPosts({ page: currentPage, perPage: POSTS_PER_PAGE });
|
||||
const categories = await getAllCategories();
|
||||
|
||||
// Форматируем дату
|
||||
const formatDate = (date: Date) => {
|
||||
return date.toLocaleDateString('ru-RU', {
|
||||
const formatDate = (date: string) => {
|
||||
return new Date(date).toLocaleDateString('ru-RU', {
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
year: 'numeric'
|
||||
});
|
||||
};
|
||||
|
||||
// Категории
|
||||
const categories = ['Все', ...new Set(posts.map(post => post.data.category))];
|
||||
---
|
||||
|
||||
<Layout
|
||||
title="Блог — страница ${currentPage} — автоюрист в Сургуте"
|
||||
description="Полезные статьи и советы по автоспорам, ДТП, ОСАГО, лишении прав и защите прав водителей. Страница ${currentPage}."
|
||||
title={`Блог — страница ${currentPage} — автоюрист в Сургуте`}
|
||||
description={`Полезные статьи и советы по автоспорам, ДТП, ОСАГО, лишении прав и защите прав водителей. Страница ${currentPage}.`}
|
||||
canonicalLink={`${SITE_URL}/blog/page/${currentPage}`}
|
||||
breadcrumbs={[
|
||||
{ label: 'Главная', href: '/' },
|
||||
|
|
@ -78,17 +57,17 @@ const categories = ['Все', ...new Set(posts.map(post => post.data.category))]
|
|||
<section class="blog-grid-section">
|
||||
<div class="site-container">
|
||||
<div class="blog-grid" id="blog-grid">
|
||||
{paginatedPosts.map((post: any) => (
|
||||
<article class="blog-card-wrapper" data-category={post.data.category}>
|
||||
{posts.map((post: any) => (
|
||||
<article class="blog-card-wrapper" data-category={post.category}>
|
||||
<BlogCard
|
||||
title={post.data.title}
|
||||
description={post.data.description}
|
||||
category={post.data.category}
|
||||
categoryColor={post.data.categoryColor}
|
||||
date={formatDate(post.data.date)}
|
||||
readTime={post.data.readTime}
|
||||
imageUrl={post.data.imageUrl}
|
||||
slug={`/blog/${post.id}`}
|
||||
title={post.title}
|
||||
description={post.description}
|
||||
category={post.category}
|
||||
categoryColor={post.categoryColor}
|
||||
date={formatDate(post.date)}
|
||||
readTime={post.readTime}
|
||||
imageUrl={post.imageUrl}
|
||||
slug={`/blog/${post.slug}`}
|
||||
/>
|
||||
</article>
|
||||
))}
|
||||
|
|
@ -179,4 +158,4 @@ const categories = ['Все', ...new Set(posts.map(post => post.data.category))]
|
|||
|
||||
setupAnimations();
|
||||
document.addEventListener('astro:after-swap', setupAnimations);
|
||||
</script>
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue