-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-meta.mjs
More file actions
156 lines (144 loc) · 4.4 KB
/
Copy pathinject-meta.mjs
File metadata and controls
156 lines (144 loc) · 4.4 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
import { readFile, writeFile, readdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import {
buildRouteMetaMap,
routeToOgPath,
DIST,
SITE,
DEFAULT_IMAGE,
DEFAULT_DESC,
} from './lib/route-meta.mjs';
function escapeAttr(s) {
return String(s)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>');
}
function absoluteUrl(maybePath) {
if (!maybePath) return SITE + DEFAULT_IMAGE;
if (/^https?:\/\//.test(maybePath)) return maybePath;
return SITE + (maybePath.startsWith('/') ? maybePath : '/' + maybePath);
}
function resolveImage(route, meta) {
if (meta.image) return absoluteUrl(meta.image);
const generated = routeToOgPath(route);
const distPath = join(DIST, generated);
if (existsSync(distPath)) return absoluteUrl('/' + generated);
return absoluteUrl(DEFAULT_IMAGE);
}
function rewriteHead(html, route, meta) {
const title = meta.title || 'fezcodex';
const description = meta.description || DEFAULT_DESC;
const image = resolveImage(route, meta);
const url = SITE + route;
const type = meta.type || 'website';
const eTitle = escapeAttr(title);
const eDesc = escapeAttr(description);
const eImg = escapeAttr(image);
const eUrl = escapeAttr(url);
const eType = escapeAttr(type);
const subs = [
[/<title>[^<]*<\/title>/i, `<title>${eTitle}</title>`],
[
/<meta\s+name="description"\s+content="[^"]*"\s*\/?>/i,
`<meta name="description" content="${eDesc}" />`,
],
[
/<meta\s+property="og:type"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:type" content="${eType}" />`,
],
[
/<meta\s+property="og:url"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:url" content="${eUrl}" />`,
],
[
/<meta\s+property="og:title"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:title" content="${eTitle}" />`,
],
[
/<meta\s+property="og:description"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:description" content="${eDesc}" />`,
],
[
/<meta\s+property="og:image"\s+content="[^"]*"\s*\/?>/i,
`<meta property="og:image" content="${eImg}" />`,
],
[
/<meta\s+name="twitter:url"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:url" content="${eUrl}" />`,
],
[
/<meta\s+name="twitter:title"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:title" content="${eTitle}" />`,
],
[
/<meta\s+name="twitter:description"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:description" content="${eDesc}" />`,
],
[
/<meta\s+name="twitter:image"\s+content="[^"]*"\s*\/?>/i,
`<meta name="twitter:image" content="${eImg}" />`,
],
];
let out = html;
let hits = 0;
for (const [re, rep] of subs) {
const next = out.replace(re, rep);
if (next !== out) hits++;
out = next;
}
return { html: out, hits };
}
function fileToRoute(file) {
const rel = file.slice(DIST.length).replace(/\\/g, '/');
if (rel === '/index.html') return '/';
if (rel.endsWith('/index.html')) return rel.slice(0, -'/index.html'.length);
return null;
}
async function walkHtml(dir, out = []) {
for (const entry of await readdir(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) await walkHtml(full, out);
else if (entry.isFile() && entry.name === 'index.html') out.push(full);
}
return out;
}
async function main() {
const t0 = Date.now();
if (!existsSync(DIST)) {
console.error(`inject-meta: ${DIST} does not exist; run vite build first`);
process.exit(1);
}
const routeMap = buildRouteMetaMap();
console.log(`inject-meta: ${routeMap.size} routes have per-page metadata`);
const files = await walkHtml(DIST);
let touched = 0;
let skipped = 0;
let missing = 0;
for (const file of files) {
const route = fileToRoute(file);
if (route == null) {
skipped++;
continue;
}
const meta = routeMap.get(route);
if (!meta) {
missing++;
continue;
}
const html = await readFile(file, 'utf8');
const { html: next, hits } = rewriteHead(html, route, meta);
if (hits === 0) {
skipped++;
continue;
}
await writeFile(file, next, 'utf8');
touched++;
}
console.log(
`inject-meta: rewrote ${touched} file(s), ${missing} without per-page meta, ${skipped} skipped (${Date.now() - t0}ms)`
);
}
await main();