File size: 1,774 Bytes
fee9c1e
 
 
 
 
 
 
e329457
fee9c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6281fd
fee9c1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
interface Props {
  title: string;
  description?: string;
  authors?: string[];
  published?: string;
  tags?: string[];
  image?: string; // Preferred absolute URL
}

const { title, description = '', authors = [], published, tags = [], image } = Astro.props as Props;

const url = Astro.url?.toString?.() ?? '';
const site = (Astro.site ? String(Astro.site) : '') as string;

const ogImage = image && image.length > 0
  ? (image.startsWith('http') ? image : (site ? new URL(image, site).toString() : image))
  : undefined;

const jsonLd = {
  '@context': 'https://schema.org',
  '@type': 'Article',
  headline: title,
  description: description || undefined,
  datePublished: published || undefined,
  author: authors.map((name) => ({ '@type': 'Person', name })),
  keywords: tags.length ? tags.join(', ') : undefined,
  mainEntityOfPage: url || undefined,
  image: ogImage ? [ogImage] : undefined,
};
---
<title>{title}</title>
<meta name="description" content={description} />
<link rel="canonical" href={url} />

<meta property="og:type" content="article" />
<meta property="og:title" content={title} />
{description && <meta property="og:description" content={description} />}
<meta property="og:url" content={url} />
{ogImage && <meta property="og:image" content={ogImage} />}
{published && <meta property="article:published_time" content={published} />}
{authors.map(a => <meta property="article:author" content={a} />)}

<meta name="twitter:card" content={ogImage ? 'summary_large_image' : 'summary'} />
<meta name="twitter:title" content={title} />
{description && <meta name="twitter:description" content={description} />}
{ogImage && <meta name="twitter:image" content={ogImage} />}

<script type="application/ld+json" set:html={JSON.stringify(jsonLd)} />