LastUpdated.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <script setup lang="ts">
  2. import { ref, computed, onMounted, watchEffect } from 'vue'
  3. import { useData } from 'vitepress'
  4. const { theme, page } = useData()
  5. const hasLastUpdated = computed(() => {
  6. const lu = theme.value.lastUpdated
  7. return lu !== undefined && lu !== false && page.value.lastUpdated !== 0
  8. })
  9. const prefix = computed(() => {
  10. const p = theme.value.lastUpdated
  11. return p === true ? 'Last Updated' : p
  12. })
  13. const datetime = ref('')
  14. onMounted(() => {
  15. watchEffect(() => {
  16. // locale string might be different based on end user
  17. // and will lead to potential hydration mismatch if calculated at build time
  18. datetime.value = new Date(page.value.lastUpdated!).toLocaleString('en-US')
  19. })
  20. })
  21. </script>
  22. <template>
  23. <p v-if="hasLastUpdated" class="last-updated">
  24. <span class="prefix">{{ prefix }}:</span>
  25. <span class="datetime">{{ datetime }}</span>
  26. </p>
  27. </template>
  28. <style scoped>
  29. .last-updated {
  30. display: inline-block;
  31. margin: 0;
  32. line-height: 1.4;
  33. font-size: 0.9rem;
  34. color: var(--c-text-light);
  35. }
  36. @media (min-width: 960px) {
  37. .last-updated {
  38. font-size: 1rem;
  39. }
  40. }
  41. .prefix {
  42. display: inline-block;
  43. font-weight: 500;
  44. }
  45. .datetime {
  46. display: inline-block;
  47. margin-left: 6px;
  48. font-weight: 400;
  49. }
  50. </style>