69 lines
2.4 KiB
JavaScript
69 lines
2.4 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const srcDir = __dirname;
|
|
const distDir = path.join(__dirname, 'dist');
|
|
const imagesDir = path.join(distDir, 'images');
|
|
|
|
// Create dist directories
|
|
if (!fs.existsSync(distDir)) fs.mkdirSync(distDir);
|
|
if (!fs.existsSync(imagesDir)) fs.mkdirSync(imagesDir);
|
|
|
|
// Helper to copy directory recursively
|
|
function copyDir(src, dest) {
|
|
if (!fs.existsSync(src)) return;
|
|
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
|
|
const entries = fs.readdirSync(src, { withFileTypes: true });
|
|
for (let entry of entries) {
|
|
const srcPath = path.join(src, entry.name);
|
|
const destPath = path.join(dest, entry.name);
|
|
if (entry.isDirectory()) {
|
|
copyDir(srcPath, destPath);
|
|
} else {
|
|
fs.copyFileSync(srcPath, destPath);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Copy css and js
|
|
copyDir(path.join(srcDir, 'css'), path.join(distDir, 'css'));
|
|
if (fs.existsSync(path.join(srcDir, 'js'))) {
|
|
copyDir(path.join(srcDir, 'js'), path.join(distDir, 'js'));
|
|
}
|
|
|
|
// Read index.html
|
|
let html = fs.readFileSync(path.join(srcDir, 'index.html'), 'utf8');
|
|
|
|
// Regex to find images in the bài viết folder
|
|
const imgRegex = /src="bài viết\/Trang chủ - Bệnh Viện Đa Khoa Quốc Tế S\.I\.S Cần Thơ_files\/([^"]+)"/g;
|
|
let match;
|
|
const imageMap = {};
|
|
|
|
while ((match = imgRegex.exec(html)) !== null) {
|
|
const originalName = match[1];
|
|
// Sanitize filename
|
|
const ext = path.extname(originalName);
|
|
const safeName = originalName.replace(/[^a-zA-Z0-9.-]/g, '_').toLowerCase();
|
|
imageMap[originalName] = safeName;
|
|
}
|
|
|
|
// Copy images and replace in HTML
|
|
for (const [originalName, safeName] of Object.entries(imageMap)) {
|
|
const sourcePath = path.join(srcDir, 'bài viết', 'Trang chủ - Bệnh Viện Đa Khoa Quốc Tế S.I.S Cần Thơ_files', originalName);
|
|
const destPath = path.join(imagesDir, safeName);
|
|
|
|
if (fs.existsSync(sourcePath)) {
|
|
fs.copyFileSync(sourcePath, destPath);
|
|
// Replace in HTML
|
|
const searchStr = `src="bài viết/Trang chủ - Bệnh Viện Đa Khoa Quốc Tế S.I.S Cần Thơ_files/${originalName}"`;
|
|
const replaceStr = `src="images/${safeName}"`;
|
|
html = html.split(searchStr).join(replaceStr);
|
|
} else {
|
|
console.warn(`File not found: ${sourcePath}`);
|
|
}
|
|
}
|
|
|
|
// Write new index.html to dist
|
|
fs.writeFileSync(path.join(distDir, 'index.html'), html);
|
|
console.log('Static build successful. Files are in /dist folder.');
|