Files
vuepress/packages/@vuepress/plugin-last-updated/index.js
ULIVZ c059faa6fd fix($theme-default): shouldn't transform date string at client side (close: #1035)
Move the transformation to $plugin-last-updated, BTW, expose `lang` as the 2nd argument
2018-11-26 01:31:35 +08:00

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
}