1
1
import 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' ;
4
3
5
4
interface ExternLinkProps {
6
5
href : string ;
@@ -14,7 +13,6 @@ interface LinkMetaData {
14
13
icon : string ;
15
14
}
16
15
17
-
18
16
const fetchMetaData = async ( url : string ) : Promise < LinkMetaData > => {
19
17
try {
20
18
const response = await fetch ( url ) ;
@@ -26,14 +24,14 @@ const fetchMetaData = async (url: string): Promise<LinkMetaData> => {
26
24
const doc = parser . parseFromString ( text , 'text/html' ) ;
27
25
28
26
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' ;
31
29
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' ;
34
32
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' ;
37
35
38
36
return { title, description, icon : new URL ( icon , url ) . href } ;
39
37
} catch ( error ) {
@@ -45,7 +43,7 @@ const fetchMetaData = async (url: string): Promise<LinkMetaData> => {
45
43
} ;
46
44
}
47
45
} ;
48
-
46
+
49
47
const ExternLink : React . FC < ExternLinkProps > = ( { href, icon, manualTitle } ) => {
50
48
const [ metaData , setMetaData ] = useState < LinkMetaData | null > ( null ) ;
51
49
const [ loading , setLoading ] = useState ( true ) ;
@@ -70,10 +68,18 @@ const ExternLink: React.FC<ExternLinkProps> = ({ href, icon, manualTitle }) => {
70
68
return < div > Loading...</ div > ; // Display a loading state while fetching metadata
71
69
}
72
70
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
+
73
79
return (
74
80
< a
75
81
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"
77
83
target = "_blank"
78
84
rel = "noopener noreferrer"
79
85
>
@@ -98,7 +104,7 @@ const ExternLink: React.FC<ExternLinkProps> = ({ href, icon, manualTitle }) => {
98
104
{ metaData ?. title }
99
105
</ span >
100
106
< 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 */ }
102
108
</ span >
103
109
</ div >
104
110
{ /* Arrow Icon */ }
0 commit comments