fix: set `lastUpdated` time to git commit timestamp

More details on issue #343

close #343
pull/345/head
sabertazimi 4 years ago
parent cfbba80a0a
commit 3d7676816f
No known key found for this signature in database
GPG Key ID: 6A07DD4DEAA65D8F

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted, onUpdated } from 'vue'
import { useData } from 'vitepress' import { useData } from 'vitepress'
const { theme, page } = useData() const { theme, page } = useData()
@ -16,11 +16,16 @@ const prefix = computed(() => {
}) })
const datetime = ref('') const datetime = ref('')
onMounted(() => { onMounted(() => {
// locale string might be different based on end user // locale string might be different based on end user
// and will lead to potential hydration mismatch if calculated at build time // and will lead to potential hydration mismatch if calculated at build time
datetime.value = new Date(page.value.lastUpdated).toLocaleString('en-US') datetime.value = new Date(page.value.lastUpdated).toLocaleString('en-US')
}) })
onUpdated(() => {
datetime.value = new Date(page.value.lastUpdated).toLocaleString('en-US')
})
</script> </script>
<template> <template>

@ -1,5 +1,6 @@
import fs from 'fs' import fs from 'fs'
import path from 'path' import path from 'path'
import { spawnSync } from 'child_process'
import matter from 'gray-matter' import matter from 'gray-matter'
import LRUCache from 'lru-cache' import LRUCache from 'lru-cache'
import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown' import { createMarkdownRenderer, MarkdownOptions } from './markdown/markdown'
@ -91,14 +92,33 @@ export function createMarkdownToVueRenderFn(
} }
} }
/* forked from @vuepress/plugin-last-updated */
const getGitLastUpdatedTimeStamp = (filePath: string) => {
let lastUpdated = 0
try {
lastUpdated =
parseInt(
spawnSync(
'git',
['log', '-1', '--format=%at', path.basename(filePath)],
{ cwd: path.dirname(filePath) }
).stdout.toString('utf-8')
) * 1000
} catch (e) {
/* do not handle for now */
}
return lastUpdated
}
const pageData: PageData = { const pageData: PageData = {
title: inferTitle(frontmatter, content), title: inferTitle(frontmatter, content),
description: inferDescription(frontmatter), description: inferDescription(frontmatter),
frontmatter, frontmatter,
headers: data.headers, headers: data.headers,
relativePath, relativePath,
// TODO use git timestamp? lastUpdated: getGitLastUpdatedTimeStamp(file)
lastUpdated: Math.round(fs.statSync(file).mtimeMs)
} }
const vueSrc = const vueSrc =

Loading…
Cancel
Save