mirror of
https://github.com/wahyd4/vuepress.git
synced 2026-08-10 22:06:22 +10:00
Move the transformation to $plugin-last-updated, BTW, expose `lang` as the 2nd argument
28 lines
838 B
JavaScript
28 lines
838 B
JavaScript
const spawn = require('cross-spawn')
|
|
|
|
module.exports = (options = {}, context) => ({
|
|
extendPageData ($page) {
|
|
const { transformer } = options
|
|
const timestamp = getGitLastUpdatedTimeStamp($page._filePath)
|
|
const $lang = $page._computed.$lang
|
|
if (timestamp) {
|
|
const lastUpdated = typeof transformer === 'function'
|
|
? transformer(timestamp, $lang)
|
|
: defaultTransformer(timestamp, $lang)
|
|
$page.lastUpdated = lastUpdated
|
|
}
|
|
}
|
|
})
|
|
|
|
function defaultTransformer (timestamp, lang) {
|
|
return new Date(timestamp).toLocaleString(lang)
|
|
}
|
|
|
|
function getGitLastUpdatedTimeStamp (filePath) {
|
|
let lastUpdated
|
|
try {
|
|
lastUpdated = parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filePath]).stdout.toString('utf-8')) * 1000
|
|
} catch (e) { /* do not handle for now */ }
|
|
return lastUpdated
|
|
}
|