mirror of
https://github.com/netbirdio/docs.git
synced 2026-09-01 20:51:28 +02:00
Harden last-updated dates: CI guard, SEO metadata, View history link (#904)
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#!/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.
|
||||
* Generates src/lib/edit-on-github-routes.js by scanning src/pages for .mdx files.
|
||||
* Used by Layout.jsx so "Edit on GitHub" links point to .../index.mdx for index
|
||||
* routes, and so GitHub links render only on routes backed by a real MDX file
|
||||
* (e.g. not on the prerendered 404/500 pages, where pathname is /_error).
|
||||
* Run automatically with dev and build.
|
||||
*/
|
||||
|
||||
@@ -14,29 +16,37 @@ 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 = '') {
|
||||
function findMdxRoutes(dir, basePath = '') {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true })
|
||||
const routes = []
|
||||
const indexRoutes = []
|
||||
const allRoutes = []
|
||||
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))
|
||||
const nested = findMdxRoutes(path.join(dir, e.name), rel)
|
||||
indexRoutes.push(...nested.indexRoutes)
|
||||
allRoutes.push(...nested.allRoutes)
|
||||
} else if (e.name === 'index.mdx') {
|
||||
routes.push('/' + basePath)
|
||||
indexRoutes.push('/' + basePath)
|
||||
allRoutes.push('/' + basePath)
|
||||
} else if (e.name.endsWith('.mdx')) {
|
||||
allRoutes.push('/' + rel.replace(/\.mdx$/, ''))
|
||||
}
|
||||
}
|
||||
return routes
|
||||
return { indexRoutes, allRoutes }
|
||||
}
|
||||
|
||||
const routes = findIndexMdxRoutes(PAGES_DIR)
|
||||
.filter((r) => r !== '') // ignore root index if any
|
||||
.sort()
|
||||
const { indexRoutes, allRoutes } = findMdxRoutes(PAGES_DIR)
|
||||
const routes = indexRoutes.filter((r) => r !== '/').sort() // ignore root index if any
|
||||
const mdxRoutes = allRoutes.filter((r) => r !== '/').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)});
|
||||
/** Every pathname backed by an .mdx file under src/pages. GitHub links are hidden elsewhere. */
|
||||
export const MDX_PAGE_ROUTES = new Set(${JSON.stringify(mdxRoutes)});
|
||||
`
|
||||
|
||||
fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true })
|
||||
fs.writeFileSync(OUT_PATH, content, 'utf8')
|
||||
console.log('Generated', OUT_PATH, 'with', routes.length, 'index routes')
|
||||
console.log('Generated', OUT_PATH, 'with', routes.length, 'index routes,', mdxRoutes.length, 'mdx routes')
|
||||
|
||||
Reference in New Issue
Block a user