Repository navigation
Expand file tree
/
Copy pathtool-pages.ts
More file actions
146 lines (131 loc) · 5.15 KB
/
Copy pathtool-pages.ts
File metadata and controls
146 lines (131 loc) · 5.15 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
import site from '@/config/site.json';
import { englishHead, type PageHeadId } from './head-text';
import { localizedPath, type PageLanguage } from './localized-routes';
/**
* Heads and structured data for the free tools under /tools. pages.ts spreads
* TOOL_PAGE_HEADS into PAGE_HEADS, so the prerender and usePageHead treat
* these like any other public page.
*/
const BRAND = site.brand;
const ORIGIN = site.origin;
/** Each tool page's `pageHead.<id>` catalog section, where its title and description live. */
export const TOOL_PAGE_HEAD_IDS = {
'/tools': 'tools',
'/tools/debate-timer': 'debateTimer',
'/tools/debate-motion-generator': 'motionGenerator',
'/tools/debate-flow-sheet': 'flowSheet',
'/tools/speech-time-calculator': 'speechTimeCalculator',
} as const satisfies Record<string, PageHeadId>;
export type ToolRoute = keyof typeof TOOL_PAGE_HEAD_IDS;
export const TOOL_PAGE_HEADS = Object.fromEntries(
Object.entries(TOOL_PAGE_HEAD_IDS).map(([route, id]) => [route, { ...englishHead(id), canonicalPath: route }]),
) as Record<ToolRoute, { title: string; description: string; canonicalPath: ToolRoute }>;
export type ToolPageRoute = Exclude<ToolRoute, '/tools'>;
/** The last time each page's content changed in substance; bump it with the change. */
export const TOOL_PAGES_MODIFIED: Record<ToolRoute, string> = {
'/tools': '2026-10-29',
'/tools/debate-timer': '2026-10-27',
'/tools/debate-motion-generator': '2026-10-22',
'/tools/debate-flow-sheet': '2026-10-26',
'/tools/speech-time-calculator': '2026-10-26',
};
export interface FaqEntry {
question: string;
answer: string;
}
const ORGANIZATION_ID = `${ORIGIN}/#organization`;
const WEBSITE_ID = `${ORIGIN}/#website`;
/**
* A tool page in Hungarian or Spanish: its head in that language, the hub's
* name for the breadcrumb, and when its copy last changed. English pages pass none.
*/
export interface ToolPageLocale {
language: Exclude<PageLanguage, 'en'>;
title: string;
description: string;
hubName: string;
modified: string;
}
const pageUrl = (route: ToolRoute, locale?: ToolPageLocale) => `${ORIGIN}${localizedPath(route, locale?.language ?? 'en')}`;
function breadcrumbs(route: ToolRoute, name: string, locale?: ToolPageLocale) {
const items = [
{ name: BRAND, url: `${ORIGIN}${localizedPath('/', locale?.language ?? 'en')}` },
{ name: locale?.hubName ?? 'Free debate tools', url: pageUrl('/tools', locale) },
...(route === '/tools' ? [] : [{ name, url: pageUrl(route, locale) }]),
];
return {
'@type': 'BreadcrumbList',
itemListElement: items.map((item, index) => ({ '@type': 'ListItem', position: index + 1, name: item.name, item: item.url })),
};
}
function webPage(route: ToolRoute, type: string, locale?: ToolPageLocale) {
const head = locale ?? TOOL_PAGE_HEADS[route];
return {
'@type': type,
'@id': `${pageUrl(route, locale)}#webpage`,
url: pageUrl(route, locale),
name: head.title,
description: head.description,
...(locale ? { inLanguage: locale.language } : {}),
isPartOf: { '@id': WEBSITE_ID },
dateModified: locale?.modified ?? TOOL_PAGES_MODIFIED[route],
};
}
/** One tool page: the web app itself, where it sits, and its FAQ. No ratings or reviews: there are none to report. */
export function toolStructuredData(route: ToolPageRoute, name: string, faq: readonly FaqEntry[], locale?: ToolPageLocale) {
const url = pageUrl(route, locale);
return {
'@context': 'https://schema.org',
'@graph': [
{
'@type': 'WebApplication',
'@id': `${url}#app`,
name,
url,
description: (locale ?? TOOL_PAGE_HEADS[route]).description,
...(locale ? { inLanguage: locale.language } : {}),
applicationCategory: 'EducationalApplication',
operatingSystem: 'Any (web browser)',
isAccessibleForFree: true,
offers: { '@type': 'Offer', price: '0', priceCurrency: 'USD' },
publisher: { '@id': ORGANIZATION_ID },
dateModified: locale?.modified ?? TOOL_PAGES_MODIFIED[route],
},
webPage(route, 'WebPage', locale),
breadcrumbs(route, name, locale),
{
'@type': 'FAQPage',
'@id': `${url}#faq`,
mainEntity: faq.map((entry) => ({
'@type': 'Question',
name: entry.question,
acceptedAnswer: { '@type': 'Answer', text: entry.answer },
})),
},
],
};
}
export function hubStructuredData(tools: readonly { route: ToolPageRoute; name: string }[], locale?: ToolPageLocale) {
return {
'@context': 'https://schema.org',
'@graph': [
{
...webPage('/tools', 'CollectionPage', locale),
mainEntity: {
'@type': 'ItemList',
itemListElement: tools.map((tool, index) => ({
'@type': 'ListItem',
position: index + 1,
name: tool.name,
url: pageUrl(tool.route, locale),
})),
},
},
breadcrumbs('/tools', locale?.hubName ?? 'Free debate tools', locale),
],
};
}
/** The JSON for a <script type="application/ld+json">, with "<" escaped so it cannot close the element. */
export function jsonLd(data: object): string {
return JSON.stringify(data).replaceAll('<', '\\u003c');
}