({ url, env }: { url: string; env: Env })
| 715 | } |
| 716 | |
| 717 | export async function fetchUrlContent({ url, env }: { url: string; env: Env }) { |
| 718 | try { |
| 719 | // Use the robotsTxt checking function to respect robots.txt rules |
| 720 | const result = await fetchFileWithRobotsTxtCheck(url, env); |
| 721 | |
| 722 | if (result.blockedByRobots) { |
| 723 | return { |
| 724 | url, |
| 725 | status: "blocked", |
| 726 | content: [ |
| 727 | { |
| 728 | type: "text" as const, |
| 729 | text: `Access to ${url} is disallowed by robots.txt. GitMCP respects robots.txt directives.`, |
| 730 | }, |
| 731 | ], |
| 732 | }; |
| 733 | } |
| 734 | |
| 735 | if (!result.content) { |
| 736 | return { |
| 737 | url, |
| 738 | status: "not_found", |
| 739 | content: [ |
| 740 | { |
| 741 | type: "text" as const, |
| 742 | text: `Content at ${url} could not be retrieved. The resource may not exist or may require authentication.`, |
| 743 | }, |
| 744 | ], |
| 745 | }; |
| 746 | } |
| 747 | |
| 748 | let finalContent = result.content; |
| 749 | |
| 750 | // Convert HTML to markdown if content appears to be HTML |
| 751 | if ( |
| 752 | finalContent.trim().startsWith("<!DOCTYPE") || |
| 753 | finalContent.trim().startsWith("<html") || |
| 754 | finalContent.includes("<body") |
| 755 | ) { |
| 756 | try { |
| 757 | finalContent = htmlToMd(finalContent); |
| 758 | } catch (error) { |
| 759 | console.warn(`Error converting HTML to Markdown for ${url}: ${error}`); |
| 760 | // Continue with the original content if conversion fails |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | return { |
| 765 | url, |
| 766 | status: "success", |
| 767 | content: [ |
| 768 | { |
| 769 | type: "text" as const, |
| 770 | text: finalContent, |
| 771 | }, |
| 772 | ], |
| 773 | }; |
| 774 | } catch (error) { |
no test coverage detected