From 3725943ecf5c48856c2976aedfa41ef6f48f4ef7 Mon Sep 17 00:00:00 2001 From: Nikola Lukovic Date: Thu, 19 Oct 2023 18:07:51 +0200 Subject: [PATCH] Potential fix for urls in content surrounded with gunk --- src/components/ParsedNote/ParsedNote.tsx | 13 +++++++++---- src/constants.ts | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/components/ParsedNote/ParsedNote.tsx b/src/components/ParsedNote/ParsedNote.tsx index 61088d4..6a5d0cd 100644 --- a/src/components/ParsedNote/ParsedNote.tsx +++ b/src/components/ParsedNote/ParsedNote.tsx @@ -39,7 +39,7 @@ import { hookForDev } from '../../lib/devTools'; import { getMediaUrl as getMediaUrlDefault } from "../../lib/media"; import NoteImage from '../NoteImage/NoteImage'; import { createStore } from 'solid-js/store'; -import { linebreakRegex } from '../../constants'; +import { linebreakRegex, urlExtractRegex } from '../../constants'; const specialChars = [",", "?", ";", "!", "'", ".", "-"]; @@ -81,8 +81,14 @@ const ParsedNote: Component<{ if (index > 0) { const prefix = token.slice(0, index); - const url = token.slice(index); - return <>{parseToken(prefix)} {parseToken(url)}; + + const matched = token.match(urlExtractRegex)[0]; + if (matched) { + const suffix = token.substring(matched.length + index, token.length); + return <>{parseToken(prefix)}{parseToken(matched)}{parseToken(suffix)}; + } else { + return <>{parseToken(prefix)}{parseToken(token.slice(index))}; + } } if (!props.ignoreMedia) { @@ -421,7 +427,6 @@ const ParsedNote: Component<{ preview.url && ((!!preview.description && preview.description.length > 0) || !preview.images?.some(x => x === '') || - !preview.favicons?.some(x => x === '') || !!preview.title ); diff --git a/src/constants.ts b/src/constants.ts index b675db7..9cd178b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -245,6 +245,7 @@ export const nostrNestsRegex = /nostrnests\.com\/[a-zA-Z0-9]+/i; export const wavlakeRegex = /https?:\/\/(?:player\.|www\.)?wavlake\.com\/(?!top|new|artists|account|activity|login|preferences|feed|profile)(?:(?:track|album)\/[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}|[a-z-]+)/i; export const youtubeRegex = /(?:https?:\/\/)?(?:www|m\.)?(?:youtu\.be\/|youtube\.com\/(?:live\/|shorts\/|embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})/; export const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9\u00F0-\u02AF@:%._\+~#=]{1,256}\.[a-zA-Z0-9\u00F0-\u02AF()]{1,8}\b([-a-zA-Z0-9\u00F0-\u02AF()@:%_\+.~#?&//=]*)/; +export const urlExtractRegex = /https?:\/\/\S+\.[^()]+(?:\([^)]*\))*/; export const interpunctionRegex = /^(\.|,|;|\?|\!)$/; export const hashtagRegex = /(?:\s|^)#[^\s!@#$%^&*(),.?":{}|<>]+/i;