Skip to content

Commit de09ce2

Browse files
Added Link and image embeds
1 parent b5ba774 commit de09ce2

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"dependencies": {
6060
"@notionhq/client": "^2.2.4",
6161
"@unpic/svelte": "^0.0.17",
62-
"neverthrow": "^6.0.0"
62+
"neverthrow": "^6.0.0",
63+
"sveltekit-embed": "^0.0.12"
6364
},
6465
"homepage": "https://iridescent-gingersnap-952901.netlify.app/",
6566
"description": "A plug and play library for setting up blogs in subdirectory or main directory in Sveltekit projects using Notion as CMS.",

src/lib/components/Block.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import BulletList from "./BulletList.svelte";
99
import Embed from "./Embed.svelte";
1010
export let block: BlockObjectResponse;
11-
$: console.log(block.type, block);
11+
//$: console.log(block.type, block);
1212
1313
</script>
1414

src/lib/components/Embed.svelte

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
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
}
@@ -23,16 +46,6 @@
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

Comments
 (0)