72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
// Function to find all HTML files recursively
|
|
function findHtmlFiles(dir, fileList = []) {
|
|
const files = fs.readdirSync(dir);
|
|
|
|
files.forEach(file => {
|
|
const filePath = path.join(dir, file);
|
|
const stat = fs.statSync(filePath);
|
|
|
|
if (stat.isDirectory()) {
|
|
findHtmlFiles(filePath, fileList);
|
|
} else if (path.extname(file) === '.html') {
|
|
fileList.push(filePath);
|
|
}
|
|
});
|
|
|
|
return fileList;
|
|
}
|
|
|
|
// Function to get relative path from HTML file to root
|
|
function getRelativePathToRoot(filePath, outDir) {
|
|
const relativePath = path.relative(path.dirname(filePath), outDir);
|
|
return relativePath || '.'; // If empty, use '.' for same directory
|
|
}
|
|
|
|
// Function to inject script tag into HTML files
|
|
function injectScriptTag(filePath, outDir) {
|
|
let html = fs.readFileSync(filePath, 'utf8');
|
|
|
|
// Skip if script is already injected
|
|
if (html.includes('path-fix.js')) {
|
|
return;
|
|
}
|
|
|
|
// Find the closing head tag
|
|
const headCloseIndex = html.indexOf('</head>');
|
|
|
|
if (headCloseIndex !== -1) {
|
|
// Get relative path from this HTML file to the root
|
|
const relativePath = getRelativePathToRoot(filePath, outDir);
|
|
|
|
// Inject the script tag before the closing head tag
|
|
const scriptTag = `<script src="${relativePath}/path-fix.js"></script>`;
|
|
const newHtml = html.slice(0, headCloseIndex) + scriptTag + html.slice(headCloseIndex);
|
|
|
|
// Write the modified HTML back to the file
|
|
fs.writeFileSync(filePath, newHtml, 'utf8');
|
|
console.log(`Injected script into ${filePath}`);
|
|
} else {
|
|
console.log(`Could not find </head> in ${filePath}`);
|
|
}
|
|
}
|
|
|
|
// Main function
|
|
function main() {
|
|
const outDir = path.join(__dirname, '..', 'out');
|
|
const htmlFiles = findHtmlFiles(outDir);
|
|
|
|
console.log(`Found ${htmlFiles.length} HTML files`);
|
|
|
|
htmlFiles.forEach(file => {
|
|
injectScriptTag(file, outDir);
|
|
});
|
|
|
|
console.log('Post-export processing complete!');
|
|
}
|
|
|
|
// Run the main function
|
|
main();
|