40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
import fs from "node:fs";
|
|
import path from "node:path";
|
|
import { renderFooter, renderPrimaryNav, renderSidebar } from "./components.mjs";
|
|
|
|
const repoRoot = process.cwd();
|
|
const legacyHtmlRoot = path.join(repoRoot, "archive-source");
|
|
|
|
function walk(dir) {
|
|
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
return walk(fullPath);
|
|
}
|
|
return fullPath.endsWith(".html") ? [fullPath] : [];
|
|
});
|
|
}
|
|
|
|
function toPathname(filePath) {
|
|
const relativePath = path.relative(legacyHtmlRoot, filePath).split(path.sep).join("/");
|
|
return `/${relativePath === "index.html" ? "index.html" : relativePath}`;
|
|
}
|
|
|
|
let updated = 0;
|
|
|
|
for (const filePath of walk(legacyHtmlRoot)) {
|
|
const pathname = toPathname(filePath);
|
|
let html = fs.readFileSync(filePath, "utf8");
|
|
const original = html;
|
|
|
|
html = html.replace(/<nav id="access"[\s\S]*?<\/nav><!-- #access -->/, renderPrimaryNav(pathname));
|
|
html = html.replace(/<div id="secondary" class="widget-area sidey"[\s\S]*?<\/div>\s*(?=<\/section><!-- #container -->)/, renderSidebar());
|
|
html = html.replace(/<footer id="footer"[\s\S]*?<\/footer><!-- #footer -->/, renderFooter());
|
|
|
|
if (html !== original) {
|
|
fs.writeFileSync(filePath, html);
|
|
updated += 1;
|
|
}
|
|
}
|
|
|
|
console.log(`Rendered shared layout components in ${updated} HTML files.`);
|