Skip to content

Commit ef17bbe

Browse files
authored
fix: isASocialNetworkUrl() regex for summarize-cool-pages (#44)
1 parent 1c7fc96 commit ef17bbe

File tree

5 files changed

+58
-12
lines changed

5 files changed

+58
-12
lines changed

src/__tests__/regex.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { isASocialNetworkUrl } from '../helpers/regex.helper';
4+
5+
describe('Helpers: Regex', () => {
6+
describe('Rule: isASocialNetworkUrl should regex correctly an url', () => {
7+
it('isASocialNetworkUrl() should return true when the url is a social network url', () => {
8+
const url = 'www.facebook.com/username';
9+
const result = isASocialNetworkUrl(url);
10+
expect(result).toBe(true);
11+
});
12+
it('isASocialNetworkUrl() should return false when the url is not a social network url', () => {
13+
const url = 'https://blog.richardekwonye.com/bezier-curves';
14+
const result = isASocialNetworkUrl(url);
15+
expect(result).toBe(false);
16+
});
17+
});
18+
});

src/__tests__/summarize-cool-pages.spec.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { afterEach } from 'node:test';
2-
3-
import { beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
42

53
import {
64
getPageSummaryDiscordView,
5+
isPageSummarizeSuccessData,
76
NoContentFoundSummaryError,
87
parseHtmlSummarized,
98
} from '../summarize-cool-pages';
10-
119
const createSummarizeCoolPagesFixture = () => {
1210
return {
1311
// from https://react.dev/learn/you-might-not-need-an-effect
@@ -71,4 +69,30 @@ describe('Feature: summarize cool pages', () => {
7169
it('getPageSummaryDiscordView() should return a string with the page summary', () => {
7270
expect(getPageSummaryDiscordView(fixture.pageSummary)).toEqual(fixture.pageSummaryDiscordView);
7371
});
72+
73+
describe('Rule: isPageSummarizeSuccessData() should check if the data is a PageSummarizerDataSuccess', () => {
74+
it('isPageSummarizeSuccessData() should return true when the data is a PageSummarizerDataSuccess', () => {
75+
// I'm using as because eslint can't autofix and sort the import itself
76+
const data = {
77+
success: true,
78+
html: fixture.htmlWithSummaryContent,
79+
} as {
80+
success: true;
81+
html: string;
82+
};
83+
const result = isPageSummarizeSuccessData(data);
84+
expect(result).toBe(true);
85+
});
86+
it('isPageSummarizeSuccessData() should return false when the data is not a PageSummarizerDataSuccess', () => {
87+
const data = {
88+
success: false,
89+
html: undefined,
90+
} as {
91+
success: false;
92+
html: undefined;
93+
};
94+
const result = isPageSummarizeSuccessData(data);
95+
expect(result).toBe(false);
96+
});
97+
});
7498
});

src/cool-links-management.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ThreadAutoArchiveDuration, type Message } from 'discord.js';
22
import ogs from 'open-graph-scraper';
33

4+
import { isASocialNetworkUrl } from './helpers/regex.helper';
45
import { getPageSummary } from './summarize-cool-pages';
56
import { getVideoSummary } from './summarize-cool-videos';
67

@@ -28,9 +29,6 @@ const getThreadNameFromOpenGraph = async (url: string): Promise<string | null> =
2829
};
2930

3031
const youtubeUrlRegex = new RegExp('^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))');
31-
const socialNetworksUrlRegex = new RegExp(
32-
'^(?!.*(fb.me|facebook.com|t.co|instagr.am|instagram.com|lnkd.in|youtu.be|tiktok.com)).*$',
33-
);
3432

3533
export const coolLinksManagement = async (message: Message) => {
3634
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
@@ -58,7 +56,7 @@ export const coolLinksManagement = async (message: Message) => {
5856

5957
await thread.send(summary);
6058
}
61-
if (!youtubeUrlRegex.test(url) && !socialNetworksUrlRegex.test(url)) {
59+
if (!youtubeUrlRegex.test(url) && !isASocialNetworkUrl(url)) {
6260
try {
6361
const pageSummaryDiscordView = await getPageSummary(url);
6462
await thread.send(pageSummaryDiscordView);

src/helpers/regex.helper.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const socialNetworksUrlRegex = new RegExp(
2+
'^(https?://)?(www.)?(facebook.com|fb.me|twitter.com|vxtwitter.com|instagram.com|linkedin.com|youtube.com|youtu.be|pinterest.com|snapchat.com|tiktok.com)/[a-zA-Z0-9.-/?=&#_]+$',
3+
);
4+
export const isASocialNetworkUrl = (url: string): boolean => {
5+
return socialNetworksUrlRegex.test(url);
6+
};

src/summarize-cool-pages.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ type PageSummary = {
1111
readingTime: string;
1212
};
1313

14-
type PageSummarizerDataSuccess = {
14+
export type PageSummarizerDataSuccess = {
1515
success: true;
1616
html: string;
1717
};
1818

19-
type PageSummarizerDataFailure = {
19+
export type PageSummarizerDataFailure = {
2020
success: false;
2121
html?: undefined;
2222
};
2323

24-
type PageSummarizerData = PageSummarizerDataSuccess | PageSummarizerDataFailure;
24+
export type PageSummarizerData = PageSummarizerDataSuccess | PageSummarizerDataFailure;
2525

26-
const isPageSummarizeSuccessData = (
26+
export const isPageSummarizeSuccessData = (
2727
data: PageSummarizerData,
2828
): data is PageSummarizerDataSuccess => {
2929
return data?.success && typeof data?.html === 'string';

0 commit comments

Comments
 (0)