Repository navigation
Expand file tree
/
Copy pathbuild_content.js
More file actions
98 lines (85 loc) · 2.8 KB
/
Copy pathbuild_content.js
File metadata and controls
98 lines (85 loc) · 2.8 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
96
97
98
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const csvFilePath = path.join(__dirname, 'mega_course_database.csv');
const outputDir = path.join(__dirname, 'output');
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
const csv = fs.readFileSync(csvFilePath, 'utf-8');
const lines = csv.split('\n');
const headers = lines[0].split(',').map(h => h.trim());
function parseCSVLine(line) {
const result = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
result.push(current.trim());
current = '';
} else {
current += char;
}
}
result.push(current.trim());
return result;
}
// Process courses from CSV
const rawCourses = lines.slice(1)
.filter(line => line.trim() !== '')
.map(line => {
const values = parseCSVLine(line);
const course = {};
headers.forEach((header, i) => {
course[header] = values[i] || '';
});
return course;
});
// Prepare courses for JSON output
const courses = rawCourses.map(course => ({
module: course.Module,
title: course['Post Title'],
type: course.Type,
subcategory: course.Subcategory,
url: course['Post URL'],
wistiaId: course.Wistia_ID,
cloudVideoLink: course.Cloud_Video_Link,
slug: slugify(course['Post Title']),
}));
const modules = courses.reduce((acc, course) => {
const moduleName = course.module;
if (!moduleName) return acc;
if (!acc[moduleName]) {
acc[moduleName] = {
name: moduleName,
slug: slugify(moduleName),
subcategories: [],
courses: []
};
}
acc[moduleName].courses.push(course);
if (course.subcategory && course.subcategory !== 'General' && !acc[moduleName].subcategories.includes(course.subcategory)) {
acc[moduleName].subcategories.push(course.subcategory);
}
return acc;
}, {});
const modulesArray = Object.values(modules);
fs.writeFileSync(path.join(outputDir, 'courses.json'), JSON.stringify(courses, null, 2));
fs.writeFileSync(path.join(outputDir, 'modules.json'), JSON.stringify(modulesArray, null, 2));
console.log('✅ Content built successfully!');
console.log(` - ${courses.length} courses`);
console.log(` - ${modulesArray.length} modules`);
function slugify(text) {
if (!text) return '';
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}