[opt] wiki performance optimization

This commit is contained in:
xaoxuu 2022-11-19 16:08:50 +08:00
parent 79e380b4ee
commit ff168d322f
16 changed files with 102 additions and 91 deletions

View File

@ -28,7 +28,7 @@ sidebar:
index: welcome, recent, timeline # for home/wiki/categories/tags/archives/404 pages
page: welcome, toc # for pages using 'layout:page'
post: toc, ghrepo, ghissues # for pages using 'layout:post'
wiki: toc, ghrepo, ghissues, related # for pages using 'layout:wiki'
wiki: ghrepo, toc, ghissues, related # for pages using 'layout:wiki'
######## Index ########
post-index: # 近期发布 分类 标签 归档 and ...

View File

@ -13,9 +13,7 @@ function layoutDiv() {
// 项目分类
for (let id of Object.keys(theme.wiki.all_tags)) {
let tag = theme.wiki.all_tags[id];
let projects = tag.items.filter(function(item){
return item.index !== false;
})
let projects = tag.items.filter(item => item.index !== false)
if (projects && projects.length > 0) {
el += '<a';
if (tag.name && tag.name.length > 0 && page.tagName === tag.name) {

View File

@ -13,20 +13,20 @@ function layoutDiv() {
// body
var arr = [];
if (page.menu_id == 'wiki') {
arr = theme.wiki.all_pages.filter(function(p){
arr = theme.wiki.all_pages.filter( p => {
if (p.wiki) {
let proj = theme.wiki.projects[p.wiki];
return proj.index != false;
}
return false;
});
return false
})
} else {
arr = site.posts.filter(function(p){
return p.title && p.title.length > 0;
});
arr = site.posts.filter( p => p.title && p.title.length > 0)
}
el += '<div class="widget-body related-posts fs14">';
arr.sort("updated", -1).limit(item.limit).each(function(post) {
arr = arr.sort((p1, p2) => { p1.order > p2.order })
arr.length = item.limit
arr.forEach(post => {
el += '<a class="item title" href="' + url_for(post.link || post.path) + '">';
el += '<span class="title">'
if (post.layout == 'wiki') {

View File

@ -28,9 +28,7 @@ function layout_posts(partial) {
if (is_home()) {
// pinned posts
if (page.current == 1) {
var pinned = site.posts.filter(function(post){
return post.pin != undefined;
}).sort((config.index_generator && config.index_generator.order_by) || '-date');
var pinned = site.posts.filter(post => post.pin != undefined).sort((config.index_generator && config.index_generator.order_by) || '-date');
pinned.forEach((post, i) => {
el += layout_post_card('post', post, partial(post));
});

View File

@ -4,51 +4,50 @@
'use strict';
function page(page) {
return {
title: page.title,
path: page.path,
wiki: page.wiki
};
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
}
}
module.exports = hexo => {
const data = hexo.locals.get('data');
if (hexo.theme.config.wiki == undefined) {
hexo.theme.config.wiki = {};
}
if (hexo.theme.config.wiki.projects == undefined) {
hexo.theme.config.wiki.projects = {};
}
if (data.projects) {
for (let id of Object.keys(data.projects)) {
hexo.theme.config.wiki.projects[id] = data.projects[id];
}
function createWikiObject(ctx) {
const wiki = { projects:{} }
const { projects } = ctx.locals.get('data')
if (projects) {
Object.assign(wiki.projects, projects)
}
return wiki
}
module.exports = ctx => {
// wiki 配置
var wiki = hexo.theme.config.wiki;
var wiki = createWikiObject(ctx)
const pages = ctx.locals.get('pages')
// wiki 所有页面
const wiki_pages = hexo.locals.get('pages').filter(function (p) {
return (p.layout === 'wiki') && (p.wiki != undefined) && (p.wiki.length > 0);
});
const wiki_pages = pages.filter(p => (p.layout === 'wiki') && (p.wiki != undefined) && (p.wiki.length > 0)).map(p => new WikiPage(p))
// 数据整合:项目标签
var tagNames = [];
var all_tag_name = [];
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);
if (all_tag_name.includes(tags) === false) {
all_tag_name.push(tags);
}
// 类型转换
tags = [tags];
} else if ((typeof tags == 'object') && tags.constructor == Array) {
tags.forEach((tag, i) => {
if (tagNames.includes(tag) === false) {
tagNames.push(tag);
if (all_tag_name.includes(tag) === false) {
all_tag_name.push(tag);
}
});
}
@ -91,19 +90,18 @@ module.exports = hexo => {
// 数据整合:每个项目的子页面
for (let id of Object.keys(wiki.projects)) {
let proj = wiki.projects[id];
proj.pages = wiki_pages.filter(function (p) {
return p.wiki === id;
}).sort('order');
proj.pages.limit(1).forEach((p, i) => {
proj.homepage = p;
});
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]
// 内页按 section 分组
var sectionConfigs = [];
var section_configs = [];
if (proj.sections) {
for (let t of Object.keys(proj.sections)) {
let range = proj.sections[t];
if (range.length > 1) {
sectionConfigs.push({
section_configs.push({
title: t,
from: range[0],
to: range[1]
@ -112,44 +110,43 @@ module.exports = hexo => {
}
}
var sections = [];
sectionConfigs.forEach((sec, i) => {
const pages = proj.pages.filter(function (p) {
return p.order >= sec.from && p.order <= sec.to;
});
if (pages && pages.length > 0) {
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: pages
pages: sec_pages
});
}
});
proj.sections = sections;
proj.sections = sections
proj.pages = proj_pages
}
// 全站所有的项目标签
var all_tags = {};
tagNames.forEach((tagName, i) => {
all_tag_name.forEach((tag_name, i) => {
var projs = [];
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) {
if (proj.tags && proj.tags.includes(tag_name) === true && projs.includes(tag_name) === false) {
projs.push(proj.id);
}
}
all_tags[tagName] = {
name: tagName,
path: (hexo.config.wiki_dir || 'wiki') + '/tags/' + tagName + '/index.html',
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];
if (proj.tags) {
var related = [];
proj.tags.forEach((tagName, i) => {
let tagObj = all_tags[tagName];
proj.tags.forEach((tag_name, i) => {
let tagObj = all_tags[tag_name];
related = related.concat(tagObj.items);
related = [...new Set(related)];
});
@ -159,4 +156,6 @@ module.exports = hexo => {
wiki.all_tags = all_tags;
wiki.all_pages = wiki_pages;
ctx.theme.config.wiki = wiki;
};

View File

@ -29,9 +29,7 @@ hexo.extend.helper.register('popular_posts_wrapper', function(args){
function listItem(obj){
var el = '';
el += '<a class="item" href="' + obj.path + '" title="' + obj.title + '">';
var p = posts.filter(function(p) {
return root + p.path == obj.path;
});
var p = posts.filter(p => root + p.path == obj.path)
if (p && p.length > 0) {
p = p.data[0];
}

20
scripts/helpers/utils.js Normal file
View File

@ -0,0 +1,20 @@
/**
* utils v1 | https://github.com/xaoxuu/hexo-theme-stellar/
*/
'use strict';
hexo.extend.helper.register('get_page', function(id) {
const pages = hexo.locals.get('pages');
let page = pages.data.find(element => element._id == id)
if (page && page._id == id) {
return page
} else {
const posts = hexo.locals.get('posts');
let post = posts.data.find(element => element._id == id)
if (post && post._id == id) {
return post
}
}
return null
});

View File

@ -19,9 +19,7 @@ hexo.extend.tag.register('folders', function(args, content) {
el += ' ' + hexo.args.joinTags(args, ['color']).join(' ');
el += '>';
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => {
return item.trim().length > 0;
});
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter(item => item.trim().length > 0)
if (arr.length > 0) {
var nodes = [];
arr.forEach((item, i) => {

View File

@ -8,9 +8,7 @@ var tab_index = 0;
module.exports = ctx => function(args, content) {
var el = '';
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => {
return item.trim().length > 0;
});
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter(item => item.trim().length > 0)
if (arr.length < 1) {
return el;
}

View File

@ -19,9 +19,7 @@ hexo.extend.tag.register('split', function(args, content) {
el += ' ' + hexo.args.joinTags(args, ['bg']).join(' ');
el += '>';
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => {
return item.trim().length > 0;
});
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter(item => item.trim().length > 0)
if (arr.length > 0) {
var nodes = [];
arr.forEach((item, i) => {

View File

@ -49,9 +49,7 @@ function postTimeline(args, content) {
el += '<div class="tag-plugin timeline">';
}
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => {
return item.trim().length > 0;
});
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter(item => item.trim().length > 0)
if (arr.length > 0) {
var nodes = [];
arr.forEach((item, i) => {

View File

@ -50,7 +50,7 @@ article.md.content
.md
article.md
ul:not(:last-child),
ol:not(:last-child)
padding-bottom: .5rem

View File

@ -1,5 +1,5 @@
.widget-wrap#toc
.widget-body+.widget-header
.widget-header
margin-top: 1rem
.widget-wrap.multi#toc .widget-header

View File

@ -9,8 +9,9 @@
z-index: 1
line-height: 1.2
.widget-wrap
margin: 1rem var(--gap-l) 3rem var(--gap-l)
margin: 1rem var(--gap-l) 2rem var(--gap-l)
.widget-header
margin-top: 3rem
display: flex
justify-content: space-between
align-items: center
@ -50,7 +51,6 @@
margin-top: 0
.l_left .widgets
.widget-wrap#recent .widget-body
display: flex

View File

@ -82,31 +82,31 @@ set_text_black()
set_darkmode_tags()
.tag-plugin[color='red']
--theme-border: mix($c-red, black, 70)
--theme-bg: mix($c-red, black, 20)
--theme-bg: mix($c-red, black, 35)
.tag-plugin[color='orange']
--theme-border: mix($c-orange, black, 70)
--theme-bg: mix($c-orange, black, 20)
--theme-bg: mix($c-orange, black, 35)
.tag-plugin[color='yellow']
--theme-border: mix($c-yellow, black, 70)
--theme-bg: mix($c-yellow, black, 20)
--theme-bg: mix($c-yellow, black, 30)
.tag-plugin[color='green']
--theme-border: mix($c-green, black, 70)
--theme-bg: mix($c-green, black, 20)
--theme-bg: mix($c-green, black, 30)
.tag-plugin[color='cyan']
--theme-border: mix($c-cyan, black, 70)
--theme-bg: mix($c-cyan, black, 20)
--theme-bg: mix($c-cyan, black, 30)
.tag-plugin[color='blue']
--theme-border: mix($c-blue, black, 70)
--theme-bg: mix($c-blue, black, 20)
--theme-bg: mix($c-blue, black, 35)
.tag-plugin[color='purple']
--theme-border: mix($c-purple, black, 70)
--theme-bg: mix($c-purple, black, 20)
--theme-bg: mix($c-purple, black, 40)
.tag-plugin[color='light']
--theme-border: white

View File

@ -4,6 +4,8 @@
grid-template-columns: repeat(auto-fill, "calc((100% - 1 * %s) / 2)" % 16px)
>.cell
margin: 1rem 0
p:first-child>strong:only-child
font-size: $fs-h4
>.cell>
p
line-height: 1.5
@ -29,4 +31,8 @@
background: var(--block)
&[bg='card']>.cell
background: var(--card)
box-shadow: $boxshadow-card
box-shadow: $boxshadow-card
trans2 box-shadow transform
&:hover
transform: translateY(-1px)
box-shadow: $boxshadow-card-float