-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontentlayer.config.ts
509 lines (453 loc) · 14 KB
/
contentlayer.config.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
import path from "path";
import * as fs from "fs";
import {
ContentData,
createSlug,
SpeakerData,
TopicsData,
unsluggify,
} from "./src/utils";
import {
defineDocumentType,
defineNestedType,
makeSource,
} from "contentlayer2/source-files";
import {
Transcript as ContentTranscriptType,
Source as ContentSourceType,
} from "./.contentlayer/generated/types";
import { LanguageCodes } from "./src/config";
const Resources = defineNestedType(() => ({
name: "Resources",
fields: {
title: { type: "string" },
url: { type: "string" },
},
}));
export interface Topic {
title: string;
slug: string;
optech_url: string;
categories: string[];
aliases?: string[];
excerpt: string;
}
// The full processed topic we use internally
interface ProcessedTopic {
name: string; // Display name (from topic.title or original tag)
slug: string; // Slugified identifier
count: number; // Number of occurrences
categories: string[]; // List of categories it belongs to
}
interface TagInfo {
name: string;
slug: string;
count: number;
}
interface ContentTree {
[key: string]: ContentTree | ContentTranscriptType[];
}
const getTranscriptAliases = (allTranscripts: ContentTranscriptType[]) => {
const aliases: Record<string, string> = {};
for (const transcript of allTranscripts) {
if (!transcript.aliases) continue;
if (transcript.aliases) {
transcript.aliases.forEach((alias) => {
aliases[alias] = transcript.url;
});
}
}
fs.writeFileSync("./public/aliases.json", JSON.stringify(aliases));
};
const getTopics = () => {
const filePath = path.join(process.cwd(), "public", "topics.json");
const fileContents = fs.readFileSync(filePath, "utf8");
return JSON.parse(fileContents);
};
function buildTopicsMap(
transcripts: ContentTranscriptType[],
topics: Topic[]
): Map<string, ProcessedTopic> {
// Create topics lookup map (includes aliases)
const topicsLookup = new Map<string, Topic>();
topics.forEach((topic) => {
topicsLookup.set(topic.slug, topic);
topic.aliases?.forEach((alias) => topicsLookup.set(alias, topic));
});
// Build the main topics map
const processedTopics = new Map<string, ProcessedTopic>();
// Process all transcripts
transcripts.forEach((transcript) => {
transcript.tags?.forEach((tag) => {
const slug = createSlug(tag);
const topic = topicsLookup.get(slug);
if (!processedTopics.has(slug)) {
processedTopics.set(slug, {
name: topic?.title || tag,
slug,
count: 1,
categories: topic?.categories || ["Miscellaneous"],
});
} else {
const processed = processedTopics.get(slug)!;
processed.count += 1;
}
});
});
return processedTopics;
}
function generateAlphabeticalList(
processedTopics: Map<string, ProcessedTopic>
): TopicsData[] {
const result: TopicsData[] = [];
// The categories property is not needed for this list, so we drop it
for (const { name, slug, count } of processedTopics.values()) {
result.push({ name, slug, count });
}
return result.sort((a, b) => a.name.localeCompare(b.name));
}
function generateCategorizedList(
processedTopics: Map<string, ProcessedTopic>
): Record<string, TopicsData[]> {
const categorizedTopics: Record<string, TopicsData[]> = {};
Array.from(processedTopics.values()).forEach(
({ name, slug, count, categories }) => {
categories.forEach((category) => {
if (!categorizedTopics[category]) {
categorizedTopics[category] = [];
}
// Check if topic name contains category name and ends with "(Miscellaneous)"
const modifiedName =
name.includes(category) && name.endsWith("(Miscellaneous)")
? "Miscellaneous"
: name;
categorizedTopics[category].push({ name: modifiedName, slug, count });
});
}
);
// Sort topics within each category
Object.values(categorizedTopics).forEach((topics) => {
topics.sort((a, b) => {
if (a.name == "Miscellaneous") return 1;
if (b.name == "Miscellaneous") return -1;
return a.name.localeCompare(b.name);
});
});
return categorizedTopics;
}
function generateTopicsCounts(transcripts: ContentTranscriptType[]) {
// Get topics
const topics = getTopics();
// Build the primary data structure
const processedTopics = buildTopicsMap(transcripts, topics);
// Generate both output formats
const alphabeticalList = generateAlphabeticalList(processedTopics);
const categorizedList = generateCategorizedList(processedTopics);
// Write output files
fs.writeFileSync(
"./public/topics-counts.json",
JSON.stringify(alphabeticalList, null, 2)
);
fs.writeFileSync(
"./public/topics-by-category-counts.json",
JSON.stringify(categorizedList, null, 2)
);
}
function createSpeakers(transcripts: ContentTranscriptType[]) {
const slugSpeakers: any = {};
const speakerArray: SpeakerData[] = [];
transcripts.forEach((transcript) => {
const slugSpeakersArray = transcript.speakers?.map((speaker) => ({
slug: createSlug(speaker),
name: speaker,
}));
slugSpeakersArray?.forEach(({ slug, name }) => {
if (slugSpeakers[slug] !== undefined) {
const index = slugSpeakers[slug];
speakerArray[index].count += 1;
} else {
const speakersLength = speakerArray.length;
slugSpeakers[slug] = speakersLength;
speakerArray[speakersLength] = {
slug,
name,
count: 1,
};
}
});
});
fs.writeFileSync("./public/speaker-data.json", JSON.stringify(speakerArray));
}
function generateSourcesCount(
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) {
const sourcesArray: TagInfo[] = [];
const slugSources: Record<string, number> = {};
transcripts.forEach((transcript) => {
const slug = transcript._raw.flattenedPath.split("/")[0];
if (slugSources[slug] !== undefined) {
sourcesArray[slugSources[slug]].count += 1;
} else {
const sourcesLength = sourcesArray.length;
slugSources[slug] = sourcesLength;
const getSourceName = (slug: string) =>
sources.find(
(source) =>
source.language === "en" && source.slugAsParams[0] === slug
)?.title ?? unsluggify(slug);
sourcesArray[sourcesLength] = {
slug,
name: getSourceName(slug),
count: 1,
};
}
});
fs.writeFileSync(
"./public/source-count-data.json",
JSON.stringify(sourcesArray)
);
return { sourcesArray, slugSources };
}
const createTypesCount = (
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) => {
const { sourcesArray, slugSources } = generateSourcesCount(
transcripts,
sources
);
const nestedTypes: any = {};
sources.forEach((transcript) => {
if (transcript.types) {
transcript.types.forEach((type) => {
const slugType = type.charAt(0).toUpperCase() + type.slice(1);
const slug = transcript.slugAsParams[0];
const sourceIndex = slugSources[slug];
const getSource = sourcesArray[sourceIndex] ?? null;
if (!nestedTypes[slugType]) {
nestedTypes[slugType] = [];
} else {
if (nestedTypes[slugType].includes(getSource) || getSource === null)
return;
nestedTypes[slugType].push(getSource);
}
});
}
});
fs.writeFileSync("./public/types-data.json", JSON.stringify(nestedTypes));
};
function organizeContent(
transcripts: ContentTranscriptType[],
sources: ContentSourceType[]
) {
const tree: any = {};
sources.forEach((source) => {
const {
_id,
slugAsParams,
language,
_raw,
weight,
body,
hosts,
transcription_coverage,
url,
type,
types,
...metaData
} = source;
const params = source.slugAsParams;
const topParam = params[0] as string;
const nestedSource = params.length > 1;
if (!tree[topParam]) {
tree[topParam] = {};
}
const allTranscriptsForSourceLanguage = transcripts.filter(
(transcript) =>
transcript._raw.sourceFileDir === source._raw.sourceFileDir &&
transcript.language === language
);
const allTranscriptsForSourceLanguageURLs =
allTranscriptsForSourceLanguage.map((transcript) => transcript.url);
if (!nestedSource) {
tree[topParam] = {
...tree[topParam],
[language]: {
data: allTranscriptsForSourceLanguageURLs.length
? allTranscriptsForSourceLanguageURLs
: {},
metadata: {
...metaData,
},
},
};
} else {
tree[topParam][language].data = {
...tree[topParam][language].data,
[params[1]]: {
data: allTranscriptsForSourceLanguageURLs.length
? allTranscriptsForSourceLanguageURLs
: {},
metadata: {
...metaData,
},
},
};
}
});
fs.writeFileSync("./public/sources-data.json", JSON.stringify(tree, null, 2));
}
const getLanCode = /[.]\w{2}$/gi; // Removes the last two characters if there's a dot
export const Transcript = defineDocumentType(() => ({
name: "Transcript",
filePathPattern: `**/*.md`,
contentType: "markdown",
fields: {
title: { type: "string", required: true },
speakers: { type: "list", of: { type: "string" } },
date: { type: "date" },
transcript_by: { type: "string" },
Transcript_by: { type: "string" },
categories: { type: "list", of: { type: "string" } },
tag: { type: "list", of: { type: "string" } },
tags: { type: "list", of: { type: "string" } },
media: { type: "string" },
translation_by: { type: "string" },
episode: { type: "number" },
aliases: { type: "list", of: { type: "string" } },
video: { type: "string" },
hosts: { type: "list", of: { type: "string" } },
source: { type: "string" },
transcription_coverage: { type: "string" },
summary: { type: "string" },
needs: { type: "string" },
aditional_resources: { type: "list", of: Resources },
additional_resources: { type: "list", of: Resources },
weight: { type: "number" },
types: { type: "list", of: { type: "string" } },
source_file: { type: "string" },
},
computedFields: {
tagsDetailed: {
type: "list",
resolve: (doc) => {
// doc?.tags doesn't give an array in contentLayer so we do _array to get it
const topicsStore = doc?.tags as any || [];
const topics = (topicsStore?._array as string[]) ?? [];
const topicsWithTitles = topics.map((topic) => {
const currentTopic = getTopics().find(
(topicData: ContentData) => topicData.slug === topic
);
if(currentTopic?.title && currentTopic?.title.includes("(Miscellaneous)")) {
return {
name: currentTopic?.title.replace("(Miscellaneous)",""),
slug: currentTopic.slug,
}
}
return {
name: currentTopic?.title || topic,
slug: currentTopic?.slug || topic,
};
});
return topicsWithTitles;
},
},
url: {
type: "string",
resolve: (doc) => `/${doc._raw.flattenedPath}`,
},
language: {
type: "string",
resolve: (doc) => {
const transcript = doc._raw.flattenedPath.split("/").pop();
const lan = transcript?.match(getLanCode);
const languageCode = (lan?.[lan.length - 1] || "").replace(".", "");
const finalLanguage = LanguageCodes.includes(languageCode)
? languageCode
: "en";
return finalLanguage;
},
},
languageURL: {
type: "string",
resolve: (doc) => {
const transcript = doc._raw.flattenedPath.split("/").pop();
const fullPathWithoutDot = doc._raw.flattenedPath.replace(
getLanCode,
""
);
const lan = transcript?.match(getLanCode);
const languageCode = (lan?.[0] || "").replace(".", "");
if (LanguageCodes.includes(languageCode)) {
return `/${languageCode}/${fullPathWithoutDot}`;
}
return `/${fullPathWithoutDot}`;
},
},
slugAsParams: {
type: "list",
resolve: (doc) => {
const pathWithoutDot = doc._raw.flattenedPath.replace(getLanCode, "");
return pathWithoutDot.split("/");
},
},
},
}));
export const Source = defineDocumentType(() => ({
name: "Source",
filePathPattern: `**/_index{,.??}.md`,
contentType: "markdown",
fields: {
title: { type: "string", required: true },
source: { type: "string" },
transcription_coverage: { type: "string" },
hosts: { type: "list", of: { type: "string" } },
weight: { type: "number" },
website: { type: "string" },
types: { type: "list", of: { type: "string" } },
additional_resources: { type: "list", of: Resources },
},
computedFields: {
url: {
type: "string",
resolve: (doc) =>
`/${doc._raw.flattenedPath.split("/").slice(0, -1).join("/")}`,
},
language: {
type: "string",
resolve: (doc) => {
const index = doc._raw.flattenedPath.split("/").pop();
const lan =
index?.split(".").length === 2 ? index?.split(".")[1] : "en";
return lan;
},
},
slugAsParams: {
type: "list",
resolve: (doc) => doc._raw.flattenedPath.split("/").slice(0, -1),
},
},
}));
export default makeSource({
contentDirPath: path.join(process.cwd(), "public", "bitcoin-transcript"),
documentTypes: [Source, Transcript],
contentDirExclude: [
".github",
".gitignore",
"LICENSE.md",
"README.md",
"STYLE.md",
"twitter_handles.json",
".json",
],
onSuccess: async (importData) => {
const { allTranscripts, allSources } = await importData();
generateTopicsCounts(allTranscripts);
createTypesCount(allTranscripts, allSources);
getTranscriptAliases(allTranscripts);
createSpeakers(allTranscripts);
generateSourcesCount(allTranscripts, allSources);
organizeContent(allTranscripts, allSources);
},
});