Spaces:
Sleeping

File size: 2,374 Bytes
8baf122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node

const fs = require("fs");
const path = require("path");

function calculateDepth(filePath) {
	// Count the number of directory separators to determine depth
	// Remove the filename and count directories
	const dir = path.dirname(filePath);
	if (dir === "." || dir === "/") {
		return 0; // Root level
	}
	return dir.split(path.sep).filter((part) => part !== "").length;
}

function generateRelativePath(depth) {
	if (depth === 0) {
		return "./"; // Root level uses ./
	}
	return "../".repeat(depth); // Go up {depth} directories
}

function fixPathsInFile(filePath) {
	const content = fs.readFileSync(filePath, "utf8");
	const depth = calculateDepth(filePath);
	const relativePath = generateRelativePath(depth);

	console.log(`Processing ${filePath} (depth: ${depth}, prefix: ${relativePath})`);

	// Replace absolute paths with relative paths
	// /_next/ -> ./_next/ or ../_next/ depending on depth
	const updatedContent = content.replace(/"\/_next\//g, `"${relativePath}_next/`);

	if (content !== updatedContent) {
		fs.writeFileSync(filePath, updatedContent);
		console.log(`  ✓ Fixed paths in ${filePath}`);
	} else {
		console.log(`  - No changes needed in ${filePath}`);
	}
}

function findHtmlFiles(dir) {
	const files = [];

	function traverse(currentDir) {
		const items = fs.readdirSync(currentDir);

		for (const item of items) {
			const fullPath = path.join(currentDir, item);
			const stat = fs.statSync(fullPath);

			if (stat.isDirectory()) {
				traverse(fullPath);
			} else if (item.endsWith(".html")) {
				// Convert to relative path from the out directory
				const relativePath = path.relative("out", fullPath);
				files.push(relativePath);
			}
		}
	}

	traverse(dir);
	return files;
}

// Main execution
const outDir = "out";
if (!fs.existsSync(outDir)) {
	console.error("Output directory not found. Please run next build first.");
	process.exit(1);
}

console.log("Finding HTML files...");
const htmlFiles = findHtmlFiles(outDir);

console.log(`Found ${htmlFiles.length} HTML files`);

for (const file of htmlFiles) {
	const fullPath = path.join(outDir, file);

	// Skip root index.html and 404.html as they already have correct paths
	if (file === "index.html" || file === "404.html") {
		console.log(`Skipping root file: ${file}`);
		continue;
	}

	fixPathsInFile(fullPath);
}

console.log("✅ Path fixing complete!");