[opt] wiki sort

This commit is contained in:
xaoxuu 2022-11-19 19:54:37 +08:00
parent e898a4a818
commit 80e6e749cc
4 changed files with 49 additions and 51 deletions

View File

@ -24,7 +24,7 @@ function layoutDiv() {
arr = site.posts.filter( p => p.title && p.title.length > 0)
}
el += '<div class="widget-body related-posts fs14">';
arr = arr.sort((p1, p2) => { p1.order > p2.order })
arr = arr.sort((p1, p2) => p1.order > p2.order ? -1 : 1)
arr.length = item.limit
arr.forEach(post => {
el += '<a class="item title" href="' + url_for(post.link || post.path) + '">';

View File

@ -1,5 +1,5 @@
/**
* doc_tree.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/
* doc_tree.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/
*/
'use strict';
@ -33,129 +33,129 @@ module.exports = ctx => {
const wiki_pages = pages.filter(p => (p.layout === 'wiki') && (p.wiki != undefined) && (p.wiki.length > 0)).map(p => new WikiPage(p))
// 数据整合:项目标签
var all_tag_name = [];
var all_tag_name = []
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
let tags = proj.tags;
let proj = wiki.projects[id]
let tags = proj.tags
if (tags) {
if ((typeof tags == 'string') && tags.constructor == String) {
if (all_tag_name.includes(tags) === false) {
all_tag_name.push(tags);
all_tag_name.push(tags)
}
// 类型转换
tags = [tags];
tags = [tags]
} else if ((typeof tags == 'object') && tags.constructor == Array) {
tags.forEach((tag, i) => {
if (all_tag_name.includes(tag) === false) {
all_tag_name.push(tag);
all_tag_name.push(tag)
}
});
})
}
wiki.projects[id].tags = tags;
wiki.projects[id].tags = tags
}
}
// 补充未分组的项目
const projs = Object.keys(wiki.projects);
const projs = Object.keys(wiki.projects)
wiki_pages.forEach((p, i) => {
if (projs.includes(p.wiki) == false) {
if (wiki.projects[p.wiki] == undefined) {
wiki.projects[p.wiki] = {};
wiki.projects[p.wiki].pages = [];
wiki.projects[p.wiki] = {}
wiki.projects[p.wiki].pages = []
}
var proj = wiki.projects[p.wiki];
var proj = wiki.projects[p.wiki]
if (proj.description == undefined) {
proj.description = p.description;
proj.description = p.description
}
wiki.projects[p.wiki].pages.push(p);
wiki.projects[p.wiki].pages.push(p)
}
});
})
// 补充项目名称和首页
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
proj.id = id;
let proj = wiki.projects[id]
proj.id = id
if (proj.title == undefined || proj.title.length === 0) {
proj.title = id;
proj.title = id
}
if (proj.name == undefined || proj.name.length == 0) {
proj.name = id;
proj.name = id
}
}
// 补充 order
wiki_pages.forEach((p, i) => {
if (p.order == undefined) {
p.order = 0;
p.order = 0
}
});
})
// 数据整合:每个项目的子页面
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
const proj_pages = wiki_pages.filter( p => p.wiki === id ).sort((p1, p2) => { p1.order < p2.order });
let proj = wiki.projects[id]
let proj_pages = wiki_pages.filter(p => p.wiki === id).sort((p1, p2) => p1.order < p2.order ? -1 : 1)
if (!proj_pages || proj_pages.length == 0) {
continue
}
proj.homepage = proj_pages[0]
// 内页按 section 分组
var section_configs = [];
var section_configs = []
if (proj.sections) {
for (let t of Object.keys(proj.sections)) {
let range = proj.sections[t];
let range = proj.sections[t]
if (range.length > 1) {
section_configs.push({
title: t,
from: range[0],
to: range[1]
});
})
}
}
}
var sections = [];
var sections = []
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) {
sections.push({
title: sec.title,
pages: sec_pages
});
})
}
});
})
proj.sections = sections
proj.pages = proj_pages
}
// 全站所有的项目标签
var all_tags = {};
var all_tags = {}
all_tag_name.forEach((tag_name, i) => {
var projs = [];
var projs = []
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
let proj = wiki.projects[id]
if (proj.tags && proj.tags.includes(tag_name) === true && projs.includes(tag_name) === false) {
projs.push(proj.id);
projs.push(proj.id)
}
}
all_tags[tag_name] = {
name: tag_name,
path: (ctx.config.wiki_dir || 'wiki') + '/tags/' + tag_name + '/index.html',
items: projs
};
});
}
})
// 关联相似项目
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
let proj = wiki.projects[id]
if (proj.tags) {
var related = [];
var related = []
proj.tags.forEach((tag_name, i) => {
let tagObj = all_tags[tag_name];
related = related.concat(tagObj.items);
related = [...new Set(related)];
});
proj.related = related;
let tagObj = all_tags[tag_name]
related = related.concat(tagObj.items)
related = [...new Set(related)]
})
proj.related = related
}
}
wiki.all_tags = all_tags;
wiki.all_pages = wiki_pages;
ctx.theme.config.wiki = wiki;
wiki.all_tags = all_tags
wiki.all_pages = wiki_pages
ctx.theme.config.wiki = wiki
};
}

View File

@ -14,14 +14,13 @@
a.more
padding: 0.25rem 0.5rem
border-radius: $border-block
color: var(--text-p1)
.related-posts
width: 100%
max-width: 100%
margin: 1rem 0
.item
line-height: 1.2

View File

@ -73,7 +73,6 @@
.widget-wrap#related .widget-body a
margin-top: 0.5rem
margin-bottom: 1rem
max-width: 'calc(100% - %s * 2)' % var(--gap-l)
.title
font-weight: 700
font-size: $fs-14