hexo.extend.helper.register("uk_paginator", function(option) {
const options = Object.assign({
base: "/",
pagePath: "page/%d/",
total: 1,
current: 1,
space: "...",
sideSize: 5,
midSize: 10,
mainClasses: [],
itemClasses: [],
}, option);
const pagePathReplaced = options.pagePath.replace('/', '\/').replace('%d', '\\d+');
const regPagePath = new RegExp(pagePathReplaced);
options.base = options.base.replace(regPagePath, '');
let pageStructure = [];
pageStructure.push(`
`);
if (options.total > 0 && options.current > 2) {
pageStructure.push(` `);
} else if (options.total > 0 && options.current === 2) {
pageStructure.push(` `);
} else {
pageStructure.push(` `);
}
let pageGroups = [];
// 生成起始页范围
for (let p = 1; p <= Math.min(options.sideSize, options.total); p++) {
pageGroups.push(p);
}
// 生成中间页范围
if (options.current > options.sideSize && options.current < (options.total - options.sideSize + 1)) {
for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.current - options.midSize / 2); p <= Math.min(options.total, options.current + options.midSize / 2); p++) {
pageGroups.push(p);
}
}
// 生成结束页范围
for (let p = Math.max(pageGroups[pageGroups.length - 1] + 1, options.total - options.sideSize + 1); p <= options.total; p++) {
pageGroups.push(p);
}
pageGroups.forEach(function(page, index) {
if (index > 0 && page - pageGroups[index - 1] > 1) {
pageStructure.push(`- ${options.space}
`);
}
if (page === options.current) {
pageStructure.push(`- ${page}
`)
} else if (page === 1) {
pageStructure.push(`- ${page}
`)
} else {
pageStructure.push(`- ${page}
`)
}
});
if (options.total > 0 && options.current + 1 <= options.total) {
pageStructure.push(` `)
} else {
pageStructure.push(` `)
}
pageStructure.push(`
`);
return pageStructure.join("\n");
});