aboutsummaryrefslogtreecommitdiff
path: root/src/utils/schemas/pageSchema.ts
blob: ba8fd865bec65dae27afb3b4bed9bec648cf602b (plain) (blame)
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
import type { ProfilePage, WebPage } from "schema-dts";
import { personId, websiteId } from "./ids";

export type WebsiteSchemaParams = {
	readonly description: string;
	readonly lang: string;
	readonly mainEntityId?: string;
	readonly page: string;
	readonly siteUrl: string;
	readonly title: string;
	readonly type?: "WebPage" | "ProfilePage";
};

export default ({ siteUrl, page, title, description, lang, type = "WebPage", mainEntityId }: WebsiteSchemaParams): WebPage | ProfilePage => {
	const url = new URL(page, siteUrl).toString();

	const base = {
		"@type": type,
		"@id": url,
		"url": url,
		"name": title,
		"description": description,
		"inLanguage": lang,
		"isPartOf": { "@id": websiteId(siteUrl) },
	} as const;

	if (type === "ProfilePage") {
		return {
			...base,
			"@type": "ProfilePage",
			"mainEntity": { "@id": mainEntityId ?? personId(siteUrl) },
		};
	}

	return base;
};