Skip to content

Commit 0725399

Browse files
author
vitaly.basaraba
committed
Featured: generate question
1 parent 719a99f commit 0725399

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

helpers/generateTextLesson.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3-
const repo = '/Users/vitaliisemianchuk/Projects/javascript-questions-pro';
3+
const repo = '../../javascript-questions-pro';
44

55
// Get the input file path from command-line arguments
66
const inputFilePath = process.argv[2];
@@ -38,11 +38,23 @@ function writeToFile(folderPath, fileName, content) {
3838
fs.writeFileSync(filePath, content, 'utf8');
3939
}
4040

41+
// Sanitize names to replace spaces and special characters with underscores and convert to lowercase
42+
function sanitizeName(name) {
43+
return name.replace(/[^a-z0-9]+/gi, '_').toLowerCase();
44+
}
45+
4146
// Generate Markdown content for a question
4247
function generateMarkdown(obj) {
4348
const titleWithLink = obj.link ? `[${obj.title}](${obj.link})` : obj.title;
44-
const tags = obj.level && obj.theme ? `**Tags**: ${obj.level}, ${obj.theme}` : '';
49+
50+
// Create links for level and theme tags
51+
const levelLink = obj.level ? `[${obj.level}](./level/${sanitizeName(obj.level)})` : '';
52+
const themeLinks = obj.theme ? obj.theme.split(',').map(theme => `[${theme.trim()}](./theme/${sanitizeName(theme.trim())})`).join(', ') : '';
53+
54+
const tags = (levelLink || themeLinks) ? `**Tags**: ${levelLink}${levelLink && themeLinks ? ', ' : ''}${themeLinks}` : '';
55+
4556
const url = obj.url ? `**URL**: [${obj.url}](${obj.url})` : '';
57+
4658
return `## ${titleWithLink}\n\n${obj.text}\n\n${tags}\n\n${url}\n`;
4759
}
4860

@@ -53,10 +65,11 @@ clearFolder(levelsFolder);
5365
const uniqueLevels = new Set();
5466
data.forEach(obj => {
5567
if (obj.level) {
56-
uniqueLevels.add(obj.level);
57-
const levelFolderPath = path.join(levelsFolder, obj.level);
68+
const sanitizedLevel = sanitizeName(obj.level); // Ensure level is lowercase
69+
uniqueLevels.add(sanitizedLevel);
70+
const levelFolderPath = path.join(levelsFolder, sanitizedLevel); // Ensure folder path is lowercase
5871
const content = generateMarkdown(obj);
59-
writeToFile(levelFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
72+
writeToFile(levelFolderPath, `${sanitizeName(obj.title)}.md`, content); // Ensure file name is lowercase
6073
}
6174
});
6275

@@ -67,12 +80,12 @@ clearFolder(themesFolder);
6780
const uniqueThemes = new Set();
6881
data.forEach(obj => {
6982
if (obj.theme) {
70-
const themes = obj.theme.split(',').map(theme => theme.trim());
83+
const themes = obj.theme.split(',').map(theme => sanitizeName(theme.trim())); // Ensure theme is lowercase
7184
themes.forEach(theme => {
7285
uniqueThemes.add(theme);
73-
const themeFolderPath = path.join(themesFolder, theme);
86+
const themeFolderPath = path.join(themesFolder, theme); // Ensure folder path is lowercase
7487
const content = generateMarkdown(obj);
75-
writeToFile(themeFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
88+
writeToFile(themeFolderPath, `${sanitizeName(obj.title)}.md`, content); // Ensure file name is lowercase
7689
});
7790
}
7891
});
@@ -86,17 +99,17 @@ data.forEach(obj => {
8699
if (obj.url && obj.url.includes('tiktok')) {
87100
videoQuestions.push(obj);
88101
const content = generateMarkdown(obj);
89-
writeToFile(videoFolder, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
102+
writeToFile(videoFolder, `${sanitizeName(obj.title)}.md`, content); // Ensure file name is lowercase
90103
}
91104
});
92105

93106
// Generate the README header
94107
const totalQuestions = data.length;
95108
const levelsLinks = Array.from(uniqueLevels)
96-
.map(level => `- [${level}](./level/${level})`)
109+
.map(level => `- [${level.replace(/_/g, ' ')}](./level/${level})`)
97110
.join('\n');
98111
const themesLinks = Array.from(uniqueThemes)
99-
.map(theme => `- [${theme}](./theme/${theme})`)
112+
.map(theme => `- [${theme.replace(/_/g, ' ')}](./theme/${theme})`)
100113
.join('\n');
101114
const videosLinks = videoQuestions
102115
.map(q => `- [${q.title}](${q.url})`)
@@ -105,9 +118,16 @@ const videosLinks = videoQuestions
105118
// Convert the array of objects to Markdown format
106119
const markdownContent = data.map(obj => {
107120
const titleWithLink = obj.link ? `[${obj.title}](${obj.link})` : obj.title;
108-
const tags = obj.level && obj.theme ? `**Tags**: ${obj.level}, ${obj.theme}` : '';
121+
122+
// Create links for level and theme tags
123+
const levelLink = obj.level ? `[${obj.level}](./level/${sanitizeName(obj.level)})` : '';
124+
const themeLinks = obj.theme ? obj.theme.split(',').map(theme => `[${theme.trim()}](./theme/${sanitizeName(theme.trim())})`).join(', ') : '';
125+
126+
const tags = (levelLink || themeLinks) ? `**Tags**: ${levelLink}${levelLink && themeLinks ? ', ' : ''}${themeLinks}` : '';
127+
109128
const url = obj.url ? `**URL**: [${obj.url}](${obj.url})` : '';
110-
return `## ${titleWithLink}\n\n${obj.text}\n\n${tags}\n\n${url}\n`;
129+
130+
return `${obj.text}\n\n${tags}\n\n${url}\n`;
111131
}).join('\n---\n\n');
112132

113133
const readmeContent = `

0 commit comments

Comments
 (0)