2025-03-03 21:16:16 +08:00
|
|
|
|
import type { APIRoute } from 'astro';
|
|
|
|
|
import { getCollection } from 'astro:content';
|
2025-03-09 01:11:43 +08:00
|
|
|
|
import { getSpecialPath } from '../../content.config';
|
|
|
|
|
|
|
|
|
|
// 处理特殊ID的函数
|
|
|
|
|
function getArticleUrl(articleId: string) {
|
|
|
|
|
return `/articles/${getSpecialPath(articleId)}`;
|
|
|
|
|
}
|
2025-03-03 21:16:16 +08:00
|
|
|
|
|
|
|
|
|
export const GET: APIRoute = async ({ request }) => {
|
|
|
|
|
// 获取查询参数
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
const page = parseInt(url.searchParams.get('page') || '1');
|
|
|
|
|
const limit = parseInt(url.searchParams.get('limit') || '10');
|
|
|
|
|
const tag = url.searchParams.get('tag') || '';
|
|
|
|
|
const path = url.searchParams.get('path') || '';
|
|
|
|
|
|
|
|
|
|
// 获取所有文章
|
|
|
|
|
const articles = await getCollection('articles');
|
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
// 打印所有文章的ID,用于调试
|
|
|
|
|
console.log('所有文章的ID:');
|
|
|
|
|
articles.forEach(article => {
|
|
|
|
|
console.log(`- ${article.id}`);
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-03 21:16:16 +08:00
|
|
|
|
// 根据条件过滤文章
|
|
|
|
|
let filteredArticles = articles;
|
|
|
|
|
|
|
|
|
|
// 如果有标签过滤
|
|
|
|
|
if (tag) {
|
|
|
|
|
filteredArticles = filteredArticles.filter(article =>
|
|
|
|
|
article.data.tags && article.data.tags.includes(tag)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
// 如果有路径过滤,直接使用文章ID来判断
|
2025-03-03 21:16:16 +08:00
|
|
|
|
if (path) {
|
2025-03-09 01:11:43 +08:00
|
|
|
|
const normalizedPath = path.toLowerCase();
|
|
|
|
|
console.log('当前过滤路径:', normalizedPath);
|
2025-03-03 21:16:16 +08:00
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
filteredArticles = filteredArticles.filter(article => {
|
|
|
|
|
const articlePath = article.id.split('/');
|
|
|
|
|
console.log('处理文章:', article.id, '分割后:', articlePath);
|
2025-03-03 21:16:16 +08:00
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
// 检查文章路径的每一部分
|
|
|
|
|
return article.id.toLowerCase().includes(normalizedPath);
|
|
|
|
|
});
|
2025-03-03 21:16:16 +08:00
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
console.log('过滤后的文章数量:', filteredArticles.length);
|
2025-03-03 21:16:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按日期排序(最新的在前面)
|
|
|
|
|
const sortedArticles = filteredArticles.sort(
|
|
|
|
|
(a, b) => b.data.date.getTime() - a.data.date.getTime()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 计算分页
|
|
|
|
|
const startIndex = (page - 1) * limit;
|
|
|
|
|
const endIndex = startIndex + limit;
|
|
|
|
|
const paginatedArticles = sortedArticles.slice(startIndex, endIndex);
|
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
// 格式化文章数据
|
|
|
|
|
const formattedArticles = paginatedArticles.map(article => ({
|
|
|
|
|
id: article.id,
|
|
|
|
|
title: article.data.title,
|
|
|
|
|
date: article.data.date,
|
|
|
|
|
tags: article.data.tags || [],
|
|
|
|
|
summary: article.data.summary || '',
|
|
|
|
|
url: getArticleUrl(article.id) // 使用特殊ID处理函数
|
|
|
|
|
}));
|
2025-03-03 21:16:16 +08:00
|
|
|
|
|
2025-03-09 01:11:43 +08:00
|
|
|
|
return new Response(JSON.stringify({
|
|
|
|
|
articles: formattedArticles,
|
2025-03-03 21:16:16 +08:00
|
|
|
|
total: sortedArticles.length,
|
2025-03-09 01:11:43 +08:00
|
|
|
|
page,
|
|
|
|
|
limit,
|
2025-03-03 21:16:16 +08:00
|
|
|
|
totalPages: Math.ceil(sortedArticles.length / limit)
|
2025-03-09 01:11:43 +08:00
|
|
|
|
}), {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json'
|
2025-03-03 21:16:16 +08:00
|
|
|
|
}
|
2025-03-09 01:11:43 +08:00
|
|
|
|
});
|
2025-03-03 21:16:16 +08:00
|
|
|
|
};
|