diff --git a/docs/utils-reference/getting-started.md b/docs/utils-reference/getting-started.md index f2d35c8..ae2a529 100644 --- a/docs/utils-reference/getting-started.md +++ b/docs/utils-reference/getting-started.md @@ -16,6 +16,10 @@ npm install --save @raycast/utils ## Changelog +### v2.1.0 + +- `getFavicon` will now respect the user's setting for the favicon provider. Note that the `Apple` provider isn't supported since it relies on a native API. + ### v2.0.1 - Fix types for ESM extensions diff --git a/package-lock.json b/package-lock.json index 8a9cdc2..044c836 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@raycast/utils", - "version": "2.0.1", + "version": "2.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@raycast/utils", - "version": "2.0.1", + "version": "2.1.0", "license": "MIT", "dependencies": { "dequal": "^2.0.3" diff --git a/package.json b/package.json index 85f446b..ad4ab56 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@raycast/utils", - "version": "2.0.1", + "version": "2.1.0", "description": "Set of utilities to streamline building Raycast extensions", "author": "Raycast Technologies Ltd.", "homepage": "https://developers.raycast.com/utils-reference", diff --git a/src/icon/favicon.ts b/src/icon/favicon.ts index 7c12123..8e4eb20 100644 --- a/src/icon/favicon.ts +++ b/src/icon/favicon.ts @@ -35,13 +35,53 @@ export function getFavicon( }, ): Image.ImageLike { try { - const urlObj = typeof url === "string" ? new URL(url) : url; - const hostname = urlObj.hostname; - return { - source: `https://www.google.com/s2/favicons?sz=${options?.size ?? 64}&domain=${hostname}`, - fallback: options?.fallback ?? Icon.Link, - mask: options?.mask, + const sanitize = (url: string) => { + if (!url.startsWith("http")) { + return `https://${url}`; + } + return url; }; + + const urlObj = typeof url === "string" ? new URL(sanitize(url)) : url; + const hostname = urlObj.hostname; + + const faviconProvider: "none" | "raycast" | "apple" | "google" | "duckDuckGo" | "duckduckgo" | "legacy" = + (process.env.FAVICON_PROVIDER as any) ?? "raycast"; + + switch (faviconProvider) { + case "none": + return { + source: options?.fallback ?? Icon.Link, + mask: options?.mask, + }; + case "apple": + // we can't support apple favicons as it's a native API + return { + source: options?.fallback ?? Icon.Link, + mask: options?.mask, + }; + case "duckduckgo": + case "duckDuckGo": + return { + source: `https://icons.duckduckgo.com/ip3/${hostname}.ico`, + fallback: options?.fallback ?? Icon.Link, + mask: options?.mask, + }; + case "google": + return { + source: `https://www.google.com/s2/favicons?sz=${options?.size ?? 64}&domain=${hostname}`, + fallback: options?.fallback ?? Icon.Link, + mask: options?.mask, + }; + case "legacy": + case "raycast": + default: + return { + source: `https://api.ray.so/favicon?url=${hostname}&size=${options?.size}`, + fallback: options?.fallback ?? Icon.Link, + mask: options?.mask, + }; + } } catch (e) { console.error(e); return Icon.Link;