From 3d975ff6c3c13dc2ee092a7003dd23a7cd5b8208 Mon Sep 17 00:00:00 2001 From: Nevo David Date: Fri, 8 Aug 2025 02:16:27 +0700 Subject: [PATCH] feat: fix shorlinking --- .../src/short-linking/short.link.service.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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;