-
Notifications
You must be signed in to change notification settings - Fork 3
/
contentlayer.config.ts
483 lines (430 loc) · 13.1 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
import path from "path";
import * as fs from "fs";
import { 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 CategoryInfo {
title: string;
slug: string;
optech_url: string;
categories: string[];
aliases?: string[];
excerpt: string;
}
interface TagInfo {
name: string;
slug: string;
count: number;
}
interface ContentTree {
[key: string]: ContentTree | ContentTranscriptType[];
}
/**
* Count the occurrences of all tags across transcripts and write to json file
*/
function createTagCount(allTranscripts: ContentTranscriptType[]): {
tagCounts: Record<string, number>;
} {
const tagCounts: Record<string, number> = {};
for (const file of allTranscripts) {
if (!file.tags) continue;
for (const tag of file.tags) {
const formattedTag = createSlug(tag);
tagCounts[formattedTag] = (tagCounts[formattedTag] || 0) + 1;
}
}
return { tagCounts };
}
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 getCategories = () => {
const filePath = path.join(process.cwd(), "public", "categories.json");
const fileContents = fs.readFileSync(filePath, "utf8");
return JSON.parse(fileContents);
};
function organizeTags(transcripts: ContentTranscriptType[]) {
const categories: CategoryInfo[] = getCategories();
const { tagCounts } = createTagCount(transcripts);
const tagsByCategory: { [category: string]: TagInfo[] } = {};
const tagsWithoutCategory = new Set<string>();
const categorizedTags = new Set<string>();
// Create a map for faster category lookup
const categoryMap = new Map<string, CategoryInfo>();
categories.forEach((cat) => {
cat.categories.forEach((category) => {
if (!tagsByCategory[category]) {
tagsByCategory[category] = [];
}
});
categoryMap.set(createSlug(cat.slug), cat);
cat.aliases?.forEach((alias) => categoryMap.set(alias, cat));
});
// Process all tags at once
const allTags = new Set(
transcripts.flatMap(
(transcript) => transcript.tags?.map((tag) => tag) || []
)
);
allTags.forEach((tag) => {
const catInfo = categoryMap.get(tag);
if (catInfo) {
catInfo.categories.forEach((category) => {
if (!tagsByCategory[category].some((t) => t.slug === tag)) {
tagsByCategory[category].push({
name: catInfo.title,
slug: tag,
count: tagCounts[tag] || 0,
});
}
});
categorizedTags.add(tag);
} else {
tagsWithoutCategory.add(tag);
}
});
// Add "Miscellaneous" category with remaining uncategorized tags
if (tagsWithoutCategory.size > 0) {
tagsByCategory["Miscellaneous"] = Array.from(tagsWithoutCategory).map(
(tag) => ({
name: tag,
slug: tag,
count: tagCounts[tag] || 0,
})
);
}
// Sort tags alphabetically within each category
Object.keys(tagsByCategory).forEach((category) => {
tagsByCategory[category].sort((a, b) => a.name.localeCompare(b.name));
});
fs.writeFileSync("./public/tag-data.json", JSON.stringify(tagsByCategory));
return { tagsByCategory, tagsWithoutCategory };
}
function organizeTopics(transcripts: ContentTranscriptType[]) {
const slugTopics: any = {};
const topicsArray: TopicsData[] = [];
transcripts.forEach((transcript) => {
const slugTags = transcript.tags?.map((tag) => ({
slug: createSlug(tag),
name: tag,
}));
slugTags?.forEach(({ slug, name }) => {
if (slugTopics[slug] !== undefined) {
const index = slugTopics[slug];
topicsArray[index].count += 1;
} else {
const topicsLength = topicsArray.length;
slugTopics[slug] = topicsLength;
topicsArray[topicsLength] = {
slug,
name,
count: 1,
};
}
});
});
fs.writeFileSync("./public/topics-data.json", JSON.stringify(topicsArray));
}
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: {
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",
"2018-08-17-richard-bondi-bitcoin-cli-regtest.es.md",
],
onSuccess: async (importData) => {
const { allTranscripts, allSources } = await importData();
organizeTags(allTranscripts);
createTypesCount(allTranscripts, allSources);
organizeTopics(allTranscripts);
getTranscriptAliases(allTranscripts);
createSpeakers(allTranscripts);
generateSourcesCount(allTranscripts, allSources);
organizeContent(allTranscripts, allSources);
},
});