Skip to content

Commit

Permalink
fix: improve FACEBOOK_REGEX to match older style page URLs (#2650)
Browse files Browse the repository at this point in the history
Older Facebook URLs could be formatted like this:
`https://www.facebook.com/some-profile-1234`

This package currently incorrectly parses the URL as:
`https://www.facebook.com/some`

The REGEX for this package does not match the `-` character. These URLs
have been deprecated, and Facebook will redirect these older style URLs
to their newer style. We should still match these older style URLs as
they are indeed valid, albeit old.

I've added 2 tests to cover these URL patterns, happy to adjust to any
feedback.

Closes #2216
  • Loading branch information
gijoehosaphat authored Aug 30, 2024
1 parent 07ddd79 commit a005e69
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/utils/src/internals/social.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const TWITTER_REGEX_STRING = `(?<!\\w)(?:http(?:s)?:\\/\\/)?(?:www.)?(?:twitter.
const FACEBOOK_RESERVED_PATHS =
'rsrc\\.php|apps|groups|events|l\\.php|friends|images|photo.php|chat|ajax|dyi|common|policies|login|recover|reg|help|security|messages|marketplace|pages|live|bookmarks|games|fundraisers|saved|gaming|salesgroups|jobs|people|ads|ad_campaign|weather|offers|recommendations|crisisresponse|onthisday|developers|settings|connect|business|plugins|intern|sharer';

const FACEBOOK_REGEX_STRING = `(?<!\\w)(?:http(?:s)?:\\/\\/)?(?:www.)?(?:facebook.com|fb.com)\\/(?!(?:${FACEBOOK_RESERVED_PATHS})(?:[\\'\\"\\?\\.\\/]|$))(profile\\.php\\?id\\=[0-9]{3,20}|(?!profile\\.php)[a-z0-9\\.]{5,51})(?![a-z0-9\\.])(?:/)?`;
const FACEBOOK_REGEX_STRING = `(?<!\\w)(?:http(?:s)?:\\/\\/)?(?:www.)?(?:facebook.com|fb.com)\\/(?!(?:${FACEBOOK_RESERVED_PATHS})(?:[\\'\\"\\?\\.\\/]|$))(profile\\.php\\?id\\=[0-9]{3,20}|(?!profile\\.php)[a-z0-9-\\.]{5,51})(?![a-z0-9\\.])(?:/)?`;

const YOUTUBE_REGEX_STRING =
'(?<!\\w)(?:https?:\\/\\/)?(?:youtu\\.be\\/|(?:www\\.|m\\.)?youtube\\.com(?:\\/(?:watch|v|embed|user|c(?:hannel)?)(?:\\.php)?)?(?:\\?[^ ]*v=|\\/))([a-zA-Z0-9\\-_]{2,100})';
Expand Down
2 changes: 2 additions & 0 deletions test/utils/social.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,8 @@ describe('utils.social', () => {

expect(FACEBOOK_REGEX.test('https://www.facebook.com/someusername')).toBe(true);
expect(FACEBOOK_REGEX.test('https://www.facebook.com/someusername/')).toBe(true);
expect(FACEBOOK_REGEX.test('https://www.facebook.com/some-username-1234')).toBe(true);
expect(FACEBOOK_REGEX.test('https://www.facebook.com/some-username-1234/')).toBe(true);
expect(FACEBOOK_REGEX.test('http://www.facebook.com/some.username123')).toBe(true);
expect(FACEBOOK_REGEX.test('www.facebook.com/someusername')).toBe(true);
expect(FACEBOOK_REGEX.test('facebook.com/someusername')).toBe(true);
Expand Down

0 comments on commit a005e69

Please sign in to comment.