hexo-theme-stellar/scripts/generators/notebooks.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-05-13 09:45:38 +08:00
/**
* notebooks v1
*/
const pagination = require('hexo-pagination')
function paginationWithEmpty(base, posts, options={}) {
const { layout, data = {} } = options
if (posts.length === 0) {
base = `${base}/`
return [{
path: base,
layout: layout,
data: {
...data,
base: base,
total: 1,
current: 1,
current_url: base,
posts: posts,
prev: 0,
prev_link: '',
next: 0,
next_link: '',
}
}]
} else {
return pagination(base, posts, options)
}
}
hexo.extend.generator.register('notebooks', function (locals) {
const { site_tree, notebooks } = hexo.theme.config
2024-10-12 18:26:29 +08:00
if (notebooks.tree.length === undefined) {
2024-05-13 09:45:38 +08:00
return []
}
const routes = []
// The index page of all notebooks.
routes.push({
path: site_tree.notebooks.base_dir + '/index.html',
layout: ['notebooks'],
data: {
layout: 'notebooks',
menu_id: site_tree.notebooks.menu_id,
}
})
for (const notebook of Object.values(notebooks.tree)) {
const pages = locals.pages.filter(p => notebook.noteMap.has(p._id)).sort(notebook.order_by)
pages.data.sort((a, b) => notebook.noteMap.get(b._id).pin - notebook.noteMap.get(a._id).pin)
// Note list pages (for every tag) of current notebook.
for (const [_, tag] of notebook.tagTree) {
const notes = pages.filter(p => tag.noteSet.has(p._id))
const slices = paginationWithEmpty(tag.path, notes, {
perPage: notebook.per_page,
layout: ['notes'],
data: {
layout: 'notes',
menu_id: notebook.menu_id,
notebook: notebook.id,
activeTag: tag.id,
}
})
routes.push(...slices)
}
}
return routes
})