From b9f3766833e4a1b92e1316846d13bb34c7f176b4 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Sun, 25 Jul 2021 22:47:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=AE=E5=BD=95=E6=A0=91=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layout/_partial/sidebar/index.ejs | 9 +- layout/_partial/sidebar/widgets/toc.ejs | 138 +++++++++++++---------- scripts/events/lib/doc_tree.js | 28 +++++ scripts/tags/copy.js | 2 +- scripts/tags/lib/tabs.js | 8 +- source/css/_layout/sidebar/toc_wiki.styl | 12 +- 6 files changed, 122 insertions(+), 75 deletions(-) diff --git a/layout/_partial/sidebar/index.ejs b/layout/_partial/sidebar/index.ejs index 80ecf8a..8b137ee 100755 --- a/layout/_partial/sidebar/index.ejs +++ b/layout/_partial/sidebar/index.ejs @@ -3,8 +3,13 @@ if (page.sidebar == undefined) { if (page.layout == 'post' && page.content) { page.sidebar = ['toc', 'repo_info']; - } else if (page.layout == 'wiki' && page.content) { - page.sidebar = ['toc', 'repo_info', 'wiki_more']; + } 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']; + } } else { page.sidebar = theme.sidebar.widgets.default_layout; } diff --git a/layout/_partial/sidebar/widgets/toc.ejs b/layout/_partial/sidebar/widgets/toc.ejs index b5e275b..cb4a390 100644 --- a/layout/_partial/sidebar/widgets/toc.ejs +++ b/layout/_partial/sidebar/widgets/toc.ejs @@ -1,4 +1,6 @@ <% +let proj = theme.wiki.projects[page.wiki]; + function layoutToc() { if (toc(page.content).length > 0) { return toc(page.content, { @@ -9,79 +11,91 @@ function layoutToc() { } return ''; } -function layoutDiv(fallback) { - var t = ''; - 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; - } + +function layoutTocHeader(title) { var el = ''; - if (t.length > 0) { - el += '
'; - el += '
'; - if (page.toc_title) { - el += '' + page.toc_title + ''; - } else if (t == 'wiki') { - el += ''; - } else { - el += '' + __("meta.toc") + ''; + el += '
'; + if (title) { + el += '' + title + ''; + } else { + el += '' + __("meta.toc") + ''; + } + el += '
'; + return el; +} + +function layoutDocTree(pages) { + var el = ''; + pages.forEach((p, i) => { + let isActive = ''; + if (p.path === page.path) { + isActive += ' active'; + } + el += '
'; + if (proj.pages.length > 1) { + el += ''; + el += '' + (p.title || p.seo_title) + ''; + el += ''; + } + if (p.path === page.path) { + el += layoutToc(); } el += '
'; - 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 { - return false; - } - }).sort("order"); - if (wikis.length > 1) { - wikis.each(function(p) { - let isActive = ''; - if (p.path == page.path) { - isActive += ' active'; - } - el += '
'; - el += ''; - el += '' + (p.title || p.seo_title) + ''; - el += ''; - if (p.path == page.path) { - el += layoutToc(); - } - el += '
'; - }); - } else { - el += '
'; - el += layoutToc(); - el += '
'; - } + }); + return el; +} + + +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 += '
'; + if (page.layout !== 'wiki') { + // post 布局 + el += layoutTocHeader(page.toc_title || __("meta.toc")); + el += '
'; el += '
'; el += layoutToc(); el += '
'; + el += '
'; + } else if (proj) { + // wiki 布局 + if (proj.sections && proj.sections.length > 0) { + proj.sections.forEach((sec, i) => { + // 多 section + el += layoutTocHeader(sec.title); + el += '
'; + el += layoutDocTree(sec.pages); + el += '
'; + }); + } else { + // 单 section + el += '
'; + el += layoutDocTree(proj.pages); + el += '
'; + } } el += '
'; - el += '
'; - return el; } else if (item.fallback) { - return fallback(item.fallback); + el += partial(item.fallback, {item: theme.sidebar.widgets[item.fallback]}); } + return el; } %> -<%- -layoutDiv(function(widget){ - return partial(widget, {item: theme.sidebar.widgets[widget]}) -}) -%> + +<%- layoutDiv() %> diff --git a/scripts/events/lib/doc_tree.js b/scripts/events/lib/doc_tree.js index 7a54e2a..d9d43f8 100644 --- a/scripts/events/lib/doc_tree.js +++ b/scripts/events/lib/doc_tree.js @@ -67,6 +67,34 @@ module.exports = hexo => { proj.pages.limit(1).forEach((p, i) => { 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 = {}; diff --git a/scripts/tags/copy.js b/scripts/tags/copy.js index 2be21dd..9923a73 100644 --- a/scripts/tags/copy.js +++ b/scripts/tags/copy.js @@ -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 = ''; el += '
function(args, content) { var el = ''; var arr = content.split(//g).filter((item, i) => { @@ -28,16 +30,14 @@ module.exports = ctx => function(args, content) { } }); - args = ctx.args.map(args, ['active', 'align'], ['tabName']); - const tabName = args.tabName; + args = ctx.args.map(args, ['active', 'align']); + const tabName = 'tab_' + ++tab_index; const tabActive = Number(args.active) || 0; let tabId = 0; let tabNav = ''; let tabContent = ''; - if (!tabName) ctx.log.warn('Tabs block must have unique name!'); - tabs.forEach((tab, i) => { let caption = tab.header.substring(4); let content = ctx.render.renderSync({ text: tab.body, engine: 'markdown' }).trim(); diff --git a/source/css/_layout/sidebar/toc_wiki.styl b/source/css/_layout/sidebar/toc_wiki.styl index 2b4bcb7..9887a91 100644 --- a/source/css/_layout/sidebar/toc_wiki.styl +++ b/source/css/_layout/sidebar/toc_wiki.styl @@ -1,10 +1,10 @@ -.l_left[layout='wiki'] .widget-wrap#toc - margin-top: 0 - .widget-body - margin-top: 0 +.widget-wrap#toc + .widget-body+.widget-header + margin-top: 1rem + // 其它分页链接 -.widget-wrap#toc .doc-tree:not(:only-child) +.widget-wrap#toc .multi .doc-tree border-radius: 4px margin: 4px 0 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 background: var(--block) font-weight: 700