diff --git a/src/Components/Map/Subcomponents/ItemPopupComponents/TextView.tsx b/src/Components/Map/Subcomponents/ItemPopupComponents/TextView.tsx index 3e1504f6..fe4f7aef 100644 --- a/src/Components/Map/Subcomponents/ItemPopupComponents/TextView.tsx +++ b/src/Components/Map/Subcomponents/ItemPopupComponents/TextView.tsx @@ -1,40 +1,65 @@ import * as React from 'react' import { Item } from '../../../../types' import { useTags } from '../../hooks/useTags'; -import { replaceURLs } from '../../../../Utils/ReplaceURLs'; import reactStringReplace from 'react-string-replace'; import { useAddFilterTag, useResetFilterTags } from '../../hooks/useFilter'; import { hashTagRegex } from '../../../../Utils/HashTagRegex'; +import { fixUrls, mailRegex, urlRegex } from '../../../../Utils/ReplaceURLs'; -export const TextView = ({item} : {item?: Item}) => { - const tags = useTags(); +export const TextView = ({ item }: { item?: Item }) => { + const tags = useTags(); - const addFilterTag = useAddFilterTag(); - const resetFilterTags = useResetFilterTags(); + const addFilterTag = useAddFilterTag(); + const resetFilterTags = useResetFilterTags(); - return( - <> - { - reactStringReplace(item?.text, hashTagRegex, (match, i) => ( - <> - { - - tags.filter(t => t.id.toLowerCase() == match.slice(1).toLowerCase()).map(tag => - { - resetFilterTags(); - addFilterTag(tag); - }}>#{tag.id} - - - ) - } - - )) - } - + + let replacedText; + + if (item && item.text) replacedText = fixUrls(item.text); + + + + replacedText = reactStringReplace(replacedText, /(https?:\/\/\S+)/g, (url, i) => { + let shortUrl = url; + if (url.match('^https:\/\/')) { + shortUrl = url.split('https://')[1]; + } + if (url.match('^http:\/\/')) { + shortUrl = url.split('http://')[1]; + } + return ( + {shortUrl} + ) + }); + + replacedText = reactStringReplace(replacedText, mailRegex, (url, i) => { + return ( + {url} + ) + }); + + //ts-ignore + replacedText = reactStringReplace(replacedText, hashTagRegex, (match, i) => { + + const tag = tags.find(t => t.id.toLowerCase() == match.slice(1).toLowerCase()) + return ( + { + resetFilterTags(); + addFilterTag(tag!); + }}>{match} ) - + + + }) + + return ( +

+ {replacedText} +

+ ) + + } diff --git a/src/Utils/ReplaceURLs.ts b/src/Utils/ReplaceURLs.ts index 11949ac0..ecaa70aa 100644 --- a/src/Utils/ReplaceURLs.ts +++ b/src/Utils/ReplaceURLs.ts @@ -1,19 +1,17 @@ -export function replaceURLs(message: string): string { - if (!message) return ""; - - const urlRegex = /(^| )(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,10}(:[0-9]{1,10})?(\/.*)?$/gm; +export const urlRegex = /(^| )(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,10}(:[0-9]{1,10})?(\/.*)?$/gm +export const mailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi; + + +export function fixUrls(message: string): string { + message = message.replace(urlRegex, function (url) { let hyperlink = url.replace(' ', ''); if (!hyperlink.match('^https?:\/\/')) { - hyperlink = 'http://' + hyperlink; - } - return '' + url + '' + hyperlink = 'https://' + hyperlink; + } + return hyperlink }); - const mailRegex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi; - message = message.replace(mailRegex, function (mail) { - return '' + mail + '' - }); return message; } \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index bc27219b..9e20768a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,7 +26,7 @@ export interface LayerProps { } export class Item { - id: string | number; + id: string ; name: string; text: string; position: Geometry; @@ -37,7 +37,7 @@ export class Item { api?: ItemsApi; tags?: Tag[]; [key: string]: any; - constructor(id:string|number,name:string,text:string,position:Geometry, layer?: LayerProps, api?: ItemsApi){ + constructor(id:string,name:string,text:string,position:Geometry, layer?: LayerProps, api?: ItemsApi){ this.id = id; this.name = name; this.text = text;