Repository navigation
Expand file tree
/
Copy pathstructured-data.ts
More file actions
203 lines (187 loc) · 5.03 KB
/
Copy pathstructured-data.ts
File metadata and controls
203 lines (187 loc) · 5.03 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { absoluteUrl, SITE_NAME } from "./site";
/**
* JSON-LD builders, ported from the Next app's lib/structured-data.ts.
*
* The `schema-dts` types are deliberately not carried over — they cost a
* dependency and a great deal of type gymnastics (the Next version needed a
* triple cast to build a @graph) to describe objects that are serialized to
* a string immediately. The shapes below are the ones actually emitted.
*/
interface Node {
"@type": string;
"@id"?: string;
[key: string]: unknown;
}
export function buildGraph(nodes: Node[]) {
return {
"@context": "https://schema.org",
"@graph": nodes,
};
}
export const orgId = (origin: string) => `${origin}/#organization`;
export const websiteId = (origin: string) => `${origin}/#website`;
export function organizationSchema(origin: string, logoUrl?: string): Node {
return {
"@type": "Organization",
"@id": orgId(origin),
name: SITE_NAME,
url: origin,
...(logoUrl ? { logo: { "@type": "ImageObject", url: logoUrl } } : {}),
sameAs: [
"https://www.youtube.com/@CodingCatDev",
"https://twitter.com/CodingCatDev",
"https://github.com/codingcatdev",
],
};
}
export function websiteSchema(origin: string): Node {
return {
"@type": "WebSite",
"@id": websiteId(origin),
name: SITE_NAME,
url: origin,
publisher: { "@id": orgId(origin) },
};
}
export function breadcrumbSchema(
origin: string,
crumbs: Array<{ name: string; path: string }>,
): Node {
return {
"@type": "BreadcrumbList",
itemListElement: crumbs.map((crumb, index) => ({
"@type": "ListItem",
position: index + 1,
name: crumb.name,
item: absoluteUrl(crumb.path, origin),
})),
};
}
interface ArticleInput {
title?: string | null;
excerpt?: string | null;
date?: string | null;
_updatedAt?: string | null;
imageUrl?: string;
authors?: Array<{ title?: string | null; slug?: string | null }> | null;
}
export function articleSchema(
origin: string,
content: ArticleInput,
path: string,
): Node {
const url = absoluteUrl(path, origin);
const authors = (content.authors ?? [])
.filter((a): a is { title: string; slug?: string | null } =>
Boolean(a?.title),
)
.map((a) => ({
"@type": "Person",
name: a.title,
...(a.slug ? { url: absoluteUrl(`/author/${a.slug}`, origin) } : {}),
}));
return {
"@type": "Article",
"@id": `${url}#article`,
headline: content.title ?? undefined,
description: content.excerpt ?? undefined,
...(content.imageUrl ? { image: content.imageUrl } : {}),
...(content.date ? { datePublished: content.date } : {}),
...(content._updatedAt ? { dateModified: content._updatedAt } : {}),
...(authors.length ? { author: authors } : {}),
publisher: { "@id": orgId(origin) },
mainEntityOfPage: { "@type": "WebPage", "@id": url },
url,
};
}
interface PersonInput {
title?: string | null;
excerpt?: string | null;
imageUrl?: string;
socials?: Record<string, unknown> | null;
websites?: unknown[] | null;
}
/** Collect URL-like strings from the loosely-typed socials object and websites array. */
function collectProfileUrls(
socials?: Record<string, unknown> | null,
websites?: unknown[] | null,
): string[] {
const urls: string[] = [];
const pushIfUrl = (value: unknown) => {
if (typeof value === "string" && value.startsWith("http")) {
urls.push(value);
}
};
if (socials) {
for (const value of Object.values(socials)) {
pushIfUrl(value);
}
}
if (Array.isArray(websites)) {
for (const entry of websites) {
if (entry && typeof entry === "object") {
for (const value of Object.values(entry)) {
pushIfUrl(value);
}
}
}
}
return Array.from(new Set(urls));
}
export function personSchema(
origin: string,
content: PersonInput,
path: string,
): Node {
const url = absoluteUrl(path, origin);
const sameAs = collectProfileUrls(content.socials, content.websites);
return {
"@type": "Person",
"@id": `${url}#person`,
name: content.title ?? undefined,
description: content.excerpt ?? undefined,
...(content.imageUrl ? { image: content.imageUrl } : {}),
...(sameAs.length ? { sameAs } : {}),
url,
};
}
interface PodcastEpisodeInput {
title?: string | null;
excerpt?: string | null;
date?: string | null;
imageUrl?: string;
season?: number | null;
episode?: number | null;
audioUrl?: string | null;
}
export function podcastEpisodeSchema(
origin: string,
content: PodcastEpisodeInput,
path: string,
): Node {
const url = absoluteUrl(path, origin);
return {
"@type": "PodcastEpisode",
"@id": `${url}#episode`,
url,
name: content.title ?? undefined,
description: content.excerpt ?? undefined,
...(content.date ? { datePublished: content.date } : {}),
...(content.imageUrl ? { image: content.imageUrl } : {}),
...(content.season ? { seasonNumber: content.season } : {}),
...(content.episode ? { episodeNumber: content.episode } : {}),
...(content.audioUrl
? {
associatedMedia: {
"@type": "AudioObject",
contentUrl: content.audioUrl,
},
}
: {}),
partOfSeries: {
"@type": "PodcastSeries",
name: "CodingCat.dev Podcast",
url: `${origin}/podcasts`,
},
};
}