mirror of
https://github.com/netbirdio/docs.git
synced 2026-05-12 11:59:54 +00:00
Added auto-generated "Updated..." line under H1 (#719)
This commit is contained in:
61
scripts/generate-last-updated.mjs
Normal file
61
scripts/generate-last-updated.mjs
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Generates src/lib/last-updated-routes.js by scanning src/pages for .mdx files
|
||||
* and reading the last commit date for each from git. Used by Layout.jsx to
|
||||
* render an "Updated <date>" line in the right rail.
|
||||
*
|
||||
* Skips src/pages/ipa/resources/ — those are auto-generated from the OpenAPI
|
||||
* spec, so their git date reflects generator runs, not real content edits.
|
||||
*
|
||||
* Run automatically with dev and build.
|
||||
*/
|
||||
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { getGitLastModified } from './git-dates.mjs'
|
||||
|
||||
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/last-updated-routes.mjs')
|
||||
const SKIP_PREFIX = 'ipa/resources'
|
||||
|
||||
function findMdxRoutes(dir, basePath = '') {
|
||||
const entries = fs.readdirSync(dir, { withFileTypes: true })
|
||||
const results = []
|
||||
for (const e of entries) {
|
||||
const rel = basePath ? `${basePath}/${e.name}` : e.name
|
||||
if (e.isDirectory()) {
|
||||
if (rel === SKIP_PREFIX || rel.startsWith(`${SKIP_PREFIX}/`)) continue
|
||||
results.push(...findMdxRoutes(path.join(dir, e.name), rel))
|
||||
} else if (e.name.endsWith('.mdx')) {
|
||||
const filePath = path.join(dir, e.name)
|
||||
const route =
|
||||
e.name === 'index.mdx'
|
||||
? '/' + basePath
|
||||
: '/' + rel.replace(/\.mdx$/, '')
|
||||
results.push({ route, filePath })
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
const entries = findMdxRoutes(PAGES_DIR)
|
||||
.filter((r) => r.route !== '/' && r.route !== '')
|
||||
.sort((a, b) => a.route.localeCompare(b.route))
|
||||
|
||||
const map = {}
|
||||
for (const { route, filePath } of entries) {
|
||||
const date = getGitLastModified(filePath)
|
||||
if (date) map[route] = date
|
||||
}
|
||||
|
||||
const content = `// Auto-generated by scripts/generate-last-updated.mjs – do not edit
|
||||
/** Last commit date (YYYY-MM-DD) keyed by Next.js router.pathname. */
|
||||
export const LAST_UPDATED_BY_ROUTE = ${JSON.stringify(map, null, 2)};
|
||||
`
|
||||
|
||||
fs.mkdirSync(path.dirname(OUT_PATH), { recursive: true })
|
||||
fs.writeFileSync(OUT_PATH, content, 'utf8')
|
||||
console.log('Generated', OUT_PATH, 'with', Object.keys(map).length, 'dated routes')
|
||||
18
scripts/git-dates.mjs
Normal file
18
scripts/git-dates.mjs
Normal file
@@ -0,0 +1,18 @@
|
||||
import { execSync } from 'child_process'
|
||||
|
||||
/**
|
||||
* Get the last modified date for a file from git history.
|
||||
* Returns YYYY-MM-DD or null if the file is not tracked / git is unavailable.
|
||||
*/
|
||||
export function getGitLastModified(filePath) {
|
||||
try {
|
||||
const date = execSync(`git log -1 --format=%cI -- "${filePath}"`, {
|
||||
encoding: 'utf-8',
|
||||
stdio: ['pipe', 'pipe', 'ignore'],
|
||||
}).trim()
|
||||
|
||||
return date ? date.split('T')[0] : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user