Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
计算机应用开发
前端
Vuepress
提交
46e1992f
V
Vuepress
项目概览
计算机应用开发
/
前端
/
Vuepress
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
Vuepress
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
46e1992f
编写于
6月 02, 2018
作者:
U
ULIVZ
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: init plugin
上级
cd8ee42f
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
191 addition
and
3 deletion
+191
-3
docs/.vuepress/config.js
docs/.vuepress/config.js
+5
-0
lib/plugin-api/hook.js
lib/plugin-api/hook.js
+28
-0
lib/plugin-api/index.js
lib/plugin-api/index.js
+89
-0
lib/plugin-api/util.js
lib/plugin-api/util.js
+34
-0
lib/plugins/vuepress-plugin-last-updated/index.js
lib/plugins/vuepress-plugin-last-updated/index.js
+15
-0
lib/prepare/resolveOptions.js
lib/prepare/resolveOptions.js
+20
-3
未找到文件。
docs/.vuepress/config.js
浏览文件 @
46e1992f
...
...
@@ -25,6 +25,11 @@ module.exports = {
],
serviceWorker
:
true
,
theme
:
'
vue
'
,
plugins
:
[
[
require
(
'
../../lib/plugins/vuepress-plugin-last-updated/index
'
),
]
],
themeConfig
:
{
repo
:
'
vuejs/vuepress
'
,
editLinks
:
true
,
...
...
lib/plugin-api/hook.js
0 → 100644
浏览文件 @
46e1992f
module
.
exports
=
class
Hook
{
constructor
(
name
)
{
this
.
name
=
name
this
.
handlers
=
[]
}
tap
(
symbol
,
handler
)
{
if
(
typeof
handler
!==
'
function
'
)
{
return
}
this
.
handlers
.
push
({
symbol
,
handler
})
return
this
}
remove
(
symbol
)
{
const
index
=
this
.
handlers
.
findIndex
(
item
=>
item
.
symbol
===
symbol
)
if
(
index
!==
-
1
)
{
this
.
handlers
.
splice
(
index
,
1
)
}
return
this
}
async
run
()
{
for
(
const
item
of
this
.
handlers
)
{
await
item
.
handler
.
apply
(
null
,
arguments
)
}
}
}
lib/plugin-api/index.js
0 → 100644
浏览文件 @
46e1992f
const
{
resolvePlugin
,
inferPluginName
}
=
require
(
'
./util
'
)
const
Hook
=
require
(
'
./hook
'
)
module
.
exports
=
class
Plugin
{
constructor
(
pluginConfigs
)
{
this
.
hooks
=
{}
this
.
apis
=
{}
this
.
extendHooks
([
'
ready
'
,
'
compiled
'
,
'
updated
'
,
'
generated
'
])
this
.
extendAPIs
([
'
client
'
,
'
chainWebpack
'
,
'
enhanceDevServer
'
,
'
extendMarkdown
'
,
'
enhanceAppFiles
'
,
'
outFiles
'
,
'
extendPageData
'
])
this
.
resolvePluginConfig
(
pluginConfigs
)
}
extendHooks
(
hooks
)
{
hooks
.
forEach
(
hook
=>
{
this
.
hooks
[
hook
]
=
new
Hook
(
hook
)
})
}
extendAPIs
(
apis
)
{
apis
.
forEach
(
api
=>
{
this
.
apis
[
api
]
=
[]
})
}
resolvePluginConfigs
(
pluginConfigs
)
{
pluginConfigs
.
forEach
(([
name
,
pluginOptions
])
=>
{
let
plugin
=
resolvePlugin
(
name
)
if
(
typeof
plugin
===
'
function
'
)
{
plugin
=
plugin
(
pluginOptions
)
}
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
,
chainWebpack
,
enhanceDevServer
,
extendMarkdown
,
enhanceAppFiles
,
outFiles
,
extendPageData
,
ready
,
compiled
,
updated
,
generated
})
{
console
.
log
(
this
.
hooks
)
this
.
registerHook
(
'
ready
'
,
ready
,
name
)
.
registerHook
(
'
compiled
'
,
compiled
,
name
)
.
registerHook
(
'
updated
'
,
updated
,
name
)
.
registerHook
(
'
generated
'
,
generated
,
name
)
this
.
registerAPI
(
'
client
'
,
client
)
.
registerAPI
(
'
chainWebpack
'
,
chainWebpack
)
.
registerAPI
(
'
enhanceDevServer
'
,
enhanceDevServer
)
.
registerAPI
(
'
extendMarkdown
'
,
extendMarkdown
)
.
registerAPI
(
'
enhanceAppFiles
'
,
enhanceAppFiles
)
.
registerAPI
(
'
outFiles
'
,
outFiles
)
.
registerAPI
(
'
extendPageData
'
,
extendPageData
)
}
}
lib/plugin-api/util.js
0 → 100644
浏览文件 @
46e1992f
const
logger
=
require
(
'
../util/logger
'
)
const
chalk
=
require
(
'
chalk
'
)
exports
.
resolvePlugin
=
function
(
pluginName
)
{
if
(
typeof
pluginName
===
'
function
'
||
typeof
pluginName
===
'
object
'
)
{
return
pluginName
}
if
(
typeof
pluginName
===
'
string
'
)
{
try
{
return
require
(
pluginName
.
startsWith
(
'
vuepress-plugin-
'
)
?
pluginName
:
`vuepress-plugin-
${
pluginName
}
`
)
}
catch
(
err
)
{
console
.
error
(
chalk
.
red
(
logger
.
error
(
`\n[vuepress] Cannot resolve plugin:
${
pluginName
}
\n`
,
false
)))
throw
new
Error
(
err
)
}
}
logger
.
warn
(
`\n[vuepress] Invalid plugin usage:
${
chalk
.
yellow
(
pluginName
)}
\n`
)
}
exports
.
inferPluginName
=
function
(
pluginName
,
pluginConfig
)
{
if
(
pluginConfig
.
name
)
{
return
pluginConfig
.
name
}
if
(
typeof
pluginName
===
'
string
'
)
{
if
(
pluginName
.
startsWith
(
'
vuepress-plugin-
'
))
{
return
pluginName
.
slice
(
16
)
}
return
pluginName
}
// ensure each plugin have a unique name.
return
Date
.
now
().
toString
(
16
)
}
lib/plugins/vuepress-plugin-last-updated/index.js
0 → 100644
浏览文件 @
46e1992f
const
path
=
require
(
'
path
'
)
const
spawn
=
require
(
'
cross-spawn
'
)
module
.
exports
=
options
=>
({
client
:
path
.
resolve
(
__dirname
,
'
client.js
'
),
extendPageData
(
filepath
)
{
return
{
lastModified
:
getGitLastUpdatedTimeStamp
(
filepath
)
}
}
})
function
getGitLastUpdatedTimeStamp
(
filepath
)
{
return
parseInt
(
spawn
.
sync
(
'
git
'
,
[
'
log
'
,
'
-1
'
,
'
--format=%ct
'
,
filepath
]).
stdout
.
toString
(
'
utf-8
'
))
*
1000
}
lib/prepare/resolveOptions.js
浏览文件 @
46e1992f
...
...
@@ -2,6 +2,7 @@ const fs = require('fs-extra')
const
path
=
require
(
'
path
'
)
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
{
...
...
@@ -32,6 +33,14 @@ module.exports = async function resolveOptions (sourceDir) {
})
}
// resolve plugins
const
plugins
=
(
Array
.
isArray
(
siteConfig
.
plugins
)
?
siteConfig
.
plugins
:
[]
)
const
plugin
=
new
Plugin
(
plugins
)
console
.
log
(
plugin
)
// resolve outDir
const
outDir
=
siteConfig
.
dest
?
path
.
resolve
(
siteConfig
.
dest
)
...
...
@@ -99,7 +108,7 @@ module.exports = async function resolveOptions (sourceDir) {
const
isAlgoliaSearch
=
(
themeConfig
.
algolia
||
Object
.
keys
(
siteConfig
.
locales
&&
themeConfig
.
locales
||
{})
.
some
(
base
=>
themeConfig
.
locales
[
base
].
algolia
)
.
some
(
base
=>
themeConfig
.
locales
[
base
].
algolia
)
)
// resolve markdown
...
...
@@ -112,7 +121,7 @@ module.exports = async function resolveOptions (sourceDir) {
const
shouldResolveLastUpdated
=
(
themeConfig
.
lastUpdated
||
Object
.
keys
(
siteConfig
.
locales
&&
themeConfig
.
locales
||
{})
.
some
(
base
=>
themeConfig
.
locales
[
base
].
lastUpdated
)
.
some
(
base
=>
themeConfig
.
locales
[
base
].
lastUpdated
)
)
// resolve pagesData
...
...
@@ -124,6 +133,13 @@ module.exports = async function resolveOptions (sourceDir) {
path
:
encodePath
(
fileToPath
(
file
))
}
for
(
const
fn
of
plugin
.
apis
.
extendPageData
)
{
const
res
=
await
fn
(
file
)
if
(
typeof
res
===
'
object
'
)
{
Object
.
assign
(
data
,
res
)
}
}
if
(
shouldResolveLastUpdated
)
{
data
.
lastUpdated
=
getGitLastUpdatedTimeStamp
(
filepath
)
}
...
...
@@ -178,7 +194,8 @@ module.exports = async function resolveOptions (sourceDir) {
themeEnhanceAppPath
,
useDefaultTheme
,
isAlgoliaSearch
,
markdown
markdown
,
plugin
}
return
options
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录