-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-seo.mjs
More file actions
142 lines (122 loc) · 4.9 KB
/
Copy pathinject-seo.mjs
File metadata and controls
142 lines (122 loc) · 4.9 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
import { readFileSync, writeFileSync, readdirSync, statSync, existsSync, copyFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { metadataByPage } from './constants/metadata.js';
import {
homePageSchema,
aboutPageSchema,
researchPageSchema,
eventsPageSchema,
escapeRoomsPageSchema,
} from './constants/schemas.js';
const BASE_URL = 'https://endgameproject.github.io';
const OG_IMAGE = `${BASE_URL}/assets/images/og-image.png`;
const schemaByKey = {
home: homePageSchema,
about: aboutPageSchema,
research: researchPageSchema,
events: eventsPageSchema,
escaperooms: escapeRoomsPageSchema,
};
export function getPageInfo(filePath) {
const rel = filePath.replace(/\\/g, '/').replace(/.*docs\//, '');
if (rel === 'index.html') return { key: 'home', canonical: `${BASE_URL}/` };
if (rel === 'about.html') return { key: 'about', canonical: `${BASE_URL}/about` };
if (rel === 'research.html') return { key: 'research', canonical: `${BASE_URL}/research` };
if (rel === 'events.html') return { key: 'events', canonical: `${BASE_URL}/events` };
if (rel === 'escaperooms.html') return { key: 'escaperooms', canonical: `${BASE_URL}/escaperooms` };
if (rel.startsWith('escaperooms/')) {
const slug = rel.replace('escaperooms/', '').replace(/\.html$/, '');
return { key: 'escaperooms', canonical: `${BASE_URL}/escaperooms/${slug}` };
}
if (rel.startsWith('events/')) {
const slug = rel.replace('events/', '').replace(/\.html$/, '');
return { key: 'events', canonical: `${BASE_URL}/events/${slug}` };
}
return { key: 'home', canonical: `${BASE_URL}/` };
}
export function injectMetaTags(html, metadata, canonicalUrl, schema) {
if (html.includes('<title>')) return html;
const { title, description, keywords } = metadata;
const schemaTag = schema
? `<script type="application/ld+json">${JSON.stringify(schema).replace(/<\/script>/gi, '<\\/script>')}</script>`
: '';
const tags = [
`<title>${title}</title>`,
`<meta name="description" content="${description}">`,
`<meta name="keywords" content="${keywords}">`,
`<meta name="robots" content="index, follow">`,
`<meta name="googlebot" content="index, follow">`,
`<link rel="canonical" href="${canonicalUrl}">`,
`<meta property="og:type" content="website">`,
`<meta property="og:url" content="${canonicalUrl}">`,
`<meta property="og:title" content="${title}">`,
`<meta property="og:description" content="${description}">`,
`<meta property="og:image" content="${OG_IMAGE}">`,
`<meta property="og:locale" content="en_US">`,
`<meta property="og:site_name" content="ENDGAME Project">`,
`<meta name="twitter:card" content="summary_large_image">`,
`<meta name="twitter:site" content="@endgame_project">`,
`<meta name="twitter:title" content="${title}">`,
`<meta name="twitter:description" content="${description}">`,
`<meta name="twitter:image" content="${OG_IMAGE}">`,
schemaTag,
].join('');
const result = html.replace(
/(<meta charSet="utf-8"\/>|<meta charset="utf-8"\/>)/i,
`$1${tags}`
);
return result;
}
function shouldSkip(rel) {
return (
rel.startsWith('superpowers/') ||
rel.startsWith('play/') ||
rel.endsWith('/play.html') ||
rel.startsWith('google') ||
rel === '404.html'
);
}
function walkHtml(dir, results = []) {
for (const entry of readdirSync(dir)) {
const full = path.join(dir, entry);
if (statSync(full).isDirectory()) {
walkHtml(full, results);
} else if (entry.endsWith('.html')) {
results.push(full);
}
}
return results;
}
async function main() {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const docsDir = path.join(__dirname, 'docs');
const ogSrc = path.join(__dirname, 'public/assets/images/bannerBg-placeholder.png');
const ogDest = path.join(__dirname, 'docs/assets/images/og-image.png');
if (!existsSync(ogDest)) {
copyFileSync(ogSrc, ogDest);
console.log(`Copied OG image → ${ogDest}`);
}
const files = walkHtml(docsDir);
let count = 0;
for (const file of files) {
const rel = file.replace(/\\/g, '/').replace(/.*docs\//, '');
if (shouldSkip(rel)) continue;
const { key, canonical } = getPageInfo(file);
const metadata = metadataByPage[key]?.en ?? metadataByPage.home.en;
const schema = schemaByKey[key] ?? null;
const html = readFileSync(file, 'utf8');
const injected = injectMetaTags(html, metadata, canonical, schema);
if (injected !== html) {
writeFileSync(file, injected, 'utf8');
console.log(`Injected: ${rel}`);
count++;
} else if (!html.includes('<title>')) {
console.warn(`Warning: no charset anchor found in ${rel}, skipping`);
}
}
console.log(`\nSEO injection complete: ${count} files updated.`);
}
if (process.argv[1] === fileURLToPath(import.meta.url)) {
main().catch(err => { console.error(err); process.exit(1); });
}