Новые изменения в коде проекта
This commit is contained in:
parent
0777fc201f
commit
aef12e853d
12 changed files with 159 additions and 15 deletions
|
|
@ -19,6 +19,7 @@
|
|||
"astro": "^6.0.8",
|
||||
"astro-icon": "^1.1.5",
|
||||
"marked": "^18.0.0",
|
||||
"pocketbase": "^0.21.0",
|
||||
"tailwindcss": "^4.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ export interface Props {
|
|||
categoryColor?: string;
|
||||
date: string;
|
||||
readTime: string;
|
||||
imageUrl?: string;
|
||||
image?: string;
|
||||
slug?: string;
|
||||
}
|
||||
|
||||
|
|
@ -17,9 +17,11 @@ const {
|
|||
categoryColor = 'bg-gold',
|
||||
date,
|
||||
readTime,
|
||||
imageUrl = '/images/blog/default.avif',
|
||||
image,
|
||||
slug = '#'
|
||||
} = Astro.props;
|
||||
|
||||
const imageUrl = image || '/images/blog/default.avif';
|
||||
---
|
||||
|
||||
<article class="blog-card" data-animation="fade-up">
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
---
|
||||
import BlogCard from '@components/blog/BlogCard.astro';
|
||||
import { getPostImageUrl } from '@lib/pb';
|
||||
|
||||
interface Post {
|
||||
id: string;
|
||||
|
|
@ -10,7 +11,7 @@ interface Post {
|
|||
categoryColor: string;
|
||||
date: string;
|
||||
readTime: string;
|
||||
imageUrl: string;
|
||||
image: string;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
|
|
@ -47,7 +48,7 @@ const filteredPosts = currentSlug
|
|||
categoryColor={post.categoryColor}
|
||||
date={formatDate(post.date)}
|
||||
readTime={post.readTime}
|
||||
imageUrl={post.imageUrl}
|
||||
image={getPostImageUrl(post)}
|
||||
slug={`/blog/${post.slug}`}
|
||||
/>
|
||||
))}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ const PB_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
|
|||
|
||||
export const pb = new PocketBase(PB_URL);
|
||||
|
||||
pb.collection('_superusers').authRefresh().catch(() => {});
|
||||
|
||||
export interface Post {
|
||||
id: string;
|
||||
slug: string;
|
||||
|
|
@ -14,7 +16,7 @@ export interface Post {
|
|||
categoryColor: string;
|
||||
date: string;
|
||||
readTime: string;
|
||||
imageUrl: string;
|
||||
image: string;
|
||||
content?: string;
|
||||
draft: boolean;
|
||||
}
|
||||
|
|
@ -71,4 +73,15 @@ export async function getAllCategories(): Promise<string[]> {
|
|||
|
||||
const categories = (result || []).map((post: any) => post.category).filter(Boolean);
|
||||
return ['Все', ...new Set(categories)];
|
||||
}
|
||||
|
||||
export function getPostImageUrl(post: any): string {
|
||||
if (post.image) {
|
||||
const fileUrl = pb.files.getUrl(post, post.image);
|
||||
if (fileUrl.startsWith('/')) {
|
||||
return `${PB_URL}${fileUrl}`;
|
||||
}
|
||||
return fileUrl;
|
||||
}
|
||||
return '/images/blog/default.avif';
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
import type { APIRoute } from 'astro';
|
||||
import PocketBase from 'pocketbase';
|
||||
|
||||
const PB_URL = import.meta.env.POCKETBASE_URL || 'http://127.0.0.1:8090';
|
||||
|
||||
export const GET: APIRoute = async ({ url }) => {
|
||||
try {
|
||||
const pb = new PocketBase(import.meta.env.POCKETBASE_URL);
|
||||
const pb = new PocketBase(PB_URL);
|
||||
|
||||
const page = parseInt(url.searchParams.get('page') || '1');
|
||||
const perPage = parseInt(url.searchParams.get('per_page') || '10');
|
||||
|
|
@ -25,6 +27,15 @@ export const GET: APIRoute = async ({ url }) => {
|
|||
sort: '-date',
|
||||
});
|
||||
|
||||
const getImageUrl = (post: any) => {
|
||||
if (!post.image) return null;
|
||||
const fileUrl = pb.files.getUrl(post, post.image);
|
||||
if (fileUrl.startsWith('/')) {
|
||||
return `${PB_URL}${fileUrl}`;
|
||||
}
|
||||
return fileUrl;
|
||||
};
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
posts: result.items.map(post => ({
|
||||
id: post.id,
|
||||
|
|
@ -36,7 +47,7 @@ export const GET: APIRoute = async ({ url }) => {
|
|||
categoryColor: post.categoryColor,
|
||||
date: post.date,
|
||||
readTime: post.readTime,
|
||||
imageUrl: post.imageUrl,
|
||||
image: getImageUrl(post),
|
||||
})),
|
||||
total: result.totalItems,
|
||||
page: result.pageInfo.page,
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { SITE_URL } from '@constants';
|
|||
import PostCommentForm from '@components/blog/PostCommentForm.astro';
|
||||
import RelatedPosts from '@components/blog/RelatedPosts.astro';
|
||||
import ArticleTableOfContents from '@components/blog/ArticleTableOfContents.astro';
|
||||
import { getPostBySlug, getPosts } from '@lib/pb';
|
||||
import { getPostBySlug, getPosts, getPostImageUrl } from '@lib/pb';
|
||||
import { marked } from 'marked';
|
||||
|
||||
export const prerender = false;
|
||||
|
|
@ -50,6 +50,7 @@ const { posts: relatedPosts } = await getPosts({ perPage: 4, category: post.cate
|
|||
const filteredRelated = relatedPosts.filter(p => p.slug !== slug).slice(0, 3);
|
||||
|
||||
const currentUrl = `${SITE_URL}/blog/${slug}`;
|
||||
const heroImage = getPostImageUrl(post);
|
||||
---
|
||||
|
||||
<ArticleLayout
|
||||
|
|
@ -61,7 +62,7 @@ const currentUrl = `${SITE_URL}/blog/${slug}`;
|
|||
{ label: 'Блог', href: '/blog' },
|
||||
{ label: post.title }
|
||||
]}
|
||||
heroImage={post.imageUrl}
|
||||
heroImage={heroImage}
|
||||
heroAlt={post.title}
|
||||
category={post.category}
|
||||
postTitle={post.title}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ 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 { getPosts, getAllCategories } from '@lib/pb';
|
||||
import { getPosts, getAllCategories, getPostImageUrl } from '@lib/pb';
|
||||
|
||||
const POSTS_PER_PAGE = 6;
|
||||
const currentPage = 1;
|
||||
|
|
@ -64,7 +64,7 @@ const formatDate = (date: string) => {
|
|||
categoryColor={post.categoryColor}
|
||||
date={formatDate(post.date)}
|
||||
readTime={post.readTime}
|
||||
imageUrl={post.imageUrl}
|
||||
image={getPostImageUrl(post)}
|
||||
slug={`/blog/${post.slug}`}
|
||||
/>
|
||||
</article>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ 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 { getPosts, getAllCategories } from '@lib/pb';
|
||||
import { getPosts, getAllCategories, getPostImageUrl } from '@lib/pb';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
|
|
@ -66,7 +66,7 @@ const formatDate = (date: string) => {
|
|||
categoryColor={post.categoryColor}
|
||||
date={formatDate(post.date)}
|
||||
readTime={post.readTime}
|
||||
imageUrl={post.imageUrl}
|
||||
image={getPostImageUrl(post)}
|
||||
slug={`/blog/${post.slug}`}
|
||||
/>
|
||||
</article>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import Layout from '@layouts/Layout.astro';
|
|||
import { SITE_URL } from '@constants';
|
||||
import BlogCard from '@components/blog/BlogCard.astro';
|
||||
import SearchModal from '@components/base/SearchModal.astro';
|
||||
import { getPosts } from '@lib/pb';
|
||||
import { getPosts, getPostImageUrl } from '@lib/pb';
|
||||
|
||||
const url = new URL(Astro.request.url);
|
||||
const searchQuery = url.searchParams.get('q') || '';
|
||||
|
|
@ -78,7 +78,7 @@ const formatDate = (date: string) => {
|
|||
categoryColor={post.categoryColor}
|
||||
date={formatDate(post.date)}
|
||||
readTime={post.readTime}
|
||||
imageUrl={post.imageUrl}
|
||||
image={getPostImageUrl(post)}
|
||||
slug={`/blog/${post.slug}`}
|
||||
/>
|
||||
))}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue