提交 50e34673 编写于 作者: U ULIVZ

feat: make lastUpdated as a plugin.

上级 46e1992f
...@@ -27,7 +27,7 @@ module.exports = { ...@@ -27,7 +27,7 @@ module.exports = {
theme: 'vue', theme: 'vue',
plugins: [ plugins: [
[ [
require('../../lib/plugins/vuepress-plugin-last-updated/index'), require('../../lib/plugins/vuepress-plugin-last-updated/index')
] ]
], ],
themeConfig: { themeConfig: {
......
...@@ -20,7 +20,7 @@ module.exports = class Plugin { ...@@ -20,7 +20,7 @@ module.exports = class Plugin {
'outFiles', 'outFiles',
'extendPageData' 'extendPageData'
]) ])
this.resolvePluginConfig(pluginConfigs) this.resolvePluginConfigs(pluginConfigs)
} }
extendHooks (hooks) { extendHooks (hooks) {
...@@ -35,27 +35,32 @@ module.exports = class Plugin { ...@@ -35,27 +35,32 @@ module.exports = class Plugin {
}) })
} }
registerHook (name, hook, symbol) {
if (hook) {
this.hooks[name].tap(symbol, hook)
}
return this
}
registerAPI (name, api) {
if (api) {
this.apis[name].push(api)
}
return this
}
resolvePluginConfigs (pluginConfigs) { resolvePluginConfigs (pluginConfigs) {
pluginConfigs.forEach(([name, pluginOptions]) => { pluginConfigs.forEach(([name, pluginOptions]) => {
let plugin = resolvePlugin(name) let plugin = resolvePlugin(name)
if (typeof plugin === 'function') { if (typeof plugin === 'function') {
plugin = plugin(pluginOptions) plugin = plugin(pluginOptions)
} }
console.log(plugin)
plugin = Object.assign(plugin, { name: inferPluginName(name, plugin) }) plugin = Object.assign(plugin, { name: inferPluginName(name, plugin) })
this.resolvePluginConfig(plugin) this.resolvePluginConfig(plugin)
}) })
} }
registerHook (name, hook, symbol) {
this.hooks[name].tap(symbol, hook)
return this
}
registerAPI (name, api) {
this.apis[name].push(api)
return this
}
resolvePluginConfig ({ resolvePluginConfig ({
name, name,
client, client,
......
...@@ -3,9 +3,9 @@ const spawn = require('cross-spawn') ...@@ -3,9 +3,9 @@ const spawn = require('cross-spawn')
module.exports = options => ({ module.exports = options => ({
client: path.resolve(__dirname, 'client.js'), client: path.resolve(__dirname, 'client.js'),
extendPageData (filepath) { extendPageData ({ filepath }) {
return { return {
lastModified: getGitLastUpdatedTimeStamp(filepath) lastUpdated: getGitLastUpdatedTimeStamp(filepath)
} }
} }
}) })
......
...@@ -4,7 +4,7 @@ const globby = require('globby') ...@@ -4,7 +4,7 @@ const globby = require('globby')
const createMarkdown = require('../markdown') const createMarkdown = require('../markdown')
const Plugin = require('../plugin-api') const Plugin = require('../plugin-api')
const loadConfig = require('./loadConfig') const loadConfig = require('./loadConfig')
const { encodePath, fileToPath, sort, getGitLastUpdatedTimeStamp } = require('./util') const { encodePath, fileToPath, sort } = require('./util')
const { const {
inferTitle, inferTitle,
extractHeaders, extractHeaders,
...@@ -117,33 +117,22 @@ module.exports = async function resolveOptions (sourceDir) { ...@@ -117,33 +117,22 @@ module.exports = async function resolveOptions (sourceDir) {
// resolve pageFiles // resolve pageFiles
const pageFiles = sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir })) const pageFiles = sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir }))
// resolve lastUpdated
const shouldResolveLastUpdated = (
themeConfig.lastUpdated ||
Object.keys(siteConfig.locales && themeConfig.locales || {})
.some(base => themeConfig.locales[base].lastUpdated)
)
// resolve pagesData // resolve pagesData
const pagesData = await Promise.all(pageFiles.map(async (file) => { const pagesData = await Promise.all(pageFiles.map(async (relative) => {
const filepath = path.resolve(sourceDir, file) const filepath = path.resolve(sourceDir, relative)
const key = 'v-' + Math.random().toString(16).slice(2) const key = 'v-' + Math.random().toString(16).slice(2)
const data = { const data = {
key, key,
path: encodePath(fileToPath(file)) path: encodePath(fileToPath(relative))
} }
for (const fn of plugin.apis.extendPageData) { for (const fn of plugin.apis.extendPageData) {
const res = await fn(file) const res = await fn({ filepath, relative, key, sourceDir })
if (typeof res === 'object') { if (typeof res === 'object') {
Object.assign(data, res) Object.assign(data, res)
} }
} }
if (shouldResolveLastUpdated) {
data.lastUpdated = getGitLastUpdatedTimeStamp(filepath)
}
// extract yaml frontmatter // extract yaml frontmatter
const content = await fs.readFile(filepath, 'utf-8') const content = await fs.readFile(filepath, 'utf-8')
const frontmatter = parseFrontmatter(content) const frontmatter = parseFrontmatter(content)
......
const path = require('path') const path = require('path')
const spawn = require('cross-spawn')
const fs = require('fs-extra') const fs = require('fs-extra')
const globby = require('globby') const globby = require('globby')
...@@ -74,7 +73,3 @@ exports.sort = function (arr) { ...@@ -74,7 +73,3 @@ exports.sort = function (arr) {
exports.encodePath = function (userpath) { exports.encodePath = function (userpath) {
return userpath.split('/').map(item => encodeURIComponent(item)).join('/') return userpath.split('/').map(item => encodeURIComponent(item)).join('/')
} }
exports.getGitLastUpdatedTimeStamp = function (filepath) {
return parseInt(spawn.sync('git', ['log', '-1', '--format=%ct', filepath]).stdout.toString('utf-8')) * 1000
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册