mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
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[^>]*>([^<]+)<\/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\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
|
|
.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/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 });
|
|
}
|
|
}
|