11import React , { useEffect , useState } from 'react' ;
2- import { IconChevronRight , Icon as TablerIcon } from '@tabler/icons-react'
3- import { channel } from 'diagnostics_channel' ;
2+ import { IconChevronRight , Icon as TablerIcon } from '@tabler/icons-react' ;
43
54interface ExternLinkProps {
65 href : string ;
@@ -14,7 +13,6 @@ interface LinkMetaData {
1413 icon : string ;
1514}
1615
17-
1816const fetchMetaData = async ( url : string ) : Promise < LinkMetaData > => {
1917 try {
2018 const response = await fetch ( url ) ;
@@ -26,14 +24,14 @@ const fetchMetaData = async (url: string): Promise<LinkMetaData> => {
2624 const doc = parser . parseFromString ( text , 'text/html' ) ;
2725
2826 const title = doc . querySelector ( 'meta[property="og:title"]' ) ?. getAttribute ( 'content' ) ||
29- doc . querySelector ( 'title' ) ?. textContent ||
30- 'No title' ;
27+ doc . querySelector ( 'title' ) ?. textContent ||
28+ 'No title' ;
3129 const description = doc . querySelector ( 'meta[property="og:description"]' ) ?. getAttribute ( 'content' ) ||
32- doc . querySelector ( 'meta[name="description"]' ) ?. getAttribute ( 'content' ) ||
33- 'No description' ;
30+ doc . querySelector ( 'meta[name="description"]' ) ?. getAttribute ( 'content' ) ||
31+ 'No description' ;
3432 const icon = doc . querySelector ( 'link[rel="icon"]' ) ?. getAttribute ( 'href' ) ||
35- doc . querySelector ( 'link[rel="shortcut icon"]' ) ?. getAttribute ( 'href' ) ||
36- 'https://example.com/default-icon.png' ;
33+ doc . querySelector ( 'link[rel="shortcut icon"]' ) ?. getAttribute ( 'href' ) ||
34+ 'https://example.com/default-icon.png' ;
3735
3836 return { title, description, icon : new URL ( icon , url ) . href } ;
3937 } catch ( error ) {
@@ -45,7 +43,7 @@ const fetchMetaData = async (url: string): Promise<LinkMetaData> => {
4543 } ;
4644 }
4745} ;
48-
46+
4947const ExternLink : React . FC < ExternLinkProps > = ( { href, icon, manualTitle } ) => {
5048 const [ metaData , setMetaData ] = useState < LinkMetaData | null > ( null ) ;
5149 const [ loading , setLoading ] = useState ( true ) ;
@@ -70,10 +68,18 @@ const ExternLink: React.FC<ExternLinkProps> = ({ href, icon, manualTitle }) => {
7068 return < div > Loading...</ div > ; // Display a loading state while fetching metadata
7169 }
7270
71+ // Funktion zur Kürzung der Beschreibung
72+ const truncateDescription = ( description : string , maxLength : number ) => {
73+ if ( description . length <= maxLength ) {
74+ return description ;
75+ }
76+ return description . substring ( 0 , maxLength ) + '...' ; // Kürzen und '...' hinzufügen
77+ } ;
78+
7379 return (
7480 < a
7581 href = { href }
76- className = "w-full border border-gray-200 shadow-sm hover:shadow-md dark:border-neutral-700 dark:hover:border-neutral-600 transition-all duration-200 dark:bg-neutral-900 bg-white rounded-lg overflow-hidden flex flex-col justify-start relative"
82+ className = "w-full my-5 border border-gray-200 shadow-sm hover:shadow-md dark:border-neutral-700 dark:hover:border-neutral-600 transition-all duration-200 dark:bg-neutral-900 bg-white rounded-lg overflow-hidden flex flex-col justify-start relative"
7783 target = "_blank"
7884 rel = "noopener noreferrer"
7985 >
@@ -98,7 +104,7 @@ const ExternLink: React.FC<ExternLinkProps> = ({ href, icon, manualTitle }) => {
98104 { metaData ?. title }
99105 </ span >
100106 < span className = "text-sm text-gray-500 dark:text-gray-300" >
101- { metaData ?. description }
107+ { metaData ?. description && truncateDescription ( metaData . description , 100 ) } { /* Hier wird die Beschreibung gekürzt */ }
102108 </ span >
103109 </ div >
104110 { /* Arrow Icon */ }
0 commit comments