Skip to content

Commit 7c3fc72

Browse files
author
vitaly.basaraba
committed
Generate md questions
1 parent 27e396c commit 7c3fc72

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

helpers/generateTextLesson.js

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

45
// Get the input file path from command-line arguments
56
const inputFilePath = process.argv[2];
@@ -20,20 +21,74 @@ try {
2021
process.exit(1);
2122
}
2223

23-
// Convert the array of objects to Markdown format
24-
const markdownContent = data.map(obj => {
24+
// Function to delete a folder and its contents
25+
function clearFolder(folderPath) {
26+
if (fs.existsSync(folderPath)) {
27+
fs.rmSync(folderPath, { recursive: true, force: true });
28+
}
29+
fs.mkdirSync(folderPath, { recursive: true });
30+
}
31+
32+
// Function to create folders and write files
33+
function writeToFile(folderPath, fileName, content) {
34+
if (!fs.existsSync(folderPath)) {
35+
fs.mkdirSync(folderPath, { recursive: true });
36+
}
37+
const filePath = path.join(folderPath, fileName);
38+
fs.writeFileSync(filePath, content, 'utf8');
39+
}
40+
41+
// Generate Markdown content for a question
42+
function generateMarkdown(obj) {
2543
const titleWithLink = obj.link ? `[${obj.title}](${obj.link})` : obj.title;
2644
const tags = obj.level && obj.theme ? `**Tags**: ${obj.level}, ${obj.theme}` : '';
2745
const url = obj.url ? `**URL**: [${obj.url}](${obj.url})` : '';
2846
return `## ${titleWithLink}\n\n${obj.text}\n\n${tags}\n\n${url}\n`;
29-
}).join('\n---\n\n');
47+
}
3048

31-
// Save the Markdown content to a .md file
32-
const outputFilePath = path.resolve(__dirname, 'output.md');
49+
// Clear and recreate the level folder
50+
const levelsFolder = path.resolve(__dirname, `${repo}/level`);
51+
clearFolder(levelsFolder);
52+
data.forEach(obj => {
53+
if (obj.level) {
54+
const levelFolderPath = path.join(levelsFolder, obj.level);
55+
const content = generateMarkdown(obj);
56+
writeToFile(levelFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
57+
}
58+
});
59+
60+
// Clear and recreate the theme folder
61+
const themesFolder = path.resolve(__dirname, `${repo}/theme`);
62+
clearFolder(themesFolder);
63+
data.forEach(obj => {
64+
if (obj.theme) {
65+
const themes = obj.theme.split(',').map(theme => theme.trim());
66+
themes.forEach(theme => {
67+
const themeFolderPath = path.join(themesFolder, theme);
68+
const content = generateMarkdown(obj);
69+
writeToFile(themeFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
70+
});
71+
}
72+
});
73+
74+
// Clear and recreate the video folder
75+
const videoFolder = path.resolve(__dirname, `${repo}/video`);
76+
clearFolder(videoFolder);
77+
data.forEach(obj => {
78+
if (obj.url && obj.url.includes('tiktok')) {
79+
const content = generateMarkdown(obj);
80+
writeToFile(videoFolder, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
81+
}
82+
});
83+
84+
// Save the Markdown content to a README.md file
85+
const outputFilePath = path.resolve(__dirname, './repo/README.md');
3386
try {
34-
fs.writeFileSync(outputFilePath, markdownContent, 'utf8');
87+
fs.writeFileSync(outputFilePath, generateMarkdown, 'utf8');
3588
console.log(`Markdown file has been saved to ${outputFilePath}`);
3689
} catch (err) {
3790
console.error(`Failed to write to file at ${outputFilePath}:`, err.message);
3891
process.exit(1);
3992
}
93+
94+
console.log('Folders and files have been generated successfully.');

helpers/titleLink.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
// Get the input file path from command-line arguments
5+
const inputFilePath = process.argv[2];
6+
if (!inputFilePath) {
7+
console.error('Please provide the path to the input JSON file as an argument.');
8+
process.exit(1);
9+
}
10+
11+
// Resolve the input file path
12+
const dataPath = path.resolve(__dirname, inputFilePath);
13+
14+
// Read and parse the JSON file
15+
let data;
16+
try {
17+
data = require(dataPath);
18+
} catch (err) {
19+
console.error(`Failed to read or parse the file at ${dataPath}:`, err.message);
20+
process.exit(1);
21+
}
22+
23+
// Function to generate a unique link based on the title
24+
function generateLink(title) {
25+
return `#${title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`;
26+
}
27+
28+
// Ensure all objects have a `link` property
29+
data = data.map(obj => {
30+
if (!obj.link) {
31+
obj.link = generateLink(obj.title || 'untitled');
32+
}
33+
return obj;
34+
});
35+
36+
try {
37+
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2), 'utf8');
38+
console.log(`Data has been saved to ${dataPath}`);
39+
} catch (err) {
40+
console.error(`Failed to write to file at ${dataPath}:`, err.message);
41+
process.exit(1);
42+
}

0 commit comments

Comments
 (0)