diff --git a/libraries/nestjs-libraries/src/short-linking/short.link.service.ts b/libraries/nestjs-libraries/src/short-linking/short.link.service.ts index 98a09592..3301606c 100644 --- a/libraries/nestjs-libraries/src/short-linking/short.link.service.ts +++ b/libraries/nestjs-libraries/src/short-linking/short.link.service.ts @@ -5,6 +5,7 @@ import { Injectable } from '@nestjs/common'; import { ShortIo } from './providers/short.io'; import { Kutt } from './providers/kutt'; import { LinkDrip } from './providers/linkdrip'; +import { uniq } from 'lodash'; const getProvider = (): ShortLinking => { if (process.env.DUB_TOKEN) { @@ -36,7 +37,7 @@ export class ShortLinkService { } const mergeMessages = messages.join(' '); - const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/g; + const urlRegex = /(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*))/gm; const urls = mergeMessages.match(urlRegex); if (!urls) { // No URLs found, return the original text @@ -53,10 +54,10 @@ export class ShortLinkService { return messages; } - const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/g; + const urlRegex = /(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*))/gm; return Promise.all( messages.map(async (text) => { - const urls = text.match(urlRegex); + const urls = uniq(text.match(urlRegex)); if (!urls) { // No URLs found, return the original text return text;