Skip to content

Commit 7ea0740

Browse files
committed
知识库页面增加分页功能
1 parent 98e6b27 commit 7ea0740

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

scripts/knowledge-base.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs')
44
const path = require('path')
5+
const pagination = require('hexo-pagination')
56

67
function normalizeSource(sourcePath) {
78
if (!sourcePath) return ''
@@ -88,6 +89,7 @@ function traverseNodes(node, callback) {
8889
hexo.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

themes/butterfly/layout/knowledge-detail.pug

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ block content
1212
.article-sort-title 子知识库
1313
+knowledgeCardList(knowledgeChildren)
1414

15-
if page.knowledge && page.knowledge.posts && page.knowledge.posts.length
15+
if page.posts && page.posts.length
1616
.article-sort-title 文章
17-
+knowledgePostList(page.knowledge.posts)
17+
+knowledgePostList(page.posts)
18+
include includes/pagination.pug
1819
else if !knowledgeChildren.length
1920
p 这里暂无内容

themes/butterfly/layout/knowledge-index.pug

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ block content
1212
.article-sort-title 知识库
1313
+knowledgeCardList(knowledgeChildren)
1414

15-
if page.knowledge && page.knowledge.posts && page.knowledge.posts.length
15+
if page.posts && page.posts.length
1616
.article-sort-title 文章
17-
+knowledgePostList(page.knowledge.posts)
17+
+knowledgePostList(page.posts)
18+
include includes/pagination.pug

0 commit comments

Comments
 (0)