Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion __tests__/common/socials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { detectPlatformFromUrl } from '../../src/common/schema/socials';
import {
detectPlatformFromUrl,
socialLinksInputSchema,
MAX_SOCIAL_LINKS,
} from '../../src/common/schema/socials';

describe('detectPlatformFromUrl', () => {
it('should detect twitter.com', () => {
Expand Down Expand Up @@ -90,3 +94,37 @@ describe('detectPlatformFromUrl', () => {
expect(detectPlatformFromUrl('')).toBeNull();
});
});

describe('socialLinksInputSchema', () => {
it('should accept up to MAX_SOCIAL_LINKS links', () => {
const links = Array.from({ length: MAX_SOCIAL_LINKS }, (_, i) => ({
url: `https://github.com/user${i}`,
}));
const result = socialLinksInputSchema.safeParse(links);
expect(result.success).toBe(true);
});

it('should reject more than MAX_SOCIAL_LINKS links', () => {
const links = Array.from({ length: MAX_SOCIAL_LINKS + 1 }, (_, i) => ({
url: `https://github.com/user${i}`,
}));
const result = socialLinksInputSchema.safeParse(links);
expect(result.success).toBe(false);
});

it('should auto-detect platforms for new platforms', () => {
const links = [
{ url: 'https://codeberg.org/testuser' },
{ url: 'https://gitlab.com/testuser' },
{ url: 'https://bitbucket.org/testuser' },
{ url: 'https://kaggle.com/testuser' },
];
const result = socialLinksInputSchema.parse(links);
expect(result).toEqual([
{ platform: 'codeberg', url: 'https://codeberg.org/testuser' },
{ platform: 'gitlab', url: 'https://gitlab.com/testuser' },
{ platform: 'bitbucket', url: 'https://bitbucket.org/testuser' },
{ platform: 'kaggle', url: 'https://kaggle.com/testuser' },
]);
});
});
35 changes: 35 additions & 0 deletions src/common/schema/socials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ export const blueskySchema = socialUrlSchema(
/^(?:(?:https:\/\/)?(?:www\.)?bsky\.app\/profile\/)?(?<value>[\w.-]+)(?:\/.*)?$/,
);

export const codebergSchema = socialUrlSchema(
/^(?:(?:https:\/\/)?(?:www\.)?codeberg\.org\/)?(?<value>[\w-]{2,})\/?$/,
);

export const gitlabSchema = socialUrlSchema(
/^(?:(?:https:\/\/)?(?:www\.)?gitlab\.com\/)?(?<value>[\w-]{2,})\/?$/,
);

export const bitbucketSchema = socialUrlSchema(
/^(?:(?:https:\/\/)?(?:www\.)?bitbucket\.org\/)?(?<value>[\w-]{2,})\/?$/,
);

export const kaggleSchema = socialUrlSchema(
/^(?:(?:https:\/\/)?(?:www\.)?kaggle\.com\/)?(?<value>[\w-]{2,})\/?$/,
);

export const socialFieldsSchema = z.object({
github: githubSchema,
twitter: twitterSchema,
Expand Down Expand Up @@ -101,6 +117,10 @@ const PLATFORM_DOMAINS: Record<string, string> = {
'youtu.be': 'youtube',
'hashnode.com': 'hashnode',
'hashnode.dev': 'hashnode',
'codeberg.org': 'codeberg',
'gitlab.com': 'gitlab',
'bitbucket.org': 'bitbucket',
'kaggle.com': 'kaggle',
};

/**
Expand Down Expand Up @@ -143,11 +163,14 @@ export const socialLinkInputSchema = z.object({
platform: z.string().optional(),
});

export const MAX_SOCIAL_LINKS = 20;

/**
* Schema for socialLinks array input with auto-detection and transformation
*/
export const socialLinksInputSchema = z
.array(socialLinkInputSchema)
.max(MAX_SOCIAL_LINKS)
.transform((links) =>
links.map(({ url, platform }) => ({
platform: platform || detectPlatformFromUrl(url) || 'other',
Expand Down Expand Up @@ -212,6 +235,18 @@ export function extractHandleFromUrl(
case 'portfolio':
// Full URL is stored for portfolio
return url;
case 'codeberg':
// https://codeberg.org/username
return pathname.replace(/^\//, '') || null;
case 'gitlab':
// https://gitlab.com/username
return pathname.replace(/^\//, '') || null;
case 'bitbucket':
// https://bitbucket.org/username
return pathname.replace(/^\//, '') || null;
case 'kaggle':
// https://kaggle.com/username
return pathname.replace(/^\//, '') || null;
default:
return null;
}
Expand Down
Loading