| 9 | import { requireUserId } from "~/services/session.server"; |
| 10 | |
| 11 | export async function loader({ request }: LoaderFunctionArgs) { |
| 12 | await requireUserId(request); |
| 13 | const url = new URL(request.url); |
| 14 | const codeUrl = url.searchParams.get("url"); |
| 15 | invariant(typeof codeUrl === "string", "codeUrl is required"); |
| 16 | const decodedCodeUrl = decodeURIComponent(codeUrl); |
| 17 | const response = await fetch(decodedCodeUrl); |
| 18 | if (!response.ok) { |
| 19 | throw new Error("Network response was not ok"); |
| 20 | } |
| 21 | |
| 22 | const code = await response.text(); |
| 23 | |
| 24 | const hideCodeRegex = /(\n)?\/\/ hide-code[\s\S]*?\/\/ end-hide-code(\n)*/gm; |
| 25 | const cleanedCode = code?.replace(hideCodeRegex, "\n"); |
| 26 | |
| 27 | return json({ |
| 28 | code: cleanedCode, |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | export function CodeExample({ example }: { example: ApiExample }) { |
| 33 | const customerFetcher = useFetcher<typeof loader>(); |