diff --git a/components/ArticleExpirationNotice.js b/components/ArticleExpirationNotice.js new file mode 100644 index 00000000..ce046313 --- /dev/null +++ b/components/ArticleExpirationNotice.js @@ -0,0 +1,73 @@ +import { siteConfig } from '@/lib/config' + +/** + * 文章过期提醒组件 + * 当文章超过指定天数时显示提醒 + * @param {Object} props - 组件属性 + * @param {Object} props.post - 文章数据 + * @param {number} [props.daysThreshold=90] - 过期阈值(天) + * @returns {JSX.Element|null} + */ +export default function ArticleExpirationNotice({ + post, + daysThreshold = siteConfig('ARTICLE_EXPIRATION_DAYS', 90) +}) { + const articleExpirationEnabled = siteConfig( + 'ARTICLE_EXPIRATION_ENABLED', + false + ) + if (!articleExpirationEnabled || !post?.lastEditedDay) { + return null + } + + const postDate = new Date(post.lastEditedDay) + const today = new Date() + const diffTime = Math.abs(today - postDate) + const daysOld = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + const isVisible = daysOld >= daysThreshold + + if (!isVisible) { + return null + } + + // 使用 %%DAYS%% 作为占位符 + const articleExpirationMessage = siteConfig( + 'ARTICLE_EXPIRATION_MESSAGE', + '这篇文章发布于 %%DAYS%% 天前,内容可能已过时,请谨慎参考。' + ) + const articleExpirationMessageParts = + articleExpirationMessage.split('%%DAYS%%') + + // 直接返回 JSX 内容 + return ( +