# @vuepress/plugin-last-updated (opens new window)

last-updated 插件。

如果你使用默认主题,你无需安装本插件,因为 VuePress 的 core 中已经包含此插件,同时,你应该直接使用 themeConfig.lastUpdated 选项。

如果你在你的自定义主题中使用该插件,你将需要自己在主题中完成 lastUpdated 的 UI,你可以使用 $page.lastUpdated 去访问当前页面的时间字符串。

# 使用

module.exports = {
  plugins: ['@vuepress/last-updated']
}

# 选项

# transformer

  • 类型: (timestamp: number, lang: string) => string
  • 默认值: undefined

默认情况下,本插件为每个页面生成一个 13 位的时间戳,你可以传入一个 transformer 将其转换为你想要的任何格式。

例子:

const moment = require('moment');

module.exports = {
  plugins: [
    [
      '@vuepress/last-updated',
      {
        transformer: (timestamp, lang) => {
          // 不要忘了安装 moment
          const moment = require('moment')
          moment.locale(lang)
          return moment(timestamp).fromNow()
        }
      }
    ]
  ]
}

提示

如果你在多语言模式下运行,你还可以使用第二个参数 lang 为不同语言生成时间字符串。

请注意,在 VuePress 中,我们遵循以下规范:W3C > Language tags in HTML and XML (opens new window),因此 zh-CN 使用连字符(-)而不是下划线(_)。 请确保你使用的库遵循此规范,否则请自行转换。

# dateOptions

  • 类型: object
  • 默认值: undefined

你也可以传入一个对象作为选项,以自定义时间戳的输出格式。详细信息请参阅 Date.prototype.toLocaleString() (opens new window) 的选项参数。

module.exports = {
  plugins: [
    [
      '@vuepress/last-updated',
      {
        dateOptions:{
          hour12: false
        }
      }
    ]
  ]
}