-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute-meta.mjs
More file actions
99 lines (92 loc) · 2.77 KB
/
Copy pathroute-meta.mjs
File metadata and controls
99 lines (92 loc) · 2.77 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
import { readFileSync, existsSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
export const PUBLIC = 'public';
export const DIST = 'dist/client';
export const SITE = 'https://fezcode.com';
export const DEFAULT_IMAGE = '/images/asset/ogtitle.png';
export const DEFAULT_DESC = 'codex by fezcode...';
export function readJson(path) {
try {
return JSON.parse(readFileSync(path, 'utf8'));
} catch {
return null;
}
}
export function parsePimlItems(text) {
const items = [];
let current = null;
for (const rawLine of text.split(/\r?\n/)) {
const line = rawLine.trim();
if (line.startsWith('> (item)')) {
if (current) items.push(current);
current = {};
continue;
}
if (!current) continue;
const m = line.match(/^\(([\w-]+)\)\s*(.*)$/);
if (m) current[m[1]] = m[2];
}
if (current) items.push(current);
return items;
}
function blogMeta(routeMap, posts) {
for (const entry of posts) {
if (!entry?.slug) continue;
if (entry.series?.posts?.length) {
routeMap.set(`/blog/series/${entry.slug}`, {
title: entry.title,
description: entry.description,
image: entry.image,
type: 'article',
});
for (const ep of entry.series.posts) {
if (!ep?.slug) continue;
routeMap.set(`/blog/series/${entry.slug}/${ep.slug}`, {
title: ep.title || entry.title,
description: ep.description || entry.description,
image: ep.image || entry.image,
type: 'article',
});
}
} else {
routeMap.set(`/blog/${entry.slug}`, {
title: entry.title,
description: entry.description,
image: entry.image,
type: 'article',
});
}
}
}
function logsMeta(routeMap) {
const root = join(PUBLIC, 'logs');
if (!existsSync(root)) return;
for (const cat of readdirSync(root, { withFileTypes: true })) {
if (!cat.isDirectory()) continue;
const pimlPath = join(root, cat.name, `${cat.name}.piml`);
if (!existsSync(pimlPath)) continue;
const items = parsePimlItems(readFileSync(pimlPath, 'utf8'));
for (const it of items) {
if (!it.slug) continue;
routeMap.set(`/logs/${cat.name}/${it.slug}`, {
title: it.title?.replace(/^"|"$/g, '') || cat.name,
description: it.description,
image: it.image,
type: 'article',
});
}
}
}
export function buildRouteMetaMap() {
const map = new Map();
const posts = readJson(join(PUBLIC, 'posts', 'posts.json')) || [];
blogMeta(map, posts);
logsMeta(map);
return map;
}
export function routeToOgPath(route) {
// /blog/foo -> images/og/blog/foo.webp
// /logs/book/bar -> images/og/logs/book/bar.webp
const clean = route.replace(/^\//, '').replace(/\/$/, '');
return `images/og/${clean}.webp`;
}