toc fallback

This commit is contained in:
xaoxuu 2021-04-07 21:34:11 +08:00
parent 5dd3622c3d
commit 40f1779c50
2 changed files with 79 additions and 64 deletions

View File

@ -38,6 +38,7 @@ sidebar:
list_number: false list_number: false
min_depth: 2 min_depth: 2
max_depth: 5 max_depth: 5
fallback: recent # Use a backup widget when toc does not exist.
# welcome # welcome
welcome: welcome:
layout: markdown layout: markdown

View File

@ -1,14 +1,5 @@
<% <%
var enable = false; function layoutToc() {
if (page.layout == 'post') {
if (page.content && toc(page.content).length > 0) {
enable = true;
}
} else if (page.layout == 'wiki' || page.sidebar.includes('toc') == true) {
enable = true;
}
function layout_toc() {
if (toc(page.content).length > 0) { if (toc(page.content).length > 0) {
return toc(page.content, { return toc(page.content, {
list_number: item.list_number, list_number: item.list_number,
@ -17,58 +8,81 @@ function layout_toc() {
}); });
} }
} }
%> function layoutDiv(fallback) {
<% if (enable == true) { %> var t = '';
<div class='widget-wrap' id='toc'> if (page.layout == 'post' && page.content) {
<div class='widget-header h4 dis-select'> let toc_content = toc(page.content);
<span class='name'> if (toc_content && toc_content.length > 0) {
<% if (page.toc_title) { %> t = page.layout;
<%- page.toc_title %> }
<% } else if (page.layout == 'wiki') { %> } else if (page.layout == 'wiki') {
<%- page.wiki || item.wiki %> t = page.layout;
<% } else { %> } else if (page.sidebar.includes('toc') == true) {
<%- __('meta.toc') %> t = page.layout;
<% } %> }
</span> var el = '';
</div> if (t.length > 0) {
<div class='widget-body fs14 <%- page.layout == "wiki" ? "wiki" : "post" %>'> el += '<div class="widget-wrap" id="toc">';
<% if (page.layout == 'wiki' && page.wiki) { %> el += '<div class="widget-header h4 dis-select">';
<% el += '<span class="name">';
var wikis = []; if (page.toc_title) {
wikis = site.pages.filter(function (p) { el += page.toc_title;
if (p.layout == 'wiki' && p.wiki && p.wiki == page.wiki && (p.title || p.seo_title)) { } else if (toc == 'wiki') {
if (p.order == undefined) { el += page.wiki || item.wiki;
p.order = 0; } else {
} el += __("meta.toc");
return true; }
} else { el += '</span>';
return false; el += '</div>';
el += '<div class="widget-body fs14 ' + t + '">';
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;
} }
}).sort('order'); return true;
%> } else {
<% if (wikis.length > 1) { %> return false;
<% wikis.each(function(p) { %> }
<% if (p.path == page.path) { %> }).sort("order");
<div class='doc-tree active'> if (wikis.length > 1) {
<a class='doc-tree-link active' href="<%- url_for(p.path) %>"><span class="toc-text"><%- p.title || p.seo_title %></span></a> wikis.each(function(p) {
<%- layout_toc() %> let isActive = '';
</div> if (p.path == page.path) {
<% } else { %> isActive += ' active';
<div class='doc-tree'> }
<a class='doc-tree-link' href="<%- url_for(p.path) %>"><span class="toc-text"><%- p.title || p.seo_title %></span></a> el += '<div class="doc-tree' + isActive + '">';
</div> el += '<a class="doc-tree-link' + isActive + '" href="' + url_for(p.path) + '">';
<% } %> el += '<span class="toc-text">' + (p.title || p.seo_title) + '</span>';
<% }); %> el += '</a>';
<% } else { %> if (p.path == page.path) {
<div class='doc-tree active'> el += layoutToc();
<%- layout_toc() %> }
</div> el += '</div>';
<% } %> });
<% } else { %> } else {
<div class='doc-tree active'> el += '<div class="doc-tree active">';
<%- layout_toc() %> el += layoutToc();
</div> el += '</div>';
<% } %> }
</div> } else {
</div> el += '<div class="doc-tree active">';
<% } %> el += layoutToc();
el += '</div>';
}
el += '</div>';
el += '</div>';
return el;
} else if (item.fallback) {
return fallback(item.fallback);
}
}
%>
<%-
layoutDiv(function(widget){
return partial(widget, {item: theme.sidebar.widgets[widget]})
})
%>