From 8a5083c8f05c739b4b07b4af7a659ae2314766dd Mon Sep 17 00:00:00 2001 From: qianmao1989 Date: Sat, 16 May 2026 12:42:26 +0800 Subject: [PATCH] feat(zod-transform-socials): add generated label field to social link output Add a `label` field to the transformed social link output with the following precedence: 1. Explicit input `label` from object social input 2. Known platform name when platform !== "custom" 3. Fallback from URL hostname, stripping leading "www." Closes #42 --- zod-transform-socials/index.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/zod-transform-socials/index.ts b/zod-transform-socials/index.ts index 7b9a49f..acb2372 100644 --- a/zod-transform-socials/index.ts +++ b/zod-transform-socials/index.ts @@ -15,6 +15,7 @@ export const SocialsSchema = z.union([ z.string().url(), z.object({ icon: z.string().optional(), + label: z.string().optional(), platform: z.string().optional(), url: z.string().url(), username: z.string().optional(), @@ -28,23 +29,42 @@ export type { DomainShortcuts }; export type CreateSocialsConfig = CreateSocialLinksConfig; +const generateLabel = ( + inputLabel: string | undefined, + platform: string, + url: string, +): string => { + if (inputLabel) return inputLabel; + if (platform !== "custom") return platform; + try { + const hostname = new URL(url).hostname.replace(/^www\./, ""); + return hostname; + } catch { + return url; + } +}; + const buildTransformSocial = (extractor: ReturnType) => (social: SocialsSchema) => { if (typeof social == "string") { - return extractor({ url: social }); + const data = extractor({ url: social }); + return { ...data, label: generateLabel(undefined, data.platform, social) }; } - const { icon, url, platform, username } = social; + const { icon, label, url, platform, username } = social; const data = extractor({ url }); + const resolvedPlatform = platform ?? data.platform; + return { icon: icon ?? (platform === undefined ? data.icon : getSocialIcon(platform as INNER_SOCIAL_TYPES)), + label: generateLabel(label, resolvedPlatform, url), url: url ?? data.url, - platform: platform ?? data.platform, + platform: resolvedPlatform, username: username ?? data.username, }; };