11<script lang =" ts" >
22 import type { EmbedBlockObjectResponse } from " @notionhq/client/build/src/api-endpoints" ;
33 export let block: EmbedBlockObjectResponse ;
4+ import { GenericEmbed } from ' sveltekit-embed'
45
5- // $: url = block?.embed?.url;
6+ // $: if(block?.embed?. url) {fetchEmbedMetas( block?.embed?.url);}
67
78 const isImage = (url : string ) => {
89 return url .indexOf (" .jpg" ) >= 0 || url .indexOf (" .png" ) >= 0 || url .indexOf (" .jpeg" ) >= 0 || url .indexOf (" .webp" ) >= 0 || url .indexOf (" .avif" ) >= 0
910 }
1011
12+ let title: string , description: string , image: string , isEmbedImage: boolean = false ;
13+
1114 const fetchEmbedMetas = async (url : string ) => {
12- const response = await fetch (` https://api.dub.sh/metatags?url=${url } ` )
15+ isEmbedImage = isImage (url );
16+ console .log (" url" , url , " isEmbedImage" ,isEmbedImage );
17+
18+ if (isEmbedImage ){
19+ return url ;
20+ }
21+
22+ const response = await fetch (` https://api.dub.sh/metatags?url=${url } ` , {
23+ mode: " no-cors" ,
24+ headers: {
25+ " content-type" : " application/json"
26+ }
27+ })
28+
29+ console .log (" response" , response );
30+
1331
1432 if (response .ok ){
15- return response .json ();
33+ const json = await response .json ();
34+ console .log (json );
35+
36+ title = json ?.title ;
37+ description = json ?.description ,
38+ image = json ?.image ;
1639 }else {
1740 return url ;
1841 }
2346 {#if isImage (block .embed .url )}
2447 <img src ={block .embed .url } alt ={block .embed .caption ?.join (" " )} />
2548 {:else }
26- {#await fetchEmbedMetas (block .embed .url )}
27- Generating Embed...
28- {:then {title , description , image }}
29- <div class =" flex flex-col overflow-hidden rounded-md border border-gray-300 bg-gray-50" >
30- <img src ={image } alt ={description } class =" h-[250px] w-full border-b border-gray-300 object-cover" />
31- <div class =" flex flex-col bg-slate-50 gap-1" >
32- <b class ="text-sm text-[#536471]" >{title }</b >
33- <p class ="line-clamp-2 text-sm text-[#536471]" >{description }</p >
34- </div >
35- </div >
36- {/await }
49+ <GenericEmbed src ={block .embed .url } />
3750 {/if }
3851{/if }
0 commit comments