diff --git a/packages/starlight/schemas/favicon.ts b/packages/starlight/schemas/favicon.ts index 7f599099d2f..e47a878acd1 100644 --- a/packages/starlight/schemas/favicon.ts +++ b/packages/starlight/schemas/favicon.ts @@ -1,4 +1,4 @@ -import { extname } from 'node:path'; +import { extname, } from 'node:path'; import { z } from 'astro/zod'; const faviconTypeMap = { @@ -10,13 +10,17 @@ const faviconTypeMap = { '.svg': 'image/svg+xml', }; +function isFaviconExt(ext: string): ext is keyof typeof faviconTypeMap { + return ext in faviconTypeMap; +} + export const FaviconSchema = () => z .string() .default('/favicon.svg') .transform((favicon, ctx) => { const ext = extname(favicon).toLowerCase(); - + // Return error when favicon isn't the file type accepted if (!isFaviconExt(ext)) { ctx.addIssue({ code: z.ZodIssueCode.custom, @@ -25,7 +29,17 @@ export const FaviconSchema = () => return z.NEVER; } + // check for both http and https + if (/^https?:\/\//.test(favicon)) { + ctx.addIssue({ + // Show error message + code: z.ZodIssueCode.custom, + message: 'Favicons must be a relative URL with a .ico, .gif, .jpg, .png, or .svg file.', + }); + return z.NEVER; + } + // Return the relative path (correctly formatted) return { href: favicon, type: faviconTypeMap[ext], @@ -35,6 +49,3 @@ export const FaviconSchema = () => 'The default favicon for your site which should be a path to an image in the `public/` directory.' ); -function isFaviconExt(ext: string): ext is keyof typeof faviconTypeMap { - return ext in faviconTypeMap; -}