1
1
import path from "path" ;
2
2
import * as fs from "fs" ;
3
3
import { createSlug , SpeakerData , TopicsData , unsluggify } from "./src/utils" ;
4
- import { defineDocumentType , defineNestedType , makeSource } from "contentlayer2/source-files" ;
5
- import { Transcript as ContentTranscriptType , Source as ContentSourceType } from "./.contentlayer/generated/types" ;
4
+ import {
5
+ defineDocumentType ,
6
+ defineNestedType ,
7
+ makeSource ,
8
+ } from "contentlayer2/source-files" ;
9
+ import {
10
+ Transcript as ContentTranscriptType ,
11
+ Source as ContentSourceType ,
12
+ } from "./.contentlayer/generated/types" ;
13
+ import { LanguageCodes } from "./src/config" ;
6
14
7
15
const Resources = defineNestedType ( ( ) => ( {
8
16
name : "Resources" ,
@@ -94,7 +102,11 @@ function organizeTags(transcripts: ContentTranscriptType[]) {
94
102
} ) ;
95
103
96
104
// Process all tags at once
97
- const allTags = new Set ( transcripts . flatMap ( ( transcript ) => transcript . tags ?. map ( ( tag ) => tag ) || [ ] ) ) ;
105
+ const allTags = new Set (
106
+ transcripts . flatMap (
107
+ ( transcript ) => transcript . tags ?. map ( ( tag ) => tag ) || [ ]
108
+ )
109
+ ) ;
98
110
99
111
allTags . forEach ( ( tag ) => {
100
112
const catInfo = categoryMap . get ( tag ) ;
@@ -116,11 +128,13 @@ function organizeTags(transcripts: ContentTranscriptType[]) {
116
128
117
129
// Add "Miscellaneous" category with remaining uncategorized tags
118
130
if ( tagsWithoutCategory . size > 0 ) {
119
- tagsByCategory [ "Miscellaneous" ] = Array . from ( tagsWithoutCategory ) . map ( ( tag ) => ( {
120
- name : tag ,
121
- slug : tag ,
122
- count : tagCounts [ tag ] || 0 ,
123
- } ) ) ;
131
+ tagsByCategory [ "Miscellaneous" ] = Array . from ( tagsWithoutCategory ) . map (
132
+ ( tag ) => ( {
133
+ name : tag ,
134
+ slug : tag ,
135
+ count : tagCounts [ tag ] || 0 ,
136
+ } )
137
+ ) ;
124
138
}
125
139
126
140
// Sort tags alphabetically within each category
@@ -190,7 +204,10 @@ function createSpeakers(transcripts: ContentTranscriptType[]) {
190
204
fs . writeFileSync ( "./public/speaker-data.json" , JSON . stringify ( speakerArray ) ) ;
191
205
}
192
206
193
- function generateSourcesCount ( transcripts : ContentTranscriptType [ ] , sources : ContentSourceType [ ] ) {
207
+ function generateSourcesCount (
208
+ transcripts : ContentTranscriptType [ ] ,
209
+ sources : ContentSourceType [ ]
210
+ ) {
194
211
const sourcesArray : TagInfo [ ] = [ ] ;
195
212
const slugSources : Record < string , number > = { } ;
196
213
@@ -204,7 +221,10 @@ function generateSourcesCount(transcripts: ContentTranscriptType[], sources: Con
204
221
slugSources [ slug ] = sourcesLength ;
205
222
206
223
const getSourceName = ( slug : string ) =>
207
- sources . find ( ( source ) => source . language === "en" && source . slugAsParams [ 0 ] === slug ) ?. title ?? unsluggify ( slug ) ;
224
+ sources . find (
225
+ ( source ) =>
226
+ source . language === "en" && source . slugAsParams [ 0 ] === slug
227
+ ) ?. title ?? unsluggify ( slug ) ;
208
228
209
229
sourcesArray [ sourcesLength ] = {
210
230
slug,
@@ -214,12 +234,21 @@ function generateSourcesCount(transcripts: ContentTranscriptType[], sources: Con
214
234
}
215
235
} ) ;
216
236
217
- fs . writeFileSync ( "./public/source-count-data.json" , JSON . stringify ( sourcesArray ) ) ;
237
+ fs . writeFileSync (
238
+ "./public/source-count-data.json" ,
239
+ JSON . stringify ( sourcesArray )
240
+ ) ;
218
241
return { sourcesArray, slugSources } ;
219
242
}
220
243
221
- const createTypesCount = ( transcripts : ContentTranscriptType [ ] , sources : ContentSourceType [ ] ) => {
222
- const { sourcesArray, slugSources } = generateSourcesCount ( transcripts , sources ) ;
244
+ const createTypesCount = (
245
+ transcripts : ContentTranscriptType [ ] ,
246
+ sources : ContentSourceType [ ]
247
+ ) => {
248
+ const { sourcesArray, slugSources } = generateSourcesCount (
249
+ transcripts ,
250
+ sources
251
+ ) ;
223
252
const nestedTypes : any = { } ;
224
253
225
254
sources . forEach ( ( transcript ) => {
@@ -234,7 +263,8 @@ const createTypesCount = (transcripts: ContentTranscriptType[], sources: Content
234
263
if ( ! nestedTypes [ slugType ] ) {
235
264
nestedTypes [ slugType ] = [ ] ;
236
265
} else {
237
- if ( nestedTypes [ slugType ] . includes ( getSource ) || getSource === null ) return ;
266
+ if ( nestedTypes [ slugType ] . includes ( getSource ) || getSource === null )
267
+ return ;
238
268
nestedTypes [ slugType ] . push ( getSource ) ;
239
269
}
240
270
} ) ;
@@ -244,11 +274,27 @@ const createTypesCount = (transcripts: ContentTranscriptType[], sources: Content
244
274
fs . writeFileSync ( "./public/types-data.json" , JSON . stringify ( nestedTypes ) ) ;
245
275
} ;
246
276
247
- function organizeContent ( transcripts : ContentTranscriptType [ ] , sources : ContentSourceType [ ] ) {
277
+ function organizeContent (
278
+ transcripts : ContentTranscriptType [ ] ,
279
+ sources : ContentSourceType [ ]
280
+ ) {
248
281
const tree : any = { } ;
249
282
250
283
sources . forEach ( ( source ) => {
251
- const { _id, slugAsParams, language, _raw, weight, body, hosts, transcription_coverage, url, type, types, ...metaData } = source ;
284
+ const {
285
+ _id,
286
+ slugAsParams,
287
+ language,
288
+ _raw,
289
+ weight,
290
+ body,
291
+ hosts,
292
+ transcription_coverage,
293
+ url,
294
+ type,
295
+ types,
296
+ ...metaData
297
+ } = source ;
252
298
const params = source . slugAsParams ;
253
299
const topParam = params [ 0 ] as string ;
254
300
const nestedSource = params . length > 1 ;
@@ -257,16 +303,21 @@ function organizeContent(transcripts: ContentTranscriptType[], sources: ContentS
257
303
tree [ topParam ] = { } ;
258
304
}
259
305
const allTranscriptsForSourceLanguage = transcripts . filter (
260
- ( transcript ) => transcript . _raw . sourceFileDir === source . _raw . sourceFileDir && transcript . language === language
306
+ ( transcript ) =>
307
+ transcript . _raw . sourceFileDir === source . _raw . sourceFileDir &&
308
+ transcript . language === language
261
309
) ;
262
310
263
- const allTranscriptsForSourceLanguageURLs = allTranscriptsForSourceLanguage . map ( ( transcript ) => transcript . url ) ;
311
+ const allTranscriptsForSourceLanguageURLs =
312
+ allTranscriptsForSourceLanguage . map ( ( transcript ) => transcript . url ) ;
264
313
265
314
if ( ! nestedSource ) {
266
315
tree [ topParam ] = {
267
316
...tree [ topParam ] ,
268
317
[ language ] : {
269
- data : allTranscriptsForSourceLanguageURLs . length ? allTranscriptsForSourceLanguageURLs : { } ,
318
+ data : allTranscriptsForSourceLanguageURLs . length
319
+ ? allTranscriptsForSourceLanguageURLs
320
+ : { } ,
270
321
metadata : {
271
322
...metaData ,
272
323
} ,
@@ -276,7 +327,9 @@ function organizeContent(transcripts: ContentTranscriptType[], sources: ContentS
276
327
tree [ topParam ] [ language ] . data = {
277
328
...tree [ topParam ] [ language ] . data ,
278
329
[ params [ 1 ] ] : {
279
- data : allTranscriptsForSourceLanguageURLs . length ? allTranscriptsForSourceLanguageURLs : { } ,
330
+ data : allTranscriptsForSourceLanguageURLs . length
331
+ ? allTranscriptsForSourceLanguageURLs
332
+ : { } ,
280
333
metadata : {
281
334
...metaData ,
282
335
} ,
@@ -288,6 +341,8 @@ function organizeContent(transcripts: ContentTranscriptType[], sources: ContentS
288
341
fs . writeFileSync ( "./public/sources-data.json" , JSON . stringify ( tree , null , 2 ) ) ;
289
342
}
290
343
344
+ const getLanCode = / [ . ] \w { 2 } $ / gi // Removes the last two characters if there's a dot
345
+
291
346
export const Transcript = defineDocumentType ( ( ) => ( {
292
347
name : "Transcript" ,
293
348
filePathPattern : `**/*.md` ,
@@ -322,16 +377,45 @@ export const Transcript = defineDocumentType(() => ({
322
377
type : "string" ,
323
378
resolve : ( doc ) => `/${ doc . _raw . flattenedPath } ` ,
324
379
} ,
325
- slugAsParams : {
326
- type : "list" ,
327
- resolve : ( doc ) => doc . _raw . flattenedPath . split ( "/" ) ,
328
- } ,
329
380
language : {
330
381
type : "string" ,
331
382
resolve : ( doc ) => {
332
383
const transcript = doc . _raw . flattenedPath . split ( "/" ) . pop ( ) ;
333
- const lan = transcript ?. split ( "." ) . length === 2 ? transcript ?. split ( "." ) [ 1 ] : "en" ;
334
- return lan ;
384
+ const lan = transcript ?. match ( getLanCode ) ;
385
+ const languageCode = ( lan ?. [ lan . length - 1 ] || "" ) . replace ( "." , "" ) ;
386
+ const finalLanguage = LanguageCodes . includes ( languageCode )
387
+ ? languageCode
388
+ : "en" ;
389
+ return finalLanguage ;
390
+ } ,
391
+ } ,
392
+ languageURL : {
393
+ type : "string" ,
394
+ resolve : ( doc ) => {
395
+ const transcript = doc . _raw . flattenedPath . split ( "/" ) . pop ( ) ;
396
+ const fullPathWithoutDot = doc . _raw . flattenedPath . replace (
397
+ getLanCode ,
398
+ ""
399
+ ) ;
400
+
401
+ const lan = transcript ?. match ( getLanCode ) ;
402
+ const languageCode = ( lan ?. [ 0 ] || "" ) . replace ( "." , "" )
403
+
404
+ if ( LanguageCodes . includes ( languageCode ) ) {
405
+ return `/${ languageCode } /${ fullPathWithoutDot } ` ;
406
+ }
407
+
408
+ return `/${ fullPathWithoutDot } ` ;
409
+ } ,
410
+ } ,
411
+ slugAsParams : {
412
+ type : "list" ,
413
+ resolve : ( doc ) => {
414
+ const pathWithoutDot = doc . _raw . flattenedPath . replace (
415
+ getLanCode ,
416
+ ""
417
+ ) ;
418
+ return pathWithoutDot . split ( "/" ) ;
335
419
} ,
336
420
} ,
337
421
} ,
@@ -354,13 +438,15 @@ export const Source = defineDocumentType(() => ({
354
438
computedFields : {
355
439
url : {
356
440
type : "string" ,
357
- resolve : ( doc ) => `/${ doc . _raw . flattenedPath . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) } ` ,
441
+ resolve : ( doc ) =>
442
+ `/${ doc . _raw . flattenedPath . split ( "/" ) . slice ( 0 , - 1 ) . join ( "/" ) } ` ,
358
443
} ,
359
444
language : {
360
445
type : "string" ,
361
446
resolve : ( doc ) => {
362
447
const index = doc . _raw . flattenedPath . split ( "/" ) . pop ( ) ;
363
- const lan = index ?. split ( "." ) . length === 2 ? index ?. split ( "." ) [ 1 ] : "en" ;
448
+ const lan =
449
+ index ?. split ( "." ) . length === 2 ? index ?. split ( "." ) [ 1 ] : "en" ;
364
450
return lan ;
365
451
} ,
366
452
} ,
0 commit comments