|
1 | 1 | <script lang="ts"> |
2 | | - import type { OpenGraph } from './types'; |
| 2 | + import type { OpenGraph as OpenGraphType } from './types'; |
| 3 | + import transformKey from "../utils/transform-key"; |
3 | 4 | |
4 | 5 | interface Props { |
5 | | - openGraph: OpenGraph; |
| 6 | + openGraph: OpenGraphType; |
6 | 7 | } |
7 | 8 | |
8 | 9 | const { openGraph }: Props = $props(); |
9 | 10 | </script> |
10 | 11 |
|
11 | 12 | {#if openGraph} |
12 | 13 | {#each Object.entries(openGraph) as [key, value] (key)} |
13 | | - <!-- For openGraph, some of the keys have values as objects so we need to check |
14 | | - before. |
15 | | - --> |
16 | | - {@const _type = typeof value} |
17 | | - {#if _type !== "object"} |
18 | | - {@const transform = key.replace(/([a-z])([A-Z])/g, "$1:$2").toLowerCase()} |
19 | | - <meta property="og:{transform}" content={value} /> |
20 | | - {/if} |
21 | | - {#if _type === "object"} |
22 | | - {#if key === "images"} |
23 | | - {#each openGraph.images ?? [] as image (image)} |
24 | | - {#each Object.entries(image) as [key, value] (key)} |
25 | | - <meta property="og:image:{key}" content={value.toString()} /> |
26 | | - {/each} |
27 | | - {/each} |
28 | | - {:else if key === "videos"} |
29 | | - {#each openGraph.videos ?? [] as video (video.url)} |
30 | | - {#each Object.entries(video) as [key, value] (key)} |
31 | | - {#if key === "url"} |
32 | | - <meta property="og:video" content={value.toString()} /> |
33 | | - {:else} |
34 | | - <meta property="og:video:{key}" content={value.toString()} /> |
35 | | - {/if} |
36 | | - {/each} |
37 | | - {/each} |
38 | | - {:else if key === "localeAlternate"} |
39 | | - {#each openGraph.localeAlternate ?? [] as alternate (alternate)} |
40 | | - <meta property="og:locale:alternate" content={alternate} /> |
41 | | - {/each} |
42 | | - {:else if key === "music"} |
43 | | - {#each Object.entries(openGraph.music ?? {}) as [key, value] (key)} |
44 | | - {@const transform = key |
45 | | - .replace(/([a-z])([A-Z])/g, "$1:$2") |
46 | | - .toLowerCase()} |
47 | | - <meta property="music:{transform}" content={value.toString()} /> |
48 | | - {/each} |
49 | | - {:else if key === "movie"} |
50 | | - {#each Object.entries(openGraph.movie ?? {}) as [key, value] (key)} |
51 | | - {#if typeof value === "object"} |
52 | | - {#each value as propValue (propValue)} |
53 | | - <meta property="video:{key}" content={propValue} /> |
54 | | - {/each} |
55 | | - {:else} |
56 | | - {@const transform = key |
57 | | - .replace(/([a-z])([A-Z])/g, "$1:$2") |
58 | | - .toLowerCase()} |
59 | | - <meta property="video:{transform}" content={value.toString()} /> |
60 | | - {/if} |
61 | | - {/each} |
62 | | - {:else if key === "article"} |
63 | | - {#each Object.entries(openGraph.article ?? {}) as [key, value] (key)} |
64 | | - {#if typeof value === "object"} |
65 | | - {#each value as propValue (propValue)} |
66 | | - <meta property="article:{key}" content={propValue} /> |
67 | | - {/each} |
68 | | - {:else} |
69 | | - {@const transform = key |
70 | | - .replace(/([a-z])([A-Z])/g, "$1:$2") |
71 | | - .toLowerCase()} |
72 | | - <meta property="article:{transform}" content={value.toString()} /> |
| 14 | + {#if value === null || value === undefined} |
| 15 | + <!-- Skip null/undefined values --> |
| 16 | + {:else if typeof value === "string" || typeof value === "number"} |
| 17 | + <!-- Simple string/number values --> |
| 18 | + {@const transformed = transformKey(key)} |
| 19 | + <meta property="og:{transformed}" content={value.toString()} /> |
| 20 | + {:else if key === "images" && Array.isArray(value)} |
| 21 | + <!-- Images array --> |
| 22 | + {#each value as image (image)} |
| 23 | + {#each Object.entries(image) as [imgKey, imgValue] (imgKey)} |
| 24 | + {#if imgValue !== null && imgValue !== undefined} |
| 25 | + <meta property="og:image:{imgKey}" content={imgValue.toString()} /> |
73 | 26 | {/if} |
74 | 27 | {/each} |
75 | | - {:else if key === "book"} |
76 | | - {#each Object.entries(openGraph.book ?? {}) as [key, value] (key)} |
77 | | - {#if typeof value === "object"} |
78 | | - {#each value as propValue (propValue)} |
79 | | - <meta property="book:{key}" content={propValue} /> |
80 | | - {/each} |
81 | | - {:else} |
82 | | - {@const transform = key |
83 | | - .replace(/([a-z])([A-Z])/g, "$1:$2") |
84 | | - .toLowerCase()} |
85 | | - <meta property="book:{transform}" content={value.toString()} /> |
| 28 | + {/each} |
| 29 | + {:else if key === "videos" && Array.isArray(value)} |
| 30 | + <!-- Videos array --> |
| 31 | + {#each value as video (video.url)} |
| 32 | + {#each Object.entries(video) as [vidKey, vidValue] (vidKey)} |
| 33 | + {#if vidValue !== null && vidValue !== undefined} |
| 34 | + {@const prop = vidKey === "url" ? "og:video" : `og:video:${vidKey}`} |
| 35 | + <meta property={prop} content={vidValue.toString()} /> |
86 | 36 | {/if} |
87 | 37 | {/each} |
88 | | - {:else if key === "profile"} |
89 | | - {#each Object.entries(openGraph.profile ?? {}) as [key, value] (key)} |
90 | | - {@const transform = key |
91 | | - .replace(/([a-z])([A-Z])/g, "$1:$2") |
92 | | - .toLowerCase()} |
93 | | - <meta property="profile:{transform}" content={value} /> |
94 | | - {/each} |
95 | | - {/if} |
| 38 | + {/each} |
| 39 | + {:else if key === "localeAlternate" && Array.isArray(value)} |
| 40 | + <!-- Locale alternates --> |
| 41 | + {#each value as alternate (alternate)} |
| 42 | + <meta property="og:locale:alternate" content={alternate} /> |
| 43 | + {/each} |
| 44 | + {:else if typeof value === "object" && value !== null} |
| 45 | + <!-- Complex objects (music, movie, article, book, profile) --> |
| 46 | + {@const prefix = key === "movie" ? "video" : key} |
| 47 | + {#each Object.entries(value) as [objKey, objValue] (objKey)} |
| 48 | + {#if Array.isArray(objValue)} |
| 49 | + <!-- Handle arrays (actors, authors, tags, etc.) --> |
| 50 | + {#each objValue as item (item)} |
| 51 | + <meta property="{prefix}:{objKey}" content={item} /> |
| 52 | + {/each} |
| 53 | + {:else if objValue !== null && objValue !== undefined} |
| 54 | + <!-- Handle simple values --> |
| 55 | + {@const transformed = transformKey(objKey)} |
| 56 | + <meta property="{prefix}:{transformed}" content={objValue.toString()} /> |
| 57 | + {/if} |
| 58 | + {/each} |
96 | 59 | {/if} |
97 | 60 | {/each} |
98 | 61 | {/if} |
0 commit comments