From df03a147e1226c13952ffecf0bb329a78cf8acd2 Mon Sep 17 00:00:00 2001 From: ADARSH <64017655+0x0806@users.noreply.github.com> Date: Sat, 5 Jul 2025 21:50:34 +0530 Subject: [PATCH] Create generate-static.js --- generate-static.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 generate-static.js diff --git a/generate-static.js b/generate-static.js new file mode 100644 index 0000000..cbff1c1 --- /dev/null +++ b/generate-static.js @@ -0,0 +1,81 @@ + +const fs = require('fs'); +const path = require('path'); + +function getAllMarkdownFiles(dirPath, arrayOfFiles = []) { + try { + const files = fs.readdirSync(dirPath); + + files.forEach(file => { + const fullPath = path.join(dirPath, file); + + try { + const stat = fs.statSync(fullPath); + + if (stat.isDirectory()) { + // Skip hidden directories and node_modules + if (!file.startsWith('.') && file !== 'node_modules') { + getAllMarkdownFiles(fullPath, arrayOfFiles); + } + } else if (file.endsWith('.md')) { + const relativePath = path.relative('.', fullPath).replace(/\\/g, '/'); + let folderName = path.dirname(relativePath); + + // Clean up folder name + if (folderName === '.') { + folderName = 'Root'; + } else { + folderName = folderName.replace(/\\/g, '/'); + } + + arrayOfFiles.push({ + name: file, + path: fullPath, + relativePath: relativePath, + folder: folderName, + size: stat.size, + type: 'md', + lastModified: stat.mtime.toISOString() + }); + } + } catch (error) { + console.error(`Error processing ${fullPath}:`, error.message); + } + }); + } catch (error) { + console.error(`Error reading directory ${dirPath}:`, error.message); + } + + return arrayOfFiles; +} + +// Generate files.json +const markdownFiles = getAllMarkdownFiles('.'); +fs.writeFileSync('files.json', JSON.stringify(markdownFiles, null, 2)); + +// Generate content files +const contentDir = 'content'; +if (!fs.existsSync(contentDir)) { + fs.mkdirSync(contentDir, { recursive: true }); +} + +markdownFiles.forEach(file => { + try { + const content = fs.readFileSync(file.path, 'utf8'); + // Replace more characters that might cause issues in filenames + const safePath = file.relativePath + .replace(/[\/\\]/g, '__') + .replace(/[\[\]()]/g, '_') // Replace brackets and parentheses + .replace(/[<>:"|?*]/g, '_') // Replace other problematic characters + .replace(/\s+/g, '_') // Replace spaces with underscores + .replace(/_+/g, '_') // Replace multiple underscores with single + .replace('.md', '.txt'); + + console.log(`Converting: ${file.relativePath} -> ${safePath}`); + fs.writeFileSync(path.join(contentDir, safePath), content); + } catch (error) { + console.error(`Error reading ${file.path}:`, error.message); + } +}); + +console.log(`Generated static data for ${markdownFiles.length} files`);