diff --git a/app/api/news/route.ts b/app/api/news/route.ts index 18df599..51f6812 100644 --- a/app/api/news/route.ts +++ b/app/api/news/route.ts @@ -3,6 +3,16 @@ import { NextResponse } from 'next/server'; // FastAPI server configuration const API_URL = 'http://172.16.0.104:8000'; +// Helper function to clean HTML content +function cleanHtmlContent(text: string): string { + if (!text) return ''; + return text + .replace(/<[^>]*>/g, '') // Remove HTML tags + .replace(/ /g, ' ') // Replace   with space + .replace(/\s+/g, ' ') // Replace multiple spaces with single space + .trim(); +} + // Helper function to format time function formatDateTime(dateStr: string): { displayDate: string, timestamp: string } { try { @@ -31,15 +41,18 @@ function formatDateTime(dateStr: string): { displayDate: string, timestamp: stri // Helper function to truncate text function truncateText(text: string, maxLength: number): string { - if (!text || text.length <= maxLength) return text; - const lastSpace = text.lastIndexOf(' ', maxLength); - const truncated = text.substring(0, lastSpace > 0 ? lastSpace : maxLength).trim(); + if (!text) return ''; + const cleaned = cleanHtmlContent(text); + if (cleaned.length <= maxLength) return cleaned; + + const lastSpace = cleaned.lastIndexOf(' ', maxLength); + const truncated = cleaned.substring(0, lastSpace > 0 ? lastSpace : maxLength).trim(); return truncated.replace(/[.,!?]$/, '') + '...'; } // Helper function to format category -function formatCategory(category: string): string { - if (!category) return null; // Return null to match the interface +function formatCategory(category: string): string | null { + if (!category) return null; const categoryMap: { [key: string]: string } = { 'GLOBAL ISSUES - WORLD AFFAIRS': 'WORLD', 'UN NEWS - GLOBAL NEWS': 'UN NEWS', @@ -94,8 +107,8 @@ export async function GET() { const { displayDate, timestamp } = formatDateTime(article.date); return { id: article.id, - title: truncateText(article.title, 70), - description: article.description ? truncateText(article.description, 100) : null, + title: truncateText(article.title, 100), // Increased length for better titles + description: article.description ? truncateText(article.description, 150) : null, // Increased length for better descriptions displayDate, timestamp, source: formatSource(article.source), diff --git a/components/news.tsx b/components/news.tsx index cbb7d8e..39b9ad4 100644 --- a/components/news.tsx +++ b/components/news.tsx @@ -99,29 +99,29 @@ export function News() { {error ? (

{error}

) : ( -
+
{news.length === 0 ? (

No news available

) : ( news.map((item) => (
window.open(item.url, '_blank')} >
- {formatDate(item.date)} + {formatDate(item.date)} {item.category && ( {item.category} )}
-

+

{item.title}

{item.description && ( -

+

{item.description}

)}