Skip to content

Commit

Permalink
feat($plugin-last-updated): add dateOptions to options (#2192)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodesOfRa committed Feb 25, 2020
1 parent a9759c0 commit 369c315
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/@vuepress/plugin-last-updated/index.js
Expand Up @@ -3,20 +3,20 @@ const spawn = require('cross-spawn')

module.exports = (options = {}, context) => ({
extendPageData ($page) {
const { transformer } = options
const { transformer, dateOptions } = options
const timestamp = getGitLastUpdatedTimeStamp($page._filePath)
const $lang = $page._computed.$lang
if (timestamp) {
const lastUpdated = typeof transformer === 'function'
? transformer(timestamp, $lang)
: defaultTransformer(timestamp, $lang)
: defaultTransformer(timestamp, $lang, dateOptions)
$page.lastUpdated = lastUpdated
}
}
})

function defaultTransformer (timestamp, lang) {
return new Date(timestamp).toLocaleString(lang)
function defaultTransformer (timestamp, lang, dateOptions) {
return new Date(timestamp).toLocaleString(lang, dateOptions)
}

function getGitLastUpdatedTimeStamp (filePath) {
Expand Down
23 changes: 23 additions & 0 deletions packages/docs/docs/plugin/official/plugin-last-updated.md
Expand Up @@ -55,3 +55,26 @@ If you are running in [i18n](../../guide/i18n.md) mode, you can also use the sec

Note that in VuePress, we follow this spec: [W3C > Language tags in HTML and XML](https://en.wikipedia.org/wiki/Language_localisation), so `en-US` uses hyphens (`-`) instead of underscores (`_`). Please make sure that the library you are using follows this spec, otherwise please convert it yourself.
:::

### dateOptions

- Type: `object`
- Default: `undefined`

You can also pass in an options object to customize the timestamp output. For more properties check [`Date.prototype.toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString) options argument

```javascript

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

0 comments on commit 369c315

Please sign in to comment.