|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Generates src/lib/edit-on-github-routes.js by scanning src/pages for index.mdx. |
| 4 | + * Used by Layout.jsx so "Edit on GitHub" links point to .../index.mdx for those routes. |
| 5 | + * Run automatically with dev and build. |
| 6 | + */ |
| 7 | + |
| 8 | +import fs from 'fs' |
| 9 | +import path from 'path' |
| 10 | +import { fileURLToPath } from 'url' |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 13 | +const ROOT = path.join(__dirname, '..') |
| 14 | +const PAGES_DIR = path.join(ROOT, 'src/pages') |
| 15 | +const OUT_PATH = path.join(ROOT, 'src/lib/edit-on-github-routes.js') |
| 16 | + |
| 17 | +function findIndexMdxRoutes(dir, basePath = '') { |
| 18 | + const entries = fs.readdirSync(dir, { withFileTypes: true }) |
| 19 | + const routes = [] |
| 20 | + for (const e of entries) { |
| 21 | + const rel = basePath ? `${basePath}/${e.name}` : e.name |
| 22 | + if (e.isDirectory()) { |
| 23 | + routes.push(...findIndexMdxRoutes(path.join(dir, e.name), rel)) |
| 24 | + } else if (e.name === 'index.mdx') { |
| 25 | + routes.push('/' + basePath) |
| 26 | + } |
| 27 | + } |
| 28 | + return routes |
| 29 | +} |
| 30 | + |
| 31 | +const routes = findIndexMdxRoutes(PAGES_DIR) |
| 32 | + .filter((r) => r !== '') // ignore root index if any |
| 33 | + .sort() |
| 34 | + |
| 35 | +const content = `// Auto-generated by scripts/generate-github-routes.mjs – do not edit |
| 36 | +/** Pathnames served by src/pages/.../index.mdx (Edit on GitHub must link to /index.mdx). */ |
| 37 | +export const EDIT_ON_GITHUB_INDEX_ROUTES = new Set(${JSON.stringify(routes)}); |
| 38 | +` |
| 39 | + |
| 40 | +fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true }) |
| 41 | +fs.writeFileSync(OUT_PATH, content, 'utf8') |
| 42 | +console.log('Generated', OUT_PATH, 'with', routes.length, 'index routes') |
0 commit comments