-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscoverRoutes.js
More file actions
95 lines (89 loc) · 2.85 KB
/
discoverRoutes.js
File metadata and controls
95 lines (89 loc) · 2.85 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
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { staticRoutes } from './routes.js';
const PUBLIC = 'public';
function readJson(path) {
try {
return JSON.parse(readFileSync(path, 'utf8'));
} catch {
return null;
}
}
function blogRoutes() {
const posts = readJson(join(PUBLIC, 'posts', 'posts.json')) || [];
const out = new Set();
for (const entry of posts) {
if (!entry?.slug) continue;
if (entry.series?.posts?.length) {
out.add(`/blog/series/${entry.slug}`);
for (const ep of entry.series.posts) {
if (ep?.slug) out.add(`/blog/series/${entry.slug}/${ep.slug}`);
}
} else {
out.add(`/blog/${entry.slug}`);
}
}
return out;
}
function logsRoutes() {
const out = new Set();
const root = join(PUBLIC, 'logs');
if (!existsSync(root)) return out;
for (const category of readdirSync(root, { withFileTypes: true })) {
if (!category.isDirectory()) continue;
const dir = join(root, category.name);
for (const file of readdirSync(dir)) {
if (file.endsWith('.txt')) {
out.add(`/logs/${category.name}/${file.slice(0, -4)}`);
}
}
// Some categories (e.g. quote) keep their entries inline in the
// category piml with no per-slug .txt — pull slugs from there too.
const pimlPath = join(dir, `${category.name}.piml`);
if (existsSync(pimlPath)) {
const text = readFileSync(pimlPath, 'utf8');
for (const line of text.split(/\r?\n/)) {
const m = line.match(/^\s*\(slug\)\s*(\S+)/);
if (m) out.add(`/logs/${category.name}/${m[1]}`);
}
}
}
return out;
}
function storyBookRoutes() {
const out = new Set();
const files = ['books_en.piml', 'books_tr.piml']
.map((f) => join(PUBLIC, 'stories', f))
.filter(existsSync);
const bookIds = new Set();
const bookEpisodes = new Map();
for (const f of files) {
const text = readFileSync(f, 'utf8');
let currentBook = null;
for (const line of text.split(/\r?\n/)) {
const bookMatch = line.match(/\(bookId\)\s*(\S+)/);
if (bookMatch) {
currentBook = bookMatch[1];
bookIds.add(currentBook);
if (!bookEpisodes.has(currentBook)) bookEpisodes.set(currentBook, new Set());
continue;
}
const epMatch = line.match(/^\s*\(id\)\s*(\S+)/);
if (epMatch && currentBook) {
bookEpisodes.get(currentBook).add(epMatch[1]);
}
}
}
for (const id of bookIds) out.add(`/stories/books/${id}`);
for (const [book, eps] of bookEpisodes) {
for (const ep of eps) out.add(`/stories/books/${book}/pages/${ep}`);
}
return out;
}
export function discoverAllRoutes() {
const all = new Set(staticRoutes);
for (const r of blogRoutes()) all.add(r);
for (const r of logsRoutes()) all.add(r);
for (const r of storyBookRoutes()) all.add(r);
return Array.from(all);
}