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

160 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-07-04 20:21:31 +08:00
/**
* doc_tree.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
*/
'use strict';
2021-07-26 22:26:46 +08:00
function page(page) {
return {
title: page.title,
path: page.path,
wiki: page.wiki
};
}
2021-07-04 20:21:31 +08:00
module.exports = hexo => {
const data = hexo.locals.get('data');
2021-07-26 22:26:46 +08:00
if (hexo.theme.config.wiki == undefined) {
2021-07-09 21:17:55 +08:00
hexo.theme.config.wiki = {};
}
2021-07-26 22:26:46 +08:00
if (hexo.theme.config.wiki.projects == undefined) {
2021-07-09 21:17:55 +08:00
hexo.theme.config.wiki.projects = {};
}
2021-07-04 20:21:31 +08:00
if (data.projects) {
for (let id of Object.keys(data.projects)) {
hexo.theme.config.wiki.projects[id] = data.projects[id];
}
}
2021-07-09 21:17:55 +08:00
2021-07-04 20:21:31 +08:00
// wiki 配置
2021-07-09 21:17:55 +08:00
var wiki = hexo.theme.config.wiki;
2021-07-04 20:21:31 +08:00
// wiki 所有页面
const wiki_pages = hexo.locals.get('pages').filter(function (p) {
2021-07-26 22:26:46 +08:00
return (p.layout === 'wiki') && (p.wiki != undefined) && (p.wiki.length > 0);
2021-07-04 20:21:31 +08:00
});
2021-07-26 22:26:46 +08:00
// 数据整合:项目标签
var tagNames = [];
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) {
if (tagNames.includes(tags) === false) {
tagNames.push(tags);
}
// 类型转换
tags = [tags];
} else if ((typeof tags == 'object') && tags.constructor == Array) {
tags.forEach((tag, i) => {
if (tagNames.includes(tag) === false) {
tagNames.push(tag);
}
});
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
}
}
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];
2021-07-04 20:21:31 +08:00
proj.pages = wiki_pages.filter(function (p) {
2021-07-26 22:26:46 +08:00
return p.wiki === id;
2021-07-04 20:21:31 +08:00
}).sort('order');
proj.pages.limit(1).forEach((p, i) => {
2021-07-26 22:26:46 +08:00
proj.homepage = p;
2021-07-04 20:21:31 +08:00
});
2021-07-25 22:47:18 +08:00
// 内页按 section 分组
2021-07-26 22:26:46 +08:00
var sectionConfigs = [];
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) {
2021-07-26 22:26:46 +08:00
sectionConfigs.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 = [];
sectionConfigs.forEach((sec, i) => {
2021-07-25 22:47:18 +08:00
const pages = proj.pages.filter(function (p) {
return p.order >= sec.from && p.order <= sec.to;
});
if (pages && pages.length > 0) {
2021-07-26 22:26:46 +08:00
sections.push({
2021-07-25 22:47:18 +08:00
title: sec.title,
pages: pages
});
}
});
2021-07-26 22:26:46 +08:00
proj.sections = sections;
2021-07-04 20:21:31 +08:00
}
2021-07-26 22:26:46 +08:00
// 全站所有的项目标签
var all_tags = {};
tagNames.forEach((tagName, 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];
if (proj.tags && proj.tags.includes(tagName) === true && projs.includes(tagName) === false) {
projs.push(proj.id);
2021-07-04 20:21:31 +08:00
}
}
2021-07-26 22:26:46 +08:00
all_tags[tagName] = {
name: tagName,
path: (hexo.config.wiki_dir || 'wiki') + '/tags/' + tagName + '/index.html',
items: projs
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];
if (proj.tags) {
var related = [];
proj.tags.forEach((tagName, i) => {
let tagObj = all_tags[tagName];
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;
};