import * as React from 'react'
import { Item } from '../../../../types'
import { useTags } from '../../hooks/useTags';
import { useAddFilterTag } from '../../hooks/useFilter';
import { hashTagRegex } from '../../../../Utils/HashTagRegex';
import { fixUrls, mailRegex } from '../../../../Utils/ReplaceURLs';
import Markdown from 'react-markdown'
import rehypeVideo from 'rehype-video';
import { getValue } from '../../../../Utils/GetValue';
export const TextView = ({ item, truncate = false}: { item?: Item, truncate?: boolean }) => {
const tags = useTags();
const addFilterTag = useAddFilterTag();
let text = item?.layer?.itemTextField && item ? getValue(item, item.layer?.itemTextField) : "";
if(item && text && truncate) text = truncateString(text, 100, true);
let replacedText;
item && text ? replacedText = fixUrls(text) : "";
replacedText ? replacedText = replacedText.replace(/(? {
let shortUrl = url;
if (url.match('^https:\/\/')) {
shortUrl = url.split('https://')[1];
}
if (url.match('^http:\/\/')) {
shortUrl = url.split('http://')[1];
}
return `[${shortUrl}](${url})`
}) : "" ;
replacedText ? replacedText = replacedText.replace(mailRegex, (url) => {
return `[${url}](mailto:${url})`
}) : "";
replacedText ? replacedText = replacedText.replace(hashTagRegex, (match) => {
return `[${match}](${match})`
}) : "";
const CustomH1 = ({ children }) => (
{children}
);
const CustomH2 = ({ children }) => (
{children}
);
const CustomH3 = ({ children }) => (
{children}
);
const CustomH4 = ({ children }) => (
{children}
);
const CustomH5 = ({ children }) => (
{children}
);
const CustomH6 = ({ children }) => (
{children}
);
const CustomParagraph = ({ children }) => (
{children}
);
const CustomUnorderdList = ({ children }) => (
);
const CustomOrderdList = ({ children }) => (
{children}
);
const CustomHorizontalRow = ({ children }) => (
{children}
);
const CustomImage = ({ alt, src, title }) => (
);
const CustomExternalLink = ({ href, children }) => (
{children}
);
const CustomHashTagLink = ({ children, tag, item }) => {
return (
{
e.stopPropagation();
addFilterTag(tag!);
// map.fitBounds(items)
// map.closePopup();
}}>{children}
)};
return (
//@ts-ignore
{
// Prüft, ob der Link ein YouTube-Video ist
const isYouTubeVideo = href?.startsWith('https://www.youtube.com/watch?v=');
if (isYouTubeVideo) {
const videoId = href?.split('v=')[1].split('&')[0]; // Extrahiert die Video-ID aus der URL
const youtubeEmbedUrl = `https://www.youtube-nocookie.com/embed/${videoId}`;
return (
);
}
if (href?.startsWith("#")) {
const tag = tags.find(t => t.id.toLowerCase() == decodeURI(href).slice(1).toLowerCase())
return {children};
} else {
return (
{children}
);
}
},
ul: CustomUnorderdList,
ol: CustomOrderdList,
img: CustomImage,
hr: CustomHorizontalRow,
h1: CustomH1,
h2: CustomH2,
h3: CustomH3,
h4: CustomH4,
h5: CustomH5,
h6: CustomH6,
}}>
{replacedText}
)
}
function truncateString( str, n, useWordBoundary ){
if (str.length <= n) { return str; }
const subString = str.slice(0, n-1); // the original check
return (useWordBoundary
? subString.slice(0, subString.lastIndexOf(" "))
: subString) + "…";
};