22
33const fs = require ( 'fs' )
44const path = require ( 'path' )
5+ const pagination = require ( 'hexo-pagination' )
56
67function normalizeSource ( sourcePath ) {
78 if ( ! sourcePath ) return ''
@@ -88,6 +89,7 @@ function traverseNodes(node, callback) {
8889hexo . extend . generator . register ( 'knowledge-base' , function ( locals ) {
8990 const rootNode = createNode ( '知识库' , [ ] )
9091 const postsDir = path . join ( hexo . source_dir , '_posts' )
92+ const perPage = typeof hexo . config . per_page === 'number' ? hexo . config . per_page : 10
9193
9294 scanDirectories ( rootNode , postsDir , [ ] )
9395
@@ -103,22 +105,21 @@ hexo.extend.generator.register('knowledge-base', function (locals) {
103105
104106 sortNodeTree ( rootNode )
105107
106- const routes = [ ]
107-
108108 const rootData = {
109109 title : '知识库' ,
110110 knowledge : {
111111 name : rootNode . name ,
112112 path : rootNode . path ,
113113 url : 'knowledge/' ,
114114 children : rootNode . childrenList . map ( serializeNode ) ,
115- posts : rootNode . postsList
115+ posts : [ ]
116116 }
117117 }
118118
119- routes . push ( {
120- path : 'knowledge/index.html' ,
119+ const routes = pagination ( 'knowledge/' , rootNode . postsList , {
120+ perPage ,
121121 layout : [ 'knowledge-index' , 'page' ] ,
122+ format : 'page/%d/' ,
122123 data : rootData
123124 } )
124125
@@ -130,15 +131,18 @@ hexo.extend.generator.register('knowledge-base', function (locals) {
130131 path : node . path ,
131132 url : `knowledge/${ node . path } /` ,
132133 children : node . childrenList . map ( serializeNode ) ,
133- posts : node . postsList
134+ posts : [ ]
134135 }
135136 }
136137
137- routes . push ( {
138- path : `knowledge/ ${ node . path } /index.html` ,
138+ const pages = pagination ( `knowledge/ ${ node . path } /` , node . postsList , {
139+ perPage ,
139140 layout : [ 'knowledge-detail' , 'page' ] ,
141+ format : 'page/%d/' ,
140142 data
141143 } )
144+
145+ routes . push ( ...pages )
142146 } )
143147
144148 return routes
0 commit comments