-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6a2265
commit 8fb8acb
Showing
3 changed files
with
43 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<script lang="ts" setup> | ||
import { computed, ref, watch } from 'vue' | ||
import { useFrontmatter } from 'valaxy' | ||
import { useI18n } from 'vue-i18n' | ||
import { differenceInMilliseconds, formatDistanceToNow } from 'date-fns' | ||
const fm = useFrontmatter() | ||
const { t, locale } = useI18n() | ||
const updated = computed(() => fm.value.updated || fm.value.date || new Date()) | ||
const ago = ref('') | ||
watch(locale, () => { | ||
const fromNow = formatDistanceToNow(updated.value, { addSuffix: true }) | ||
ago.value = /^\d/.test(fromNow) ? ` ${fromNow}` : fromNow | ||
}, { immediate: true }) | ||
/** | ||
* when the post is updated more than 180 days ago, show a warning | ||
* default 180 days, you can set `time_warning` in frontmatter to change it | ||
*/ | ||
const time_warning = computed(() => { | ||
const diff = differenceInMilliseconds(new Date(), updated.value) | ||
/** | ||
* if `time_warning` is a number, compare the time difference | ||
* if `time_warning` is a boolean, show warning by flag | ||
*/ | ||
if (typeof fm.value.time_warning === 'number') | ||
return diff > fm.value.time_warning | ||
else | ||
return fm.value.time_warning | ||
}) | ||
</script> | ||
|
||
<template> | ||
<blockquote v-if="time_warning" op="80"> | ||
{{ t('post.time_warning', { ago }) }} | ||
</blockquote> | ||
</template> |