UNPKG

nuxt-content-git

Version:

Additional module for @nuxt/content that replaces or adds createdAt and updatedAt dates based on the git history.

30 lines 924 B
import { last } from '@dword-design/functions'; import simpleGit from 'simple-git'; export default (options, nuxt) => { options = { createdAtName: 'createdAt', updatedAtName: 'updatedAt', ...nuxt.options.runtimeConfig.nuxtContentGit, ...nuxt.options.nuxtContentGit, ...options }; nuxt.options.runtimeConfig.nuxtContentGit = options; nuxt.hook('content:file:afterParse', async ({ file, content }) => { if (content[options.createdAtName] && content[options.updatedAtName]) { return; } const git = simpleGit(); const log = await git.log({ file: file.path }); if (!content[options.createdAtName]) { content[options.createdAtName] = log.all.length > 0 ? new Date(last(log.all).date) : null; } if (!content[options.updatedAtName]) { content[options.updatedAtName] = log.latest === null ? null : new Date(log.latest.date); } }); };