hexo-theme-stellar/scripts/events/lib/topic_tree.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-01-14 14:10:36 +08:00
/**
* topic.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* 用于专栏/专题文章一个专栏类似于 wiki 模块中的一个项目文档
* 区别是
* 1. 按发布日期排序无需手动排序
*
*/
'use strict';
function getTopicTree(ctx) {
var tree = {}
const data = ctx.locals.get('data')
var list = []
for (let key of Object.keys(data)) {
2024-02-28 21:22:42 +08:00
if (key.endsWith('.DS_Store')) {
continue
}
2024-01-14 14:10:36 +08:00
if (key.startsWith('topic/') && key.length > 5) {
let newKey = key.replace('topic/', '')
let obj = data[key]
obj.id = newKey
if (obj.order_by == null) {
obj.order_by = '-date'
}
if (obj.path == null) {
obj.path = `/topic/${newKey}/`
}
obj.pages = []
list.push(obj)
}
}
for (let item of list) {
tree[item.id] = item
}
return tree
}
module.exports = ctx => {
var topic = ctx.locals.get('data').topic || {}
// 专栏结构树
topic.tree = getTopicTree(ctx)
// 索引页显示的专栏列表
if (topic.publish_list == null) {
topic.publish_list = Object.keys(topic.tree)
}
ctx.theme.config.topic = topic
}