|
| 1 | +open WebAPI |
| 2 | + |
| 3 | +%%raw("import React from 'react'") |
| 4 | + |
| 5 | +let loadGoogleFont = async (family: string) => { |
| 6 | + let url = `https://fonts.googleapis.com/css2?family=${family}` |
| 7 | + let css = await (await fetch(url))->Response.text |
| 8 | + |
| 9 | + // this function should fail if we can't load the font |
| 10 | + let resource = |
| 11 | + css->String.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/)->Option.getOrThrow |
| 12 | + let response = await fetch(resource[1]->Option.getOrThrow->Option.getOrThrow) |
| 13 | + await response->Response.arrayBuffer |
| 14 | +} |
| 15 | + |
| 16 | +type context = {request: FetchAPI.request} |
| 17 | + |
| 18 | +let onRequest = async ({request}: context) => { |
| 19 | + let url = WebAPI.URL.make(~url=request.url) |
| 20 | + let title = url.searchParams->URLSearchParams.get("title") |
| 21 | + let descripton = url.searchParams->URLSearchParams.get("description") |
| 22 | + |
| 23 | + // we want to split the title if it contains a | |
| 24 | + let (title, subTitle) = { |
| 25 | + let segments = title->String.split("|") |
| 26 | + (segments[0]->Option.getOr(""), segments[1]) |
| 27 | + } |
| 28 | + |
| 29 | + Cloudflare.imageResponse( |
| 30 | + <div |
| 31 | + style={{ |
| 32 | + width: "1200px", |
| 33 | + height: "630px", |
| 34 | + background: "#0B0D22", |
| 35 | + backgroundImage: "linear-gradient(45deg, #0B0D22 70%, #14162c)", |
| 36 | + color: "#efefef", |
| 37 | + display: "flex", |
| 38 | + flexDirection: "column", |
| 39 | + justifyContent: "center", |
| 40 | + alignItems: "flex-start", |
| 41 | + textAlign: "left", |
| 42 | + position: "relative", |
| 43 | + padding: "60px", |
| 44 | + boxSizing: "border-box", |
| 45 | + }} |
| 46 | + > |
| 47 | + <img |
| 48 | + src="https://rescript-lang.org/brand/rescript-logo.svg" |
| 49 | + style={{ |
| 50 | + maxWidth: "300px", |
| 51 | + objectFit: "contain", |
| 52 | + marginBottom: "10px", |
| 53 | + }} |
| 54 | + /> |
| 55 | + <h1 |
| 56 | + style={{ |
| 57 | + fontSize: "64px", |
| 58 | + fontWeight: "600", |
| 59 | + marginBottom: "20px", |
| 60 | + maxWidth: "996px", |
| 61 | + fontFamily: "heading", |
| 62 | + textWrap: "balance", |
| 63 | + }} |
| 64 | + > |
| 65 | + {React.string(title)} |
| 66 | + </h1> |
| 67 | + {switch subTitle { |
| 68 | + | Some(subTitle) => |
| 69 | + <h2 |
| 70 | + style={{ |
| 71 | + fontSize: "40px", |
| 72 | + fontWeight: "600", |
| 73 | + marginBottom: "20px", |
| 74 | + maxWidth: "996px", |
| 75 | + fontFamily: "heading", |
| 76 | + textWrap: "balance", |
| 77 | + }} |
| 78 | + > |
| 79 | + {React.string(subTitle)} |
| 80 | + </h2> |
| 81 | + | None => React.null |
| 82 | + }} |
| 83 | + <hr |
| 84 | + style={{ |
| 85 | + borderTop: "2px solid #efefef", |
| 86 | + width: "100%", |
| 87 | + }} |
| 88 | + /> |
| 89 | + <p |
| 90 | + style={{ |
| 91 | + fontFamily: "text", |
| 92 | + fontSize: "32px", |
| 93 | + opacity: "0.9", |
| 94 | + maxWidth: "900px", |
| 95 | + textWrap: "balance", |
| 96 | + }} |
| 97 | + > |
| 98 | + {React.string(descripton)} |
| 99 | + </p> |
| 100 | + </div>, |
| 101 | + { |
| 102 | + height: 630, |
| 103 | + width: 1200, |
| 104 | + fonts: [ |
| 105 | + { |
| 106 | + data: await loadGoogleFont("Inter:opsz,wght@14..32,600&display=swap"), |
| 107 | + name: "heading", |
| 108 | + style: #normal, |
| 109 | + weight: 600, |
| 110 | + }, |
| 111 | + { |
| 112 | + data: await loadGoogleFont("Inter:opsz,wght@14..32,400&display=swap"), |
| 113 | + name: "text", |
| 114 | + style: #normal, |
| 115 | + weight: 400, |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + ) |
| 120 | +} |
0 commit comments