From 8d603de43a1b400243b864a899dd42ea72532a8d Mon Sep 17 00:00:00 2001 From: Diego Marcos Segura Date: Mon, 16 Jun 2025 18:33:07 -0700 Subject: [PATCH] Rename mkdocs assets directory to static on deployment to prevent path collision with assets directory --- package.json | 4 +-- scripts/deploy-site.js | 2 +- scripts/rename-assets-to-static.js | 40 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 scripts/rename-assets-to-static.js diff --git a/package.json b/package.json index daa324f..a55ebfe 100644 --- a/package.json +++ b/package.json @@ -32,9 +32,9 @@ "lint:fix": "biome check . --fix", "lint:summary": "biome check --reporter=summary", "prepare": "npm run build:wasm && lefthook install", - "site:build": "npm run build && npm run site:clean && node scripts/copy-site-files.js && mkdocs build", + "site:build": "npm run build && npm run site:clean && node scripts/copy-site-files.js && mkdocs build && node scripts/rename-assets-to-static.js", "site:clean": "node scripts/clean-site-files.js && node scripts/clean-site.js", - "site:deploy": "node scripts/deploy-site.js", + "site:deploy": "npm run site:build && node scripts/deploy-site.js", "site:serve": "node scripts/serve-site.js site", "start": "npm run dev", "test": "node --no-warnings --loader ts-node/esm --test test/**/*.test.ts" diff --git a/scripts/deploy-site.js b/scripts/deploy-site.js index f6ca7f7..41d1945 100644 --- a/scripts/deploy-site.js +++ b/scripts/deploy-site.js @@ -3,7 +3,7 @@ import fs from "node:fs/promises"; import { rm } from "node:fs/promises"; import path from "node:path"; -const siteRemoteURL = "git@github.com:sparkjs-dev/sparkjs-dev.github.io.git"; +const siteRemoteURL = "git@github.com:sparkjsdev/sparkjsdev.github.io.git"; const siteRepoDir = "site-repo"; const siteBuild = "site"; diff --git a/scripts/rename-assets-to-static.js b/scripts/rename-assets-to-static.js new file mode 100644 index 0000000..47e2d3f --- /dev/null +++ b/scripts/rename-assets-to-static.js @@ -0,0 +1,40 @@ +import { execSync } from "node:child_process"; +import { + readFileSync, + readdirSync, + renameSync, + statSync, + writeFileSync, +} from "node:fs"; +import { extname, join } from "node:path"; + +const siteDirectory = "site"; +const oldAssets = join(siteDirectory, "assets"); +const newAssets = join(siteDirectory, "static"); + +// Rename assets directory +console.log(`Renaming ${oldAssets} → ${newAssets}...`); +renameSync(oldAssets, newAssets); +replaceInHtmlFiles(siteDirectory); + +function replaceInHtmlFiles(dir) { + for (const entry of readdirSync(dir)) { + const fullPath = join(dir, entry); + const stat = statSync(fullPath); + + if (stat.isDirectory()) { + replaceInHtmlFiles(fullPath); + } else if (stat.isFile() && extname(fullPath) === ".html") { + const html = readFileSync(fullPath, "utf-8"); + + // Replace relative/local asset references ONLY (exclude external URLs like https://sparkjs.dev/..) + const updated = html.replace( + /(["'(=])((?:\.\.\/)*|\.\/|\/)assets\//g, + (_, prefix, rel) => `${prefix}${rel}static/`, + ); + if (updated !== html) { + writeFileSync(fullPath, updated); + } + } + } +}