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

169 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-07-04 20:21:31 +08:00
/**
2022-11-19 19:54:37 +08:00
* doc_tree.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
2021-07-04 20:21:31 +08:00
*/
'use strict';
2022-11-19 16:08:50 +08:00
class WikiPage {
constructor(page) {
this.id = page._id
2023-12-06 13:20:04 +08:00
this.wiki = page.wiki
2022-11-19 16:08:50 +08:00
this.title = page.title
2023-12-06 13:20:04 +08:00
this.seo_title = page.seo_title
2022-11-19 16:08:50 +08:00
this.path = page.path
2023-12-06 13:20:04 +08:00
this.path_key = page.path.replace('.html', '')
2022-11-19 16:08:50 +08:00
this.layout = page.layout
2022-11-19 23:14:41 +08:00
this.updated = page.updated
2022-11-19 16:08:50 +08:00
}
2021-07-26 22:26:46 +08:00
}
2023-12-06 13:20:04 +08:00
function getWikiObject(ctx) {
var wiki = { tree:{} }
const data = ctx.locals.get('data')
var list = []
for (let key of Object.keys(data)) {
if (key.includes('wiki/') && key.length > 5) {
let newKey = key.replace('wiki/', '')
let obj = data[key]
if ((typeof obj.tags == 'string') && obj.tags.constructor == String) {
obj.tags = [obj.tags]
}
if ((typeof obj.toc == 'object') && obj.toc.constructor == Array) {
obj.toc = { '': obj.toc }
}
obj.id = newKey
if (obj.sort == null) {
obj.sort = 0
}
list.push(obj)
}
}
list = list.sort((p1, p2) => p1.sort - p2.sort)
for (let item of list) {
wiki.tree[item.id] = item
2021-07-04 20:21:31 +08:00
}
2022-11-19 16:08:50 +08:00
return wiki
}
2021-07-09 21:17:55 +08:00
2022-11-19 16:08:50 +08:00
module.exports = ctx => {
2021-07-04 20:21:31 +08:00
// wiki 配置
2023-12-06 13:20:04 +08:00
var wiki = getWikiObject(ctx)
2022-11-19 16:08:50 +08:00
const pages = ctx.locals.get('pages')
2021-07-04 20:21:31 +08:00
// wiki 所有页面
2023-12-06 13:20:04 +08:00
const wiki_pages = pages.filter(p => (p.layout === 'wiki')).map(p => new WikiPage(p))
const wiki_list = Object.keys(wiki.tree)
2021-07-04 20:21:31 +08:00
2021-07-26 22:26:46 +08:00
// 数据整合:项目标签
2022-11-19 19:54:37 +08:00
var all_tag_name = []
2023-12-06 13:20:04 +08:00
for (let id of wiki_list) {
let item = wiki.tree[id]
let tags = item.tags
2021-07-26 22:26:46 +08:00
if (tags) {
2023-12-06 13:20:04 +08:00
tags.forEach((tag, i) => {
if (all_tag_name.includes(tag) === false) {
all_tag_name.push(tag)
2021-07-26 22:26:46 +08:00
}
2023-12-06 13:20:04 +08:00
})
wiki.tree[id].tags = tags
2021-07-04 20:21:31 +08:00
}
}
2023-12-06 13:20:04 +08:00
2021-07-26 22:26:46 +08:00
// 补充项目名称和首页
2023-12-06 13:20:04 +08:00
for (let id of wiki_list) {
let item = wiki.tree[id]
item.id = id
if (item.title == undefined || item.title.length === 0) {
item.title = id
2021-07-04 20:21:31 +08:00
}
2023-12-06 13:20:04 +08:00
if (item.name == undefined || item.name.length == 0) {
item.name = id
2022-10-27 22:02:47 +08:00
}
2021-07-04 20:21:31 +08:00
}
2023-12-06 13:20:04 +08:00
2021-07-04 20:21:31 +08:00
// 数据整合:每个项目的子页面
2023-12-06 13:20:04 +08:00
for (let id of wiki_list) {
let item = wiki.tree[id]
let sub_pages = wiki_pages.filter(p => p.wiki === id)
if (!sub_pages || sub_pages.length == 0) {
2022-11-19 16:08:50 +08:00
continue
}
2023-12-06 13:20:04 +08:00
// 首页
// 未特别指定首页时获取TOC第一页作为首页
if (item.homepage == null && item.toc != null) {
for (let id of Object.keys(item.toc)) {
const path_key = item.toc[id]
let hs = sub_pages.filter(p => (p.path_key.endsWith(path_key)))
if (hs.length > 0) {
item.homepage = hs[0]
break
2021-07-25 22:47:18 +08:00
}
}
}
2023-12-06 13:20:04 +08:00
if (item.homepage == null) {
item.homepage = sub_pages[0]
}
item.homepage.is_homepage = true
// 内页分组
2022-11-19 19:54:37 +08:00
var sections = []
2023-12-06 13:20:04 +08:00
var others = sub_pages
if (item.toc) {
for (let title of Object.keys(item.toc)) {
let key_list = item.toc[title]
var arr = []
for (let key of key_list) {
arr = arr.concat(sub_pages.filter(p => p.path_key.endsWith(key)))
others = others.filter(p => p.path_key.endsWith(key) == false)
}
2021-07-26 22:26:46 +08:00
sections.push({
2023-12-06 13:20:04 +08:00
title: title,
pages: arr
2022-11-19 19:54:37 +08:00
})
2021-07-25 22:47:18 +08:00
}
2023-12-06 13:20:04 +08:00
}
if (others.length > 0) {
sections.push({
pages: others
})
}
item.sections = sections
item.pages = sub_pages
2021-07-04 20:21:31 +08:00
}
2021-07-26 22:26:46 +08:00
// 全站所有的项目标签
2022-11-19 19:54:37 +08:00
var all_tags = {}
2022-11-19 16:08:50 +08:00
all_tag_name.forEach((tag_name, i) => {
2023-12-06 13:20:04 +08:00
var subs = []
for (let id of wiki_list) {
let item = wiki.tree[id]
if (item.tags && item.tags.includes(tag_name) === true && subs.includes(tag_name) === false) {
subs.push(item.id)
2021-07-04 20:21:31 +08:00
}
}
2022-11-19 16:08:50 +08:00
all_tags[tag_name] = {
name: tag_name,
path: (ctx.config.wiki_dir || 'wiki') + '/tags/' + tag_name + '/index.html',
2023-12-06 13:20:04 +08:00
items: subs
2022-11-19 19:54:37 +08:00
}
})
2021-07-26 22:26:46 +08:00
2022-11-19 16:08:50 +08:00
// 关联相似项目
2023-12-06 13:20:04 +08:00
for (let id of wiki_list) {
let item = wiki.tree[id]
if (item.tags) {
2022-11-19 19:54:37 +08:00
var related = []
2023-12-06 13:20:04 +08:00
item.tags.forEach((tag_name, i) => {
2022-11-19 19:54:37 +08:00
let tagObj = all_tags[tag_name]
related = related.concat(tagObj.items)
related = [...new Set(related)]
})
2023-12-06 13:20:04 +08:00
item.related = related
2021-07-26 22:26:46 +08:00
}
}
2022-11-19 19:54:37 +08:00
wiki.all_tags = all_tags
wiki.all_pages = wiki_pages
ctx.theme.config.wiki = wiki
2022-11-19 16:08:50 +08:00
2022-11-19 19:54:37 +08:00
}