Создан компонент поста

This commit is contained in:
Web-serfer 2026-04-09 22:22:55 +05:00
parent 674ef7fe04
commit d0f41672d1
32 changed files with 2082 additions and 289 deletions

View file

@ -3,7 +3,9 @@ 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 { blogPosts } from '@data/blogData';
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
// Получаем параметр поиска из URL
const url = new URL(Astro.request.url);
@ -16,21 +18,30 @@ const breadcrumbsItems = [
];
// Функция поиска по статьям
function searchArticles(query: string) {
function searchArticles(query: string, allPosts: typeof posts) {
if (!query.trim()) return [];
const lowerQuery = query.toLowerCase();
return blogPosts.filter(post => {
const titleMatch = post.title.toLowerCase().includes(lowerQuery);
const descriptionMatch = post.description.toLowerCase().includes(lowerQuery);
const categoryMatch = post.category.toLowerCase().includes(lowerQuery);
return allPosts.filter(post => {
const titleMatch = post.data.title.toLowerCase().includes(lowerQuery);
const descriptionMatch = post.data.description.toLowerCase().includes(lowerQuery);
const categoryMatch = post.data.category.toLowerCase().includes(lowerQuery);
return titleMatch || descriptionMatch || categoryMatch;
});
}
const searchResults = searchArticles(searchQuery);
const searchResults = searchArticles(searchQuery, posts);
// Форматируем дату
const formatDate = (date: Date) => {
return date.toLocaleDateString('ru-RU', {
day: 'numeric',
month: 'long',
year: 'numeric'
});
};
---
<Layout
@ -46,20 +57,20 @@ const searchResults = searchArticles(searchQuery);
<h1 class="search-title">
{searchQuery ? `Результаты поиска` : 'Поиск статей'}
</h1>
{searchQuery && (
<div class="search-info">
<p class="search-query-text">
По запросу: <span class="query-highlight">"{searchQuery}"</span>
</p>
<p class="search-count">
{searchResults.length === 0
? 'Ничего не найдено'
{searchResults.length === 0
? 'Ничего не найдено'
: `Найдено статей: ${searchResults.length}`}
</p>
</div>
)}
<button class="open-search-btn" id="open-search-btn" data-modal-target="search-modal">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"></circle>
@ -76,16 +87,16 @@ const searchResults = searchArticles(searchQuery);
<section class="results-section">
<div class="site-container">
<div class="results-grid">
{searchResults.map((post) => (
{searchResults.map((post: any) => (
<BlogCard
title={post.title}
description={post.description}
category={post.category}
categoryColor={post.categoryColor}
date={post.date}
readTime={post.readTime}
imageUrl={post.imageUrl}
slug={post.slug}
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}`}
/>
))}
</div>
@ -131,10 +142,10 @@ const searchResults = searchArticles(searchQuery);
<script>
(function() {
const btn = document.getElementById('open-search-btn');
btn?.addEventListener('click', () => {
window.dispatchEvent(new CustomEvent('open-modal', {
detail: 'search-modal'
window.dispatchEvent(new CustomEvent('open-modal', {
detail: 'search-modal'
}));
});
@ -142,11 +153,11 @@ const searchResults = searchArticles(searchQuery);
document.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const query = urlParams.get('q');
if (!query && btn) {
setTimeout(() => {
window.dispatchEvent(new CustomEvent('open-modal', {
detail: 'search-modal'
window.dispatchEvent(new CustomEvent('open-modal', {
detail: 'search-modal'
}));
}, 300);
}