Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#IOCOM-987] Invalidate clickable links in email #297

Merged
merged 9 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 34 additions & 3 deletions EmailNotification/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
contentToHtml,
invalidateClickableLinks,
messageReducedToHtml,
removeLinks,
truncateMarkdown
Expand Down Expand Up @@ -78,18 +79,48 @@ describe("removeLinks", () => {
test("should return the string without the url if a simple url is contained", () => {
const simpleLink = "https://asimplelink.com/";
const baseText = `A simple text`;
expect(removeLinks(`${baseText}${simpleLink}`)).toBe(baseText);
expect(removeLinks(`${baseText} ${simpleLink}`)).toBe(
`${baseText} [link rimosso]`
);
});

test("should return the string without the url if more than one simple url are contained", () => {
const simpleLink = "https://asimplelink.com/";
const baseText = `A simple text`;
expect(
removeLinks(
`${baseText} ${simpleLink} this is another link ${simpleLink}`
)
).toBe(`${baseText} [link rimosso] this is another link [link rimosso]`);
});

test("should return the string without the url if an url with query params is contained", () => {
const simpleLink = "https://asimplelink.com/?qp1=value";
const baseText = `A simple text`;
expect(removeLinks(`${baseText}${simpleLink}`)).toBe(baseText);
expect(removeLinks(`${baseText} ${simpleLink}`)).toBe(
`${baseText} [link rimosso]`
);
});

test("should return the string without the url if an url with query params and # is contained ", () => {
const simpleLink = "https://asimplelink.com/?qp1=value#header";
const baseText = `A simple text`;
expect(removeLinks(`${baseText}${simpleLink}`)).toBe(baseText);
expect(removeLinks(`${baseText} ${simpleLink}`)).toBe(
`${baseText} [link rimosso]`
);
});
});

describe("invalidateClickableLinks", () => {
test("should return the same string if no period are provided", () => {
expect(invalidateClickableLinks("a simple text with no perios")).toBe(
Garma00 marked this conversation as resolved.
Show resolved Hide resolved
"a simple text with no perios"
Garma00 marked this conversation as resolved.
Show resolved Hide resolved
);
});

test("should add a zero-width space before every '.' character", () => {
Garma00 marked this conversation as resolved.
Show resolved Hide resolved
expect(invalidateClickableLinks("a text.with 2 period.")).toBe(
"a text.\u{200B}with 2 period.\u{200B}"
);
});
});
19 changes: 16 additions & 3 deletions EmailNotification/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,24 @@ export const truncateMarkdown = (plainText: string): string =>
: plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL);

export const removeLinks = (text: string): string =>
text.replace(/(?:https?|ftp):\/\/[\n\S]+/g, "");
text.replace(/(?:https?|ftp):\/\/[\n\S]+/g, "[link rimosso]");
Garma00 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add a zero-width space before every '.' character in order to makke all the links not clickable
* */

export const invalidateClickableLinks = (text: string): string =>
text.replace(/\./g, ".\u{200B}");

export const prepareBody = (markdown: string): string =>
// eslint-disable-next-line functional/immutable-data
pipe(markdown.split("---").pop(), removeMd, truncateMarkdown, removeLinks);
pipe(
// eslint-disable-next-line functional/immutable-data
markdown.split("---").pop(),
removeMd,
removeLinks,
invalidateClickableLinks,
truncateMarkdown
);

type MessageReducedToHtmlOutput = ({
content,
Expand Down
Loading