|
| 1 | +/** |
| 2 | + * Replaces all generated HTML pages with redirect pages pointing to DevHub. |
| 3 | + * Run AFTER `docusaurus build` so that llms.txt and .md files are generated |
| 4 | + * from the original HTML content first. |
| 5 | + * |
| 6 | + * Static files (schemas, llms.txt, .md) are left untouched. |
| 7 | + */ |
| 8 | + |
| 9 | +import { existsSync, readdirSync, statSync, writeFileSync } from "node:fs"; |
| 10 | +import { dirname, join } from "node:path"; |
| 11 | +import { fileURLToPath } from "node:url"; |
| 12 | + |
| 13 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 14 | +const BUILD_DIR = join(__dirname, "../build"); |
| 15 | +const DEVHUB_BASE = "https://www.databricks.com/devhub/docs/appkit/v0"; |
| 16 | + |
| 17 | +function generateRedirectHtml(targetUrl: string): string { |
| 18 | + return `<!DOCTYPE html> |
| 19 | +<html lang="en"> |
| 20 | +<head> |
| 21 | + <meta charset="utf-8" /> |
| 22 | + <title>Redirecting…</title> |
| 23 | + <link rel="canonical" href="${targetUrl}" /> |
| 24 | + <meta http-equiv="refresh" content="0; url=${targetUrl}" /> |
| 25 | +</head> |
| 26 | +<body> |
| 27 | + <p>This page has moved. Redirecting to <a href="${targetUrl}">${targetUrl}</a>…</p> |
| 28 | +</body> |
| 29 | +</html>`; |
| 30 | +} |
| 31 | + |
| 32 | +function findHtmlFiles(dir: string): string[] { |
| 33 | + const results: string[] = []; |
| 34 | + for (const entry of readdirSync(dir)) { |
| 35 | + const fullPath = join(dir, entry); |
| 36 | + if (statSync(fullPath).isDirectory()) { |
| 37 | + results.push(...findHtmlFiles(fullPath)); |
| 38 | + } else if (entry === "index.html") { |
| 39 | + results.push(fullPath); |
| 40 | + } |
| 41 | + } |
| 42 | + return results; |
| 43 | +} |
| 44 | + |
| 45 | +if (!existsSync(BUILD_DIR)) { |
| 46 | + console.error(`Build directory not found: ${BUILD_DIR}`); |
| 47 | + console.error("Run 'docusaurus build' first."); |
| 48 | + process.exit(1); |
| 49 | +} |
| 50 | + |
| 51 | +console.log("Applying DevHub redirects to HTML pages..."); |
| 52 | + |
| 53 | +const htmlFiles = findHtmlFiles(BUILD_DIR); |
| 54 | +let count = 0; |
| 55 | + |
| 56 | +for (const htmlPath of htmlFiles) { |
| 57 | + // Get path relative to build dir, e.g. "/docs/plugins/lakebase" |
| 58 | + const relativePath = htmlPath |
| 59 | + .slice(BUILD_DIR.length) |
| 60 | + .replace(/\/index\.html$/, ""); |
| 61 | + |
| 62 | + // Map /docs/{path} → devhub /{path} (devhub flattens the /docs/ prefix) |
| 63 | + const pathWithoutDocs = relativePath.replace(/^\/docs\/?/, "/"); |
| 64 | + const devhubUrl = `${DEVHUB_BASE}${pathWithoutDocs || "/"}`; |
| 65 | + |
| 66 | + writeFileSync(htmlPath, generateRedirectHtml(devhubUrl)); |
| 67 | + count++; |
| 68 | +} |
| 69 | + |
| 70 | +// Catch-all 404.html for paths not matching a generated route |
| 71 | +const notFoundHtml = `<!DOCTYPE html> |
| 72 | +<html lang="en"> |
| 73 | +<head> |
| 74 | + <meta charset="utf-8" /> |
| 75 | + <title>Redirecting…</title> |
| 76 | + <script> |
| 77 | + (function() { |
| 78 | + var path = window.location.pathname.replace(/^\\/appkit\\/?/, '/'); |
| 79 | + path = path.replace(/^\\/docs\\/?/, '/'); |
| 80 | + var target = "${DEVHUB_BASE}" + path + window.location.search + window.location.hash; |
| 81 | + window.location.replace(target); |
| 82 | + })(); |
| 83 | + </script> |
| 84 | +</head> |
| 85 | +<body> |
| 86 | + <p>Redirecting to <a href="${DEVHUB_BASE}/">AppKit Documentation</a>…</p> |
| 87 | +</body> |
| 88 | +</html>`; |
| 89 | +writeFileSync(join(BUILD_DIR, "404.html"), notFoundHtml); |
| 90 | + |
| 91 | +console.log(`Done! Replaced ${count} HTML page(s) with redirects.`); |
0 commit comments