Skip to content

Commit

Permalink
Generate md questions
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly.basaraba committed Jan 19, 2025
1 parent 27e396c commit 7c3fc72
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
67 changes: 61 additions & 6 deletions helpers/generateTextLesson.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const repo = '/Users/vitaliisemianchuk/Projects/javascript-questions-pro';

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

// Convert the array of objects to Markdown format
const markdownContent = data.map(obj => {
// Function to delete a folder and its contents
function clearFolder(folderPath) {
if (fs.existsSync(folderPath)) {
fs.rmSync(folderPath, { recursive: true, force: true });
}
fs.mkdirSync(folderPath, { recursive: true });
}

// Function to create folders and write files
function writeToFile(folderPath, fileName, content) {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
const filePath = path.join(folderPath, fileName);
fs.writeFileSync(filePath, content, 'utf8');
}

// Generate Markdown content for a question
function generateMarkdown(obj) {
const titleWithLink = obj.link ? `[${obj.title}](${obj.link})` : obj.title;
const tags = obj.level && obj.theme ? `**Tags**: ${obj.level}, ${obj.theme}` : '';
const url = obj.url ? `**URL**: [${obj.url}](${obj.url})` : '';
return `## ${titleWithLink}\n\n${obj.text}\n\n${tags}\n\n${url}\n`;
}).join('\n---\n\n');
}

// Save the Markdown content to a .md file
const outputFilePath = path.resolve(__dirname, 'output.md');
// Clear and recreate the level folder
const levelsFolder = path.resolve(__dirname, `${repo}/level`);
clearFolder(levelsFolder);
data.forEach(obj => {
if (obj.level) {
const levelFolderPath = path.join(levelsFolder, obj.level);
const content = generateMarkdown(obj);
writeToFile(levelFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
}
});

// Clear and recreate the theme folder
const themesFolder = path.resolve(__dirname, `${repo}/theme`);
clearFolder(themesFolder);
data.forEach(obj => {
if (obj.theme) {
const themes = obj.theme.split(',').map(theme => theme.trim());
themes.forEach(theme => {
const themeFolderPath = path.join(themesFolder, theme);
const content = generateMarkdown(obj);
writeToFile(themeFolderPath, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
});
}
});

// Clear and recreate the video folder
const videoFolder = path.resolve(__dirname, `${repo}/video`);
clearFolder(videoFolder);
data.forEach(obj => {
if (obj.url && obj.url.includes('tiktok')) {
const content = generateMarkdown(obj);
writeToFile(videoFolder, `${obj.title.replace(/[^a-z0-9]+/gi, '_')}.md`, content);
}
});

// Save the Markdown content to a README.md file
const outputFilePath = path.resolve(__dirname, './repo/README.md');
try {
fs.writeFileSync(outputFilePath, markdownContent, 'utf8');
fs.writeFileSync(outputFilePath, generateMarkdown, 'utf8');
console.log(`Markdown file has been saved to ${outputFilePath}`);
} catch (err) {
console.error(`Failed to write to file at ${outputFilePath}:`, err.message);
process.exit(1);
}

console.log('Folders and files have been generated successfully.');
42 changes: 42 additions & 0 deletions helpers/titleLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const fs = require('fs');
const path = require('path');

// Get the input file path from command-line arguments
const inputFilePath = process.argv[2];
if (!inputFilePath) {
console.error('Please provide the path to the input JSON file as an argument.');
process.exit(1);
}

// Resolve the input file path
const dataPath = path.resolve(__dirname, inputFilePath);

// Read and parse the JSON file
let data;
try {
data = require(dataPath);
} catch (err) {
console.error(`Failed to read or parse the file at ${dataPath}:`, err.message);
process.exit(1);
}

// Function to generate a unique link based on the title
function generateLink(title) {
return `#${title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '')}`;
}

// Ensure all objects have a `link` property
data = data.map(obj => {
if (!obj.link) {
obj.link = generateLink(obj.title || 'untitled');
}
return obj;
});

try {
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2), 'utf8');
console.log(`Data has been saved to ${dataPath}`);
} catch (err) {
console.error(`Failed to write to file at ${dataPath}:`, err.message);
process.exit(1);
}

0 comments on commit 7c3fc72

Please sign in to comment.