Новые изменения в коде проекта
This commit is contained in:
parent
0777fc201f
commit
aef12e853d
12 changed files with 159 additions and 15 deletions
|
|
@ -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