| 153 | // Rewrites the URL to convert an ipfs: url to use https://ipfs.io/ipfs/ |
| 154 | // Rewrites the URL to convert an git: url to use https:// |
| 155 | function rewriteUrl(url: string): URL { |
| 156 | const unmodifiedUrl = new URL(url); |
| 157 | |
| 158 | // Rewrite the URL if it is a relative URL |
| 159 | if (unmodifiedUrl.protocol === "ipfs:") { |
| 160 | if (unmodifiedUrl.hostname === "") { |
| 161 | return new URL( |
| 162 | `https://ipfs.io/ipfs/${unmodifiedUrl.pathname.substring(2)}` |
| 163 | ); |
| 164 | } else { |
| 165 | // Parse out the "hostname" from the raw url because hostnames are case-insensitive and automatically lowercased |
| 166 | const urlMatches = url.match(/^ipfs:\/\/([A-Za-z0-9]+)(\/.*)?/i); |
| 167 | const hostname = urlMatches?.[1]; |
| 168 | |
| 169 | return new URL( |
| 170 | `https://ipfs.io/ipfs/${hostname ?? unmodifiedUrl.hostname}${ |
| 171 | unmodifiedUrl.pathname.length > 0 ? `/${unmodifiedUrl.pathname}` : "" |
| 172 | }${unmodifiedUrl.search}` |
| 173 | ); |
| 174 | } |
| 175 | } |
| 176 | if (unmodifiedUrl.protocol === "git:") { |
| 177 | return new URL( |
| 178 | `https://${unmodifiedUrl.hostname}${unmodifiedUrl.pathname.replace( |
| 179 | ".git", |
| 180 | "" |
| 181 | )}` |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | return unmodifiedUrl; |
| 186 | } |