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

feat: make lastUpdated as a plugin.

上级 46e1992f
......@@ -27,7 +27,7 @@ module.exports = {
theme: 'vue',
plugins: [
[
require('../../lib/plugins/vuepress-plugin-last-updated/index'),
require('../../lib/plugins/vuepress-plugin-last-updated/index')
]
],
themeConfig: {
......
......@@ -20,7 +20,7 @@ module.exports = class Plugin {
'outFiles',
'extendPageData'
])
this.resolvePluginConfig(pluginConfigs)
this.resolvePluginConfigs(pluginConfigs)
}
extendHooks (hooks) {
......@@ -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) {
pluginConfigs.forEach(([name, pluginOptions]) => {
let plugin = resolvePlugin(name)
if (typeof plugin === 'function') {
plugin = plugin(pluginOptions)
}
console.log(plugin)
plugin = Object.assign(plugin, { name: inferPluginName(name, 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 ({
name,
client,
......
......@@ -3,9 +3,9 @@ const spawn = require('cross-spawn')
module.exports = options => ({
client: path.resolve(__dirname, 'client.js'),
extendPageData (filepath) {
extendPageData ({ filepath }) {
return {
lastModified: getGitLastUpdatedTimeStamp(filepath)
lastUpdated: getGitLastUpdatedTimeStamp(filepath)
}
}
})
......
......@@ -4,7 +4,7 @@ const globby = require('globby')
const createMarkdown = require('../markdown')
const Plugin = require('../plugin-api')
const loadConfig = require('./loadConfig')
const { encodePath, fileToPath, sort, getGitLastUpdatedTimeStamp } = require('./util')
const { encodePath, fileToPath, sort } = require('./util')
const {
inferTitle,
extractHeaders,
......@@ -117,33 +117,22 @@ module.exports = async function resolveOptions (sourceDir) {
// resolve pageFiles
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
const pagesData = await Promise.all(pageFiles.map(async (file) => {
const filepath = path.resolve(sourceDir, file)
const pagesData = await Promise.all(pageFiles.map(async (relative) => {
const filepath = path.resolve(sourceDir, relative)
const key = 'v-' + Math.random().toString(16).slice(2)
const data = {
key,
path: encodePath(fileToPath(file))
path: encodePath(fileToPath(relative))
}
for (const fn of plugin.apis.extendPageData) {
const res = await fn(file)
const res = await fn({ filepath, relative, key, sourceDir })
if (typeof res === 'object') {
Object.assign(data, res)
}
}
if (shouldResolveLastUpdated) {
data.lastUpdated = getGitLastUpdatedTimeStamp(filepath)
}
// extract yaml frontmatter
const content = await fs.readFile(filepath, 'utf-8')
const frontmatter = parseFrontmatter(content)
......
const path = require('path')
const spawn = require('cross-spawn')
const fs = require('fs-extra')
const globby = require('globby')
......@@ -74,7 +73,3 @@ exports.sort = function (arr) {
exports.encodePath = function (userpath) {
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.
先完成此消息的编辑!
想要评论请 注册