Show page contributors from git history, linked to Gitea
This commit is contained in:
@@ -1,8 +1,27 @@
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import { createResolver } from '@nuxt/kit'
|
||||
|
||||
const { resolve } = createResolver(import.meta.url)
|
||||
|
||||
// Contributors per page: read straight from git history rather than an
|
||||
// API, so it needs no token and no network call, but the CI checkout must
|
||||
// fetch full history (not a shallow clone) or every file will only show
|
||||
// its most recent author.
|
||||
function getContributors(absoluteFilePath: string): string[] {
|
||||
try {
|
||||
const output = execFileSync(
|
||||
'git',
|
||||
['log', '--format=%an', '--follow', '--', absoluteFilePath],
|
||||
{ cwd: resolve('.'), encoding: 'utf8' },
|
||||
)
|
||||
return [...new Set(output.split('\n').map(line => line.trim()).filter(Boolean))]
|
||||
}
|
||||
catch {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
// @nuxt/image resolves its file-storage root differently in `nuxi dev`
|
||||
// (needs an absolute path) vs a production build (needs the plain
|
||||
// `public/` string — an absolute path there breaks the SVG content-type
|
||||
@@ -60,6 +79,12 @@ export default defineNuxtConfig({
|
||||
name: 'Français',
|
||||
}],
|
||||
},
|
||||
hooks: {
|
||||
'content:file:afterParse': (ctx: { file: { path?: string }, content: Record<string, unknown> }) => {
|
||||
if (!ctx.file.path?.endsWith('.md')) return
|
||||
ctx.content.contributors = getContributors(ctx.file.path)
|
||||
},
|
||||
},
|
||||
// Permanent (301) redirects from the old site's French URLs (root-level,
|
||||
// no locale prefix) to their new /fr/... equivalents, so bookmarks and
|
||||
// search engine rankings carry over.
|
||||
|
||||
Reference in New Issue
Block a user