diff --git a/EmailNotification/__tests__/utils.test.ts b/EmailNotification/__tests__/utils.test.ts index 3136b10c..0ba0e8cb 100644 --- a/EmailNotification/__tests__/utils.test.ts +++ b/EmailNotification/__tests__/utils.test.ts @@ -1,6 +1,7 @@ import { contentToHtml, messageReducedToHtml, + removeLinks, truncateMarkdown } from "../utils"; import * as E from "fp-ts/Either"; @@ -67,3 +68,28 @@ describe("truncateMarkdown", () => { ); }); }); + +describe("removeLinks", () => { + test("should return the same string if no url is contained", () => { + const text = "A simple text without any url"; + expect(removeLinks(text)).toBe(text); + }); + + 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); + }); + + 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); + }); + + 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); + }); +}); diff --git a/EmailNotification/utils.ts b/EmailNotification/utils.ts index 7aead9b9..047e5413 100644 --- a/EmailNotification/utils.ts +++ b/EmailNotification/utils.ts @@ -113,9 +113,12 @@ export const truncateMarkdown = (plainText: string): string => ? plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL) + "..." : plainText.substring(0, MAX_CHARACTER_FOR_BODY_MAIL); +export const removeLinks = (text: string): string => + text.replace(/(?:https?|ftp):\/\/[\n\S]+/g, ""); + export const prepareBody = (markdown: string): string => // eslint-disable-next-line functional/immutable-data - pipe(markdown.split("---").pop(), removeMd, truncateMarkdown); + pipe(markdown.split("---").pop(), removeMd, truncateMarkdown, removeLinks); type MessageReducedToHtmlOutput = ({ content,