2021-07-04 20:21:31 +08:00
|
|
|
/**
|
|
|
|
* doc_tree.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2022-11-19 16:08:50 +08:00
|
|
|
class WikiPage {
|
|
|
|
constructor(page) {
|
|
|
|
this.id = page._id
|
|
|
|
this.title = page.title
|
|
|
|
this.path = page.path
|
|
|
|
this.layout = page.layout
|
|
|
|
this.seo_title = page.seo_title
|
|
|
|
this.wiki = page.wiki
|
|
|
|
this.order = page.order || 0
|
|
|
|
}
|
2021-07-26 22:26:46 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 16:08:50 +08:00
|
|
|
function createWikiObject(ctx) {
|
|
|
|
const wiki = { projects:{} }
|
|
|
|
const { projects } = ctx.locals.get('data')
|
|
|
|
if (projects) {
|
|
|
|
Object.assign(wiki.projects, projects)
|
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 配置
|
2022-11-19 16:08:50 +08:00
|
|
|
var wiki = createWikiObject(ctx)
|
|
|
|
const pages = ctx.locals.get('pages')
|
2021-07-04 20:21:31 +08:00
|
|
|
// wiki 所有页面
|
2022-11-19 16:08:50 +08:00
|
|
|
const wiki_pages = pages.filter(p => (p.layout === 'wiki') && (p.wiki != undefined) && (p.wiki.length > 0)).map(p => new WikiPage(p))
|
2021-07-04 20:21:31 +08:00
|
|
|
|
2021-07-26 22:26:46 +08:00
|
|
|
// 数据整合:项目标签
|
2022-11-19 16:08:50 +08:00
|
|
|
var all_tag_name = [];
|
2021-07-26 22:26:46 +08:00
|
|
|
for (let id of Object.keys(wiki.projects)) {
|
|
|
|
let proj = wiki.projects[id];
|
|
|
|
let tags = proj.tags;
|
|
|
|
if (tags) {
|
|
|
|
if ((typeof tags == 'string') && tags.constructor == String) {
|
2022-11-19 16:08:50 +08:00
|
|
|
if (all_tag_name.includes(tags) === false) {
|
|
|
|
all_tag_name.push(tags);
|
2021-07-26 22:26:46 +08:00
|
|
|
}
|
|
|
|
// 类型转换
|
|
|
|
tags = [tags];
|
|
|
|
} else if ((typeof tags == 'object') && tags.constructor == Array) {
|
|
|
|
tags.forEach((tag, i) => {
|
2022-11-19 16:08:50 +08:00
|
|
|
if (all_tag_name.includes(tag) === false) {
|
|
|
|
all_tag_name.push(tag);
|
2021-07-26 22:26:46 +08:00
|
|
|
}
|
|
|
|
});
|
2021-07-04 20:21:31 +08:00
|
|
|
}
|
2021-07-26 22:26:46 +08:00
|
|
|
wiki.projects[id].tags = tags;
|
2021-07-04 20:21:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 补充未分组的项目
|
|
|
|
const projs = Object.keys(wiki.projects);
|
|
|
|
wiki_pages.forEach((p, i) => {
|
2021-07-26 22:26:46 +08:00
|
|
|
if (projs.includes(p.wiki) == false) {
|
|
|
|
if (wiki.projects[p.wiki] == undefined) {
|
2021-07-04 20:21:31 +08:00
|
|
|
wiki.projects[p.wiki] = {};
|
|
|
|
wiki.projects[p.wiki].pages = [];
|
|
|
|
}
|
|
|
|
var proj = wiki.projects[p.wiki];
|
2021-07-26 22:26:46 +08:00
|
|
|
if (proj.description == undefined) {
|
2021-07-04 20:21:31 +08:00
|
|
|
proj.description = p.description;
|
|
|
|
}
|
|
|
|
wiki.projects[p.wiki].pages.push(p);
|
|
|
|
}
|
|
|
|
});
|
2021-07-26 22:26:46 +08:00
|
|
|
// 补充项目名称和首页
|
|
|
|
for (let id of Object.keys(wiki.projects)) {
|
|
|
|
let proj = wiki.projects[id];
|
|
|
|
proj.id = id;
|
|
|
|
if (proj.title == undefined || proj.title.length === 0) {
|
|
|
|
proj.title = id;
|
2021-07-04 20:21:31 +08:00
|
|
|
}
|
2022-10-27 22:02:47 +08:00
|
|
|
if (proj.name == undefined || proj.name.length == 0) {
|
|
|
|
proj.name = id;
|
|
|
|
}
|
2021-07-04 20:21:31 +08:00
|
|
|
}
|
2021-07-26 22:26:46 +08:00
|
|
|
// 补充 order
|
|
|
|
wiki_pages.forEach((p, i) => {
|
|
|
|
if (p.order == undefined) {
|
|
|
|
p.order = 0;
|
|
|
|
}
|
|
|
|
});
|
2021-07-04 20:21:31 +08:00
|
|
|
|
|
|
|
// 数据整合:每个项目的子页面
|
2021-07-26 22:26:46 +08:00
|
|
|
for (let id of Object.keys(wiki.projects)) {
|
|
|
|
let proj = wiki.projects[id];
|
2022-11-19 16:08:50 +08:00
|
|
|
const proj_pages = wiki_pages.filter( p => p.wiki === id ).sort((p1, p2) => { p1.order < p2.order });
|
|
|
|
if (!proj_pages || proj_pages.length == 0) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
proj.homepage = proj_pages[0]
|
2021-07-25 22:47:18 +08:00
|
|
|
// 内页按 section 分组
|
2022-11-19 16:08:50 +08:00
|
|
|
var section_configs = [];
|
2021-07-25 22:47:18 +08:00
|
|
|
if (proj.sections) {
|
|
|
|
for (let t of Object.keys(proj.sections)) {
|
|
|
|
let range = proj.sections[t];
|
|
|
|
if (range.length > 1) {
|
2022-11-19 16:08:50 +08:00
|
|
|
section_configs.push({
|
2021-07-25 22:47:18 +08:00
|
|
|
title: t,
|
|
|
|
from: range[0],
|
|
|
|
to: range[1]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-26 22:26:46 +08:00
|
|
|
var sections = [];
|
2022-11-19 16:08:50 +08:00
|
|
|
section_configs.forEach((sec, i) => {
|
|
|
|
const sec_pages = proj_pages.filter( p => p.order >= sec.from && p.order <= sec.to )
|
|
|
|
if (sec_pages && sec_pages.length > 0) {
|
2021-07-26 22:26:46 +08:00
|
|
|
sections.push({
|
2021-07-25 22:47:18 +08:00
|
|
|
title: sec.title,
|
2022-11-19 16:08:50 +08:00
|
|
|
pages: sec_pages
|
2021-07-25 22:47:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-11-19 16:08:50 +08:00
|
|
|
proj.sections = sections
|
|
|
|
proj.pages = proj_pages
|
2021-07-04 20:21:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-26 22:26:46 +08:00
|
|
|
// 全站所有的项目标签
|
|
|
|
var all_tags = {};
|
2022-11-19 16:08:50 +08:00
|
|
|
all_tag_name.forEach((tag_name, i) => {
|
2021-07-04 20:21:31 +08:00
|
|
|
var projs = [];
|
2021-07-26 22:26:46 +08:00
|
|
|
for (let id of Object.keys(wiki.projects)) {
|
|
|
|
let proj = wiki.projects[id];
|
2022-11-19 16:08:50 +08:00
|
|
|
if (proj.tags && proj.tags.includes(tag_name) === true && projs.includes(tag_name) === false) {
|
2021-07-26 22:26:46 +08:00
|
|
|
projs.push(proj.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',
|
2021-07-26 22:26:46 +08:00
|
|
|
items: projs
|
2021-07-04 20:21:31 +08:00
|
|
|
};
|
|
|
|
});
|
2021-07-26 22:26:46 +08:00
|
|
|
|
2022-11-19 16:08:50 +08:00
|
|
|
// 关联相似项目
|
2021-07-26 22:26:46 +08:00
|
|
|
for (let id of Object.keys(wiki.projects)) {
|
|
|
|
let proj = wiki.projects[id];
|
|
|
|
if (proj.tags) {
|
|
|
|
var related = [];
|
2022-11-19 16:08:50 +08:00
|
|
|
proj.tags.forEach((tag_name, i) => {
|
|
|
|
let tagObj = all_tags[tag_name];
|
2021-07-26 22:26:46 +08:00
|
|
|
related = related.concat(tagObj.items);
|
|
|
|
related = [...new Set(related)];
|
|
|
|
});
|
|
|
|
proj.related = related;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wiki.all_tags = all_tags;
|
2021-07-04 20:21:31 +08:00
|
|
|
wiki.all_pages = wiki_pages;
|
2022-11-19 16:08:50 +08:00
|
|
|
ctx.theme.config.wiki = wiki;
|
|
|
|
|
2021-07-04 20:21:31 +08:00
|
|
|
};
|