目录树分组

This commit is contained in:
xaoxuu 2021-07-25 22:47:18 +08:00
parent dbc4eaa135
commit b9f3766833
6 changed files with 122 additions and 75 deletions

View File

@ -3,8 +3,13 @@
if (page.sidebar == undefined) { if (page.sidebar == undefined) {
if (page.layout == 'post' && page.content) { if (page.layout == 'post' && page.content) {
page.sidebar = ['toc', 'repo_info']; page.sidebar = ['toc', 'repo_info'];
} else if (page.layout == 'wiki' && page.content) { } else if (page.layout == 'wiki' && page.content && page.wiki) {
let proj = theme.wiki.projects[page.wiki];
if (proj.sidebar) {
page.sidebar = proj.sidebar;
} else {
page.sidebar = ['toc', 'repo_info', 'wiki_more']; page.sidebar = ['toc', 'repo_info', 'wiki_more'];
}
} else { } else {
page.sidebar = theme.sidebar.widgets.default_layout; page.sidebar = theme.sidebar.widgets.default_layout;
} }

View File

@ -1,4 +1,6 @@
<% <%
let proj = theme.wiki.projects[page.wiki];
function layoutToc() { function layoutToc() {
if (toc(page.content).length > 0) { if (toc(page.content).length > 0) {
return toc(page.content, { return toc(page.content, {
@ -9,79 +11,91 @@ function layoutToc() {
} }
return ''; return '';
} }
function layoutDiv(fallback) {
var t = ''; function layoutTocHeader(title) {
if (page.layout == 'post' && page.content) {
let toc_content = toc(page.content);
if (toc_content && toc_content.length > 0) {
t = page.layout;
}
} else if (page.layout == 'wiki') {
t = page.layout;
} else if (page.sidebar.includes('toc') == true) {
t = page.layout;
}
var el = ''; var el = '';
if (t.length > 0) {
el += '<div class="widget-wrap" id="toc">';
el += '<div class="widget-header cap dis-select">'; el += '<div class="widget-header cap dis-select">';
if (page.toc_title) { if (title) {
el += '<span class="name">' + page.toc_title + '</span>'; el += '<span class="name">' + title + '</span>';
} else if (t == 'wiki') {
el += '';
} else { } else {
el += '<span class="name">' + __("meta.toc") + '</span>'; el += '<span class="name">' + __("meta.toc") + '</span>';
} }
el += '</div>'; el += '</div>';
el += '<div class="widget-body fs14 ' + t + '">'; return el;
if (page.layout == "wiki" && page.wiki) {
var wikis = [];
wikis = site.pages.filter(function (p) {
if (p.layout == "wiki" && p.wiki && p.wiki == page.wiki && (p.title || p.seo_title)) {
if (p.order == undefined) {
p.order = 0;
} }
return true;
} else { function layoutDocTree(pages) {
return false; var el = '';
} pages.forEach((p, i) => {
}).sort("order");
if (wikis.length > 1) {
wikis.each(function(p) {
let isActive = ''; let isActive = '';
if (p.path == page.path) { if (p.path === page.path) {
isActive += ' active'; isActive += ' active';
} }
el += '<div class="doc-tree' + isActive + '">'; el += '<div class="doc-tree' + isActive + '">';
if (proj.pages.length > 1) {
el += '<a class="doc-tree-link' + isActive + '" href="' + url_for(p.path) + '">'; el += '<a class="doc-tree-link' + isActive + '" href="' + url_for(p.path) + '">';
el += '<span class="toc-text">' + (p.title || p.seo_title) + '</span>'; el += '<span class="toc-text">' + (p.title || p.seo_title) + '</span>';
el += '</a>'; el += '</a>';
if (p.path == page.path) { }
if (p.path === page.path) {
el += layoutToc(); el += layoutToc();
} }
el += '</div>'; el += '</div>';
}); });
} else {
el += '<div class="doc-tree active">';
el += layoutToc();
el += '</div>';
}
} else {
el += '<div class="doc-tree active">';
el += layoutToc();
el += '</div>';
}
el += '</div>';
el += '</div>';
return el; return el;
} else if (item.fallback) { }
return fallback(item.fallback);
function layoutDiv(fallback) {
var type = '';
if (proj && proj.pages) {
if (proj.pages.length > 1) {
type = 'multi';
} else {
type = 'single';
}
} else {
let toc_content = toc(page.content);
if (toc_content && toc_content.length > 0) {
type = 'single';
} }
} }
var el = '';
if (type.length > 0) {
el += '<div class="widget-wrap" id="toc">';
if (page.layout !== 'wiki') {
// post 布局
el += layoutTocHeader(page.toc_title || __("meta.toc"));
el += '<div class="widget-body fs14 ' + type + '">';
el += '<div class="doc-tree active">';
el += layoutToc();
el += '</div>';
el += '</div>';
} else if (proj) {
// wiki 布局
if (proj.sections && proj.sections.length > 0) {
proj.sections.forEach((sec, i) => {
// 多 section
el += layoutTocHeader(sec.title);
el += '<div class="widget-body fs14 ' + type + '">';
el += layoutDocTree(sec.pages);
el += '</div>';
});
} else {
// 单 section
el += '<div class="widget-body fs14 ' + type + '">';
el += layoutDocTree(proj.pages);
el += '</div>';
}
}
el += '</div>';
} else if (item.fallback) {
el += partial(item.fallback, {item: theme.sidebar.widgets[item.fallback]});
}
return el;
}
%> %>
<%-
layoutDiv(function(widget){ <%- layoutDiv() %>
return partial(widget, {item: theme.sidebar.widgets[widget]})
})
%>

View File

@ -67,6 +67,34 @@ module.exports = hexo => {
proj.pages.limit(1).forEach((p, i) => { proj.pages.limit(1).forEach((p, i) => {
proj.path = p.path; proj.path = p.path;
}); });
// 内页按 section 分组
var secs = [];
if (proj.sections) {
for (let t of Object.keys(proj.sections)) {
let range = proj.sections[t];
if (range.length > 1) {
secs.push({
title: t,
from: range[0],
to: range[1]
});
}
}
}
var newSections = [];
secs.forEach((sec, i) => {
const pages = proj.pages.filter(function (p) {
return p.order >= sec.from && p.order <= sec.to;
});
if (pages && pages.length > 0) {
newSections.push({
title: sec.title,
pages: pages
});
}
});
proj.sections = newSections;
} }
var groups = {}; var groups = {};

View File

@ -30,7 +30,7 @@ hexo.extend.tag.register('copy', function(args) {
} }
} }
const copy_id = 'copy-' + ++copy_index + '-' + text.replace(/[^a-z|0-9|.:]/gi, ''); const copy_id = 'copy_' + ++copy_index + '-' + text.replace(/[^a-z|0-9|.:]/gi, '');
var el = ''; var el = '';
el += '<div class="tag-plugin copy"'; el += '<div class="tag-plugin copy"';

View File

@ -4,6 +4,8 @@
'use strict'; 'use strict';
var tab_index = 0;
module.exports = ctx => function(args, content) { module.exports = ctx => function(args, content) {
var el = ''; var el = '';
var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => { var arr = content.split(/<!--\s*(.*?)\s*-->/g).filter((item, i) => {
@ -28,16 +30,14 @@ module.exports = ctx => function(args, content) {
} }
}); });
args = ctx.args.map(args, ['active', 'align'], ['tabName']); args = ctx.args.map(args, ['active', 'align']);
const tabName = args.tabName; const tabName = 'tab_' + ++tab_index;
const tabActive = Number(args.active) || 0; const tabActive = Number(args.active) || 0;
let tabId = 0; let tabId = 0;
let tabNav = ''; let tabNav = '';
let tabContent = ''; let tabContent = '';
if (!tabName) ctx.log.warn('Tabs block must have unique name!');
tabs.forEach((tab, i) => { tabs.forEach((tab, i) => {
let caption = tab.header.substring(4); let caption = tab.header.substring(4);
let content = ctx.render.renderSync({ text: tab.body, engine: 'markdown' }).trim(); let content = ctx.render.renderSync({ text: tab.body, engine: 'markdown' }).trim();

View File

@ -1,10 +1,10 @@
.l_left[layout='wiki'] .widget-wrap#toc .widget-wrap#toc
margin-top: 0 .widget-body+.widget-header
.widget-body margin-top: 1rem
margin-top: 0
// //
.widget-wrap#toc .doc-tree:not(:only-child) .widget-wrap#toc .multi .doc-tree
border-radius: 4px border-radius: 4px
margin: 4px 0 margin: 4px 0
background: var(--block) background: var(--block)
@ -33,7 +33,7 @@
// //
.widget-wrap#toc .doc-tree.active:not(:only-child) .widget-wrap#toc .multi .doc-tree.active
a.doc-tree-link a.doc-tree-link
background: var(--block) background: var(--block)
font-weight: 700 font-weight: 700