forked from vtexdocs/help-center-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
834 lines (733 loc) · 24.3 KB
/
index.js
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// retrieve entries from contentful
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const docsFolderPath = path.join(__dirname, 'docs');
const contentful = require("contentful-management");
require("dotenv").config();
const client = contentful.createClient({
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN ?? "",
});
const { replaceQuotes } = require('./docs-utils/replace-quotes'); // Import the fix-callouts function
const { fixCallouts } = require('./docs-utils/fix-callouts'); // Import the fix-callouts function
let fileCount = 0;
let contentTypes = {
authors: [],
tracks: [],
knownIssues: [],
tutorials: [],
trackArticles: [],
trackTopics: [],
faqs: [],
announcements: [],
categories: [],
subcategories: [],
adminV4docs: [], // deprecated
topBar: [], // deprecated
topQuestions: [], // deprecated
concepts: [], // deprecated
businessGuides: [], // deprecated
unknownContentTypes: [],
};
// Function to delete all markdown files in the 'docs' folder and its subfolders
async function deleteMarkdownFiles(folderPath, isTopLevel = true) {
if (isTopLevel) {
console.log("Deleting markdown files and folders...");
}
async function processFiles(files) {
const fileDeletions = files.map(async file => {
const filePath = path.join(folderPath, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory()) {
// Recursively delete markdown files in subdirectories
await deleteMarkdownFiles(filePath, false);
await fs.rmdir(filePath); // Delete folder after processing
// console.log(`Deleted folder: ${filePath}`);
} else if (file.endsWith('.md')) {
// Delete markdown files
await fs.unlink(filePath);
// console.log(`Deleted: ${filePath}`);
}
});
await Promise.all(fileDeletions);
}
try {
const files = await fs.readdir(folderPath);
await processFiles(files);
if (isTopLevel) {
console.log("All markdown files and folders in the docs folder have been deleted.");
}
} catch (err) {
console.error(`Error during deletion: ${err.message}`);
}
}
let categories = [];
let subcategories = [];
async function getCategories() {
try {
const space = await client.getSpace("alneenqid6w5");
const environment = await space.getEnvironment("master");
let skip = 0;
let limit = 100;
let totalCount = 0;
do {
const response = await environment.getEntries({ skip, limit });
const entries = response;
totalCount = response.total;
for (let j = 0; j < entries.items.length ; j++) {
let entry = entries.items[j];
let contentType = entry.sys.contentType.sys.id;
if (contentType === "category") {
let categoryEN = entry.fields.title?.en || "Untitled category";
let categoryPT = entry.fields.title?.pt || "Untitled category";
let categoryES = entry.fields.title?.es || "Untitled category";
let categoryId = entry.sys.id;
let categorySubcategories = [];
if ("subcategories" in entry.fields) {
for (k = 0; k < entry.fields.subcategories.pt.length; k++) {
categorySubcategories.push(entry.fields.subcategories.pt[k].sys.id);
}
}
categories.push({"categoryEN": categoryEN, "categoryPT": categoryPT, "categoryES": categoryES, "categoryId": categoryId, "categorySubcategories": categorySubcategories})
}
}
skip += limit;
} while (skip < totalCount);
} catch (error) {
console.log("Error occurred while fetching category:", error);
}
// console.log(categories);
return categories;
}
async function getSubcategories() {
try {
const space = await client.getSpace("alneenqid6w5");
const environment = await space.getEnvironment("master");
let skip = 0;
let limit = 100;
let totalCount = 0;
do {
const response = await environment.getEntries({ skip, limit });
const entries = response;
totalCount = response.total;
for (let j = 0; j < entries.items.length ; j++) {
let entry = entries.items[j];
getSubcategoryInfo(entry,categories);
}
skip += limit;
} while (skip < totalCount);
} catch (error) {
console.log("Error occurred while fetching subcategory:", error);
}
// Cross-matching and updating categories
categories.forEach(category => {
const subcategoryNames = subcategories
.filter(sub => sub.categoryId === category.categoryId)
.map(sub => sub.subcategoryEN);
category.subcategoryNames = subcategoryNames;
});
// Cross-matching and updating subcategories
subcategories.forEach(subcategory => {
const category = categories.find(cat => cat.categoryId === subcategory.categoryId);
if (category) {
subcategory.categoryName = category.categoryEN;
}
});
return subcategories;
}
async function getEntries() {
await getCategories();
await getSubcategories();
console.log("Getting entries from Contentful...");
try {
const space = await client.getSpace("alneenqid6w5");
const environment = await space.getEnvironment("master");
let skip = 0;
let limit = 100;
let totalCount = 0;
do {
const response = await environment.getEntries({ skip, limit });
const entries = response;
totalCount = response.total;
for (let j = 0; j < entries.items.length ; j++) {
let entry = entries.items[j];
// console.log(entry.fields)
createMarkdownFile(entry, categories, subcategories);
}
skip += limit;
} while (skip < totalCount);
console.log(
"Total amount of entries retrieved from Contentful:",
totalCount
);
console.log("Authors:", contentTypes.authors.length);
console.log("Tracks:", contentTypes.tracks.length);
console.log("Known Issues:", contentTypes.knownIssues.length);
console.log("Tutorials:", contentTypes.tutorials.length);
console.log("Track Articles:", contentTypes.trackArticles.length);
console.log("Track Topics:", contentTypes.trackTopics.length);
console.log("FAQs:", contentTypes.faqs.length);
console.log("Announcements:", contentTypes.announcements.length);
console.log("Categories:", contentTypes.categories.length);
console.log("Subcategories:", contentTypes.subcategories.length);
console.log("Admin V4 docs (deprecated):", contentTypes.adminV4docs.length);
console.log("TopBar (deprecated):", contentTypes.topBar.length);
console.log("TopQuestions (deprecated):", contentTypes.topQuestions.length);
console.log("Concepts (deprecated):", contentTypes.concepts.length);
console.log(
"Business Guides (deprecated):",
contentTypes.businessGuides.length
);
console.log(
"Unknown Content Type:",
contentTypes.unknownContentTypes.length
);
console.log("Entries that generated files:", fileCount / 3); // fileCount divided by the amount of locales, since it creates one file per locale
console.log(
"Amount of errors (difference between total of entries, entries that generated files and correctly ignored entries):",
totalCount -
fileCount / 3 -
contentTypes.authors.length -
contentTypes.tracks.length -
contentTypes.trackTopics.length -
contentTypes.categories.length -
contentTypes.subcategories.length -
contentTypes.adminV4docs.length -
contentTypes.topBar.length -
contentTypes.topQuestions.length -
contentTypes.concepts.length -
contentTypes.businessGuides.length -
contentTypes.unknownContentTypes.length
);
} catch (error) {
console.log("Error occurred while fetching entry:", error);
}
}
function isDraft(entry) {
return !entry.sys.publishedVersion;
}
function isChanged(entry) {
return (
!!entry.sys.publishedVersion &&
entry.sys.version >= entry.sys.publishedVersion + 2
);
}
function isPublished(entry) {
return (
!!entry.sys.publishedVersion &&
entry.sys.version == entry.sys.publishedVersion + 1
);
}
function isArchived(entry) {
return !!entry.sys.archivedVersion;
}
function getSubcategoryInfo(entry) {
let contentType = entry.sys.contentType.sys.id;
if (contentType === "subcategory") {
let subcategoryEN = entry.fields.title?.en || "Untitled";
let subcategoryPT = entry.fields.title?.pt || "Untitled";
let subcategoryES = entry.fields.title?.es || "Untitled";
let subcategoryId = entry.sys.id;
let categoryId = entry.fields.category?.pt.sys.id || "";
subcategories.push({"subcategoryEN": subcategoryEN, "subcategoryPT": subcategoryPT, "subcategoryES": subcategoryES, "subcategoryId": subcategoryId, "categoryId": categoryId});
return subcategories;
}
}
function createMarkdownFile(entry,categories,subcategories) {
// extract information from each entry
let status;
if (isDraft(entry)) {
status = "DRAFT";
} else if (isChanged(entry)) {
status = "CHANGED";
} else if (isPublished(entry)) {
status = "PUBLISHED";
}
if (isArchived(entry)) {
status = "ARCHIVED";
}
let sys = entry.sys;
let fields = entry.fields;
// console.log(fields);
let entryId = sys.id;
let entryType = sys.type;
let createdAt = sys.createdAt || "";
let updatedAt = sys.updatedAt || "";
let publishedAt = sys?.publishedAt || "";
let firstPublishedAt = sys?.firstPublishedAt || "";
let archivedAt = sys?.archivedAt || "";
let contentType = sys.contentType.sys.id;
// console.log(contentType);
if (contentType === "author") {
contentTypes.authors.push(entry);
return;
}
let trackId = fields.trackId?.pt.sys.id || "";
let trackSlugEN = fields.trackSlug?.en || "untitled-track";
let trackSlugES = fields.trackSlug?.es || "untitled-track";
let trackSlugPT = fields.trackSlug?.pt || "untitled-track";
if (contentType === "track") {
contentTypes.tracks.push(entry);
return;
} else if (
contentType === "adminV4ContextualDocInAdmin" ||
contentType === "av4article" ||
contentType === "av4menuItem" ||
contentType === "av4menuList" ||
contentType === "adminV4LearnMoreList" ||
contentType === "adminV4LearnMoreItem"
) {
contentTypes.adminV4docs.push(entry);
return;
} else if (contentType === "topBar") {
contentTypes.topBar.push(entry);
return;
} else if (contentType === "topQuestions") {
contentTypes.topQuestions.push(entry);
return;
} else if (contentType === "concepts") {
contentTypes.concepts.push(entry);
return;
} else if (contentType === "businessGuides") {
contentTypes.businessGuides.push(entry);
return;
}
let productTeam = fields.xpTeam?.pt || "unknown";
let titleEN = fields.title?.en || "Untitled";
let titleES = fields.title?.es || "Untitled";
let titlePT = fields.title?.pt || "Untitled";
let author = fields.author?.pt[0].sys.id || "";
let tag = fields.tag?.pt || "";
let slugEN = fields.slug?.en || "untitled";
let slugES = fields.slug?.es || "untitled";
let slugPT = fields.slug?.pt || "untitled";
let legacySlugEN = fields.legacySlug?.en || "";
let legacySlugES = fields.legacySlug?.es || "";
let legacySlugPT = fields.legacySlug?.pt || "";
let textEN = fields.text?.en || "";
textEN = textEN.replace('(//', '(https://').replace('[//', '[https://')
let textES = fields.text?.es || "";
textES = textES.replace('(//', '(https://').replace('[//', '[https://')
let textPT = fields.text?.pt || "";
textPT = textPT.replace('(//', '(https://').replace('[//', '[https://')
let subcategoryId = fields.subcategory?.pt.sys.id || "unknown-subcategory";
// Initialize category and subcategory variables
let subcategoryNameEN = "Untitled subcategory";
let categoryNameEN = "Untitled category";
// Find the matching subcategory
let matchedSubcategory = subcategories.find(sub => sub.subcategoryId === subcategoryId);
if (matchedSubcategory) {
// Get the subcategory name
subcategoryNameEN = matchedSubcategory.subcategoryEN;
// Find the corresponding category for the subcategory
let matchedCategory = categories.find(cat => cat.categoryId === matchedSubcategory.categoryId);
if (matchedCategory) {
// Get the category name
categoryNameEN = matchedCategory.categoryEN
}
}
let kiStatusEN = fields.status?.pt[0] || "";
let kiStatusES = fields.status?.pt[0] || "";
let kiStatusPT = fields.status?.pt[0] || "";
let kiSummaryEN = fields.summary?.en || "";
let kiSummaryES = fields.summary?.es || "";
let kiSummaryPT = fields.summary?.pt || "";
let kiSimulationEN = fields.simulation?.en || "";
let kiSimulationES = fields.simulation?.es || "";
let kiSimulationPT = fields.simulation?.pt || "";
let kiWorkaroundEN = fields.workaround?.en || "";
let kiWorkaroundES = fields.workaround?.es || "";
let kiWorkaroundPT = fields.workaround?.pt || "";
let internalReference = fields.internalReference?.pt || "";
let announcementImageID = fields.image?.pt.id || "";
let announcementSynopsisEN = fields.synopsis?.en || "";
let announcementSynopsisES = fields.synopsis?.es || "";
let announcementSynopsisPT = fields.synopsis?.pt || "";
// create .md files in locale folders for each item
let fileNameEN = slugEN.substring(0, 147).replace(/-$/, "") + ".md";
fileNameEN = fileNameEN.replace(/\?/g, ""); // remove all "?" characters and trim if necessary to avoid "filename too long" git error
let fileNameES = slugES.substring(0, 147).replace(/-$/, "") + ".md";
fileNameES = fileNameES.replace(/\?/g, "");
let fileNamePT = slugPT.substring(0, 147).replace(/-$/, "") + ".md";
fileNamePT = fileNamePT.replace(/\?/g, "");
let fileContentEN = "";
let fileContentES = "";
let fileContentPT = "";
let fileFolders = "";
let fileSubFolder = "";
let fileSubcategoryFolder = "";
const locales = ["en", "es", "pt"];
if (contentType === "knownIssue") {
fileContentEN = `---
title: ${titleEN.includes("'") ? `"${titleEN}"` : `'${titleEN}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
tag: ${tag}
slugEN: ${slugEN}
locale: en
kiStatus: ${kiStatusEN}
internalReference: ${internalReference}
---
## Summary
${kiSummaryEN}
## Simulation
${kiSimulationEN}
## Workaround
${kiWorkaroundEN}
`;
fileContentES = `---
title: ${titleES.includes("'") ? `"${titleES}"` : `'${titleES}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
tag: ${tag}
slugEN: ${slugEN}
locale: es
kiStatus: ${kiStatusES}
internalReference: ${internalReference}
---
## Sumario
${kiSummaryES}
## Simulación
${kiSimulationES}
## Workaround
${kiWorkaroundES}
`;
fileContentPT = `---
title: ${titlePT.includes("'") ? `"${titlePT}"` : `'${titlePT}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
tag: ${tag}
slugEN: ${slugEN}
locale: pt
kiStatus: ${kiStatusPT}
internalReference: ${internalReference}
---
## Sumário
${kiSummaryPT}
## Simulação
${kiSimulationPT}
## Workaround
${kiWorkaroundPT}
`;
fileFolders = "known-issues";
fileSubFolder = productTeam;
contentTypes.knownIssues.push(entry);
} else if (contentType === "tutorial") {
fileContentEN = `---
title: ${titleEN.includes("'") ? `"${titleEN}"` : `'${titleEN}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: en
legacySlug: ${legacySlugEN}
subcategoryId: ${subcategoryId}
---
${textEN}
`;
fileContentES = `---
title: ${titleES.includes("'") ? `"${titleES}"` : `'${titleES}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: es
legacySlug: ${legacySlugES}
subcategoryId: ${subcategoryId}
---
${textES}
`;
fileContentPT = `---
title: ${titlePT.includes("'") ? `"${titlePT}"` : `'${titlePT}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: pt
legacySlug: ${legacySlugPT}
subcategoryId: ${subcategoryId}
---
${textPT}
`;
fileFolders = "tutorials"
fileSubFolder = categoryNameEN
fileSubcategoryFolder = subcategoryNameEN
contentTypes.tutorials.push(entry);
} else if (contentType === "trackArticle") {
fileContentEN = `---
title: ${titleEN.includes("'") ? `"${titleEN}"` : `'${titleEN}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
slugEN: ${slugEN}
locale: en
trackId: ${trackId}
trackSlugEN: ${trackSlugEN}
---
${textEN}
`;
fileContentES = `---
title: ${titleES.includes("'") ? `"${titleES}"` : `'${titleES}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
slugEN: ${slugEN}
locale: es
trackId: ${trackId}
trackSlugES: ${trackSlugES}
---
${textES}
`;
fileContentPT = `---
title: ${titlePT.includes("'") ? `"${titlePT}"` : `'${titlePT}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
slugEN: ${slugEN}
locale: pt
trackId: ${trackId}
trackSlugPT: ${trackSlugPT}
---
${textPT}
`;
fileFolders = "tracks";
fileSubFolder = trackSlugEN;
contentTypes.trackArticles.push(entry);
} else if (contentType === "frequentlyAskedQuestion") {
fileContentEN = `---
title: ${titleEN.includes("'") ? `"${titleEN}"` : `'${titleEN}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: en
legacySlug: ${legacySlugEN}
---
${textEN}
`;
fileContentES = `---
title: ${titleES.includes("'") ? `"${titleES}"` : `'${titleES}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: es
legacySlug: ${legacySlugES}
---
${textES}
`;
fileContentPT = `---
title: ${titlePT.includes("'") ? `"${titlePT}"` : `'${titlePT}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
firstPublishedAt: ${firstPublishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${slugEN}
locale: pt
legacySlug: ${legacySlugPT}
---
${textPT}
`;
fileFolders = "faq";
fileSubFolder = productTeam;
contentTypes.faqs.push(entry);
} else if (contentType === "updates") {
fileNameEN = createdAt.split("T")[0] + "-" + fileNameEN;
fileNamePT = createdAt.split("T")[0] + "-" + fileNamePT;
fileNameES = createdAt.split("T")[0] + "-" + fileNameES;
datedSlugEN = createdAt.split("T")[0] + "-" + slugEN;
fileContentEN = `---
title: ${titleEN.includes("'") ? `"${titleEN}"` : `'${titleEN}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${datedSlugEN}
locale: en
legacySlug: ${legacySlugEN}
announcementImageID: '${announcementImageID}'
announcementSynopsisEN: ${announcementSynopsisEN.includes("'") ? `"${announcementSynopsisEN}"` : `'${announcementSynopsisEN}'`}
---
${textEN}
`;
fileContentES = `---
title: ${titleES.includes("'") ? `"${titleES}"` : `'${titleES}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${datedSlugEN}
locale: es
legacySlug: ${legacySlugES}
announcementImageID: '${announcementImageID}'
announcementSynopsisES: ${announcementSynopsisES.includes("'") ? `"${announcementSynopsisES}"` : `'${announcementSynopsisES}'`}
---
${textES}
`;
fileContentPT = `---
title: ${titlePT.includes("'") ? `"${titlePT}"` : `'${titlePT}'`}
id: ${entryId}
status: ${status}
createdAt: ${createdAt}
updatedAt: ${updatedAt}
publishedAt: ${publishedAt}
contentType: ${contentType}
productTeam: ${productTeam}
author: ${author}
slugEN: ${datedSlugEN}
locale: pt
legacySlug: ${legacySlugPT}
announcementImageID: '${announcementImageID}'
announcementSynopsisPT: ${announcementSynopsisPT.includes("'") ? `"${announcementSynopsisPT}"` : `'${announcementSynopsisPT}'`}
---
${textPT}
`;
fileFolders = "announcements";
contentTypes.announcements.push(entry);
} else if (contentType === "category") {
contentTypes.categories.push(entry);
return;
} else if (contentType === "subcategory") {
contentTypes.subcategories.push(entry);
return;
} else if (contentType === "trackTopic") {
contentTypes.trackTopics.push(entry);
return;
} else {
contentTypes.unknownContentTypes.push(entry);
return;
}
let fileContents = [fileContentEN, fileContentES, fileContentPT];
for (let i = 0; i < locales.length; i++) {
// Adjust folder structure for troubleshooting articles
if (categoryNameEN == 'Troubleshooting') {
fileFolders = 'troubleshooting'
fileSubFolder = subcategoryNameEN
fileSubcategoryFolder = ''
}
// Construct the paths
const localeFolder = path.join('./docs', locales[i]);
const baseFolder = path.join(localeFolder, fileFolders);
const subFolder = fileSubFolder ? path.join(baseFolder, fileSubFolder).replace(": ", " - ").trim() : null;
const subcategoryFolder = fileSubcategoryFolder ? path.join(subFolder, fileSubcategoryFolder).replace(": ", " - ").trim() : null;
let filePath = fileSubcategoryFolder ? path.join(subcategoryFolder, fileNameEN).replace(": ", " - ") : fileSubFolder ? path.join(subFolder, fileNameEN).replace(": ", " - ") : path.join(baseFolder, fileNameEN).replace(": ", " - ");
if (locales[i] == "en") {
filePath = fileSubcategoryFolder ? path.join(subcategoryFolder, fileNameEN).replace(": ", " - ") : fileSubFolder ? path.join(subFolder, fileNameEN).replace(": ", " - ") : path.join(baseFolder, fileNameEN).replace(": ", " - ");
} else if (locales[i] == "pt") {
filePath = fileSubcategoryFolder ? path.join(subcategoryFolder, fileNamePT).replace(": ", " - ") : fileSubFolder ? path.join(subFolder, fileNamePT).replace(": ", " - ") : path.join(baseFolder, fileNamePT).replace(": ", " - ");
} else if (locales[i] == "es") {
filePath = fileSubcategoryFolder ? path.join(subcategoryFolder, fileNameES).replace(": ", " - ") : fileSubFolder ? path.join(subFolder, fileNameES).replace(": ", " - ") : path.join(baseFolder, fileNameES).replace(": ", " - ");
}
// Array of folders to create
const foldersToCreate = [localeFolder, baseFolder, subFolder, subcategoryFolder].filter(Boolean);
// Create each folder if it doesn't exist
try {
foldersToCreate.forEach(folder => {
if (!fsSync.existsSync(folder)) {
fsSync.mkdirSync(folder, { recursive: true });
}
});
} catch (err) {
console.error("Error creating folder:", err);
}
// Write the file
fs.writeFile(filePath, fileContents[i], (err) => {
if (err) {
console.error(`Error occurred while creating file ${filePath}:`, err);
} else {
fileCount++;
}
});
}
}
// Main function to run all tasks
async function main() {
try {
await deleteMarkdownFiles(docsFolderPath);
await getEntries();
await replaceQuotes();
await fixCallouts();
} catch (error) {
console.error("Error in main function:", error);
}
}
main();