@@ -13,23 +13,25 @@ export async function action({ request }: ActionFunctionArgs) {
1313 // Add proper headers to handle CORS and content type
1414 const response = await fetch ( url , {
1515 headers : {
16- 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ,
17- 'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' ,
16+ 'User-Agent' :
17+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ,
18+ Accept : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' ,
1819 'Accept-Language' : 'en-US,en;q=0.5' ,
19- }
20+ } ,
2021 } ) ;
2122
2223 if ( ! response . ok ) {
2324 throw new Error ( `Failed to fetch URL: ${ response . status } ${ response . statusText } ` ) ;
2425 }
2526
2627 const contentType = response . headers . get ( 'content-type' ) ;
28+
2729 if ( ! contentType ?. includes ( 'text/html' ) ) {
2830 throw new Error ( 'URL must point to an HTML page' ) ;
2931 }
3032
3133 const html = await response . text ( ) ;
32-
34+
3335 // Extract title
3436 const titleMatch = html . match ( / < t i t l e [ ^ > ] * > ( [ ^ < ] + ) < \/ t i t l e > / i) ;
3537 const title = titleMatch ? titleMatch [ 1 ] . trim ( ) : 'No title found' ;
@@ -48,7 +50,7 @@ export async function action({ request }: ActionFunctionArgs) {
4850
4951 // Extract code blocks
5052 const codeBlocks = html . match ( / < p r e [ ^ > ] * > [ \s \S ] * ?< \/ p r e > | < c o d e [ ^ > ] * > [ \s \S ] * ?< \/ c o d e > / gi) || [ ] ;
51- const formattedCodeBlocks = codeBlocks . map ( block => {
53+ const formattedCodeBlocks = codeBlocks . map ( ( block ) => {
5254 return block
5355 . replace ( / < [ ^ > ] + > / g, '' )
5456 . replace ( / & l t ; / g, '<' )
@@ -59,12 +61,13 @@ export async function action({ request }: ActionFunctionArgs) {
5961
6062 // Extract links
6163 const links = html . match ( / < a [ ^ > ] * h r e f = " ( [ ^ " ] * ) " [ ^ > ] * > ( [ ^ < ] * ) < \/ a > / gi) || [ ] ;
62- const formattedLinks = links . map ( link => {
64+ const formattedLinks = links . map ( ( link ) => {
6365 const hrefMatch = link . match ( / h r e f = " ( [ ^ " ] * ) " / i) ;
6466 const textMatch = link . match ( / > ( [ ^ < ] * ) < / i) ;
67+
6568 return {
6669 url : hrefMatch ? hrefMatch [ 1 ] : '' ,
67- text : textMatch ? textMatch [ 1 ] . trim ( ) : ''
70+ text : textMatch ? textMatch [ 1 ] . trim ( ) : '' ,
6871 } ;
6972 } ) ;
7073
@@ -74,24 +77,18 @@ export async function action({ request }: ActionFunctionArgs) {
7477 description,
7578 mainContent : mainContent . slice ( 0 , 1000 ) + '...' ,
7679 codeBlocks : formattedCodeBlocks ,
77- relevantLinks : formattedLinks . filter ( link =>
78- link . url &&
79- ! link . url . startsWith ( '#' ) &&
80- ! link . url . startsWith ( 'javascript:' ) &&
81- link . text . trim ( )
80+ relevantLinks : formattedLinks . filter (
81+ ( link ) => link . url && ! link . url . startsWith ( '#' ) && ! link . url . startsWith ( 'javascript:' ) && link . text . trim ( ) ,
8282 ) ,
83- sourceUrl : url
83+ sourceUrl : url ,
8484 } ;
8585
8686 return json ( {
8787 success : true ,
88- data : structuredContent
88+ data : structuredContent ,
8989 } ) ;
9090 } catch ( error ) {
9191 console . error ( 'Web search error:' , error ) ;
92- return json (
93- { error : error instanceof Error ? error . message : 'Unknown error occurred' } ,
94- { status : 500 }
95- ) ;
92+ return json ( { error : error instanceof Error ? error . message : 'Unknown error occurred' } , { status : 500 } ) ;
9693 }
97- }
94+ }
0 commit comments