Files
netbird-docs/scripts/generate-github-routes.mjs
2026-02-18 20:55:06 +01:00

43 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* Generates src/lib/edit-on-github-routes.js by scanning src/pages for index.mdx.
* Used by Layout.jsx so "Edit on GitHub" links point to .../index.mdx for those routes.
* Run automatically with dev and build.
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const ROOT = path.join(__dirname, '..')
const PAGES_DIR = path.join(ROOT, 'src/pages')
const OUT_PATH = path.join(ROOT, 'src/lib/edit-on-github-routes.js')
function findIndexMdxRoutes(dir, basePath = '') {
const entries = fs.readdirSync(dir, { withFileTypes: true })
const routes = []
for (const e of entries) {
const rel = basePath ? `${basePath}/${e.name}` : e.name
if (e.isDirectory()) {
routes.push(...findIndexMdxRoutes(path.join(dir, e.name), rel))
} else if (e.name === 'index.mdx') {
routes.push('/' + basePath)
}
}
return routes
}
const routes = findIndexMdxRoutes(PAGES_DIR)
.filter((r) => r !== '') // ignore root index if any
.sort()
const content = `// Auto-generated by scripts/generate-github-routes.mjs do not edit
/** Pathnames served by src/pages/.../index.mdx (Edit on GitHub must link to /index.mdx). */
export const EDIT_ON_GITHUB_INDEX_ROUTES = new Set(${JSON.stringify(routes)});
`
fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true })
fs.writeFileSync(OUT_PATH, content, 'utf8')
console.log('Generated', OUT_PATH, 'with', routes.length, 'index routes')