import { json } from '@remix-run/node'; import type { ActionFunctionArgs } from '@remix-run/node'; export async function action({ request }: ActionFunctionArgs) { try { const formData = await request.formData(); const url = formData.get('url') as string; if (!url) { return json({ error: 'URL is required' }, { status: 400 }); } const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch URL: ${response.status} ${response.statusText}`); } const html = await response.text(); // Basic HTML parsing to extract title and content const titleMatch = html.match(/]*>([^<]+)<\/title>/i); const title = titleMatch ? titleMatch[1].trim() : 'No title found'; // Extract content by removing script and style tags, then getting text content const content = html .replace(/)<[^<]*)*<\/script>/gi, '') .replace(/)<[^<]*)*<\/style>/gi, '') .replace(/<[^>]+>/g, ' ') .replace(/\s+/g, ' ') .trim() .slice(0, 1000) + '...'; // Limit content length return json({ success: true, data: { title, content, url, }, }); } catch (error) { console.error('Web search error:', error); return json({ error: error instanceof Error ? error.message : 'Unknown error occurred' }, { status: 500 }); } }