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,26 +8,37 @@ 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">';
if (page.toc_title) {
el += page.toc_title;
} else if (toc == 'wiki') {
el += page.wiki || item.wiki;
} else {
el += __("meta.toc");
}
el += '</span>';
el += '</div>';
el += '<div class="widget-body fs14 ' + t + '">';
if (page.layout == "wiki" && page.wiki) {
var wikis = []; var wikis = [];
wikis = site.pages.filter(function (p) { wikis = site.pages.filter(function (p) {
if (p.layout == 'wiki' && p.wiki && p.wiki == page.wiki && (p.title || p.seo_title)) { if (p.layout == "wiki" && p.wiki && p.wiki == page.wiki && (p.title || p.seo_title)) {
if (p.order == undefined) { if (p.order == undefined) {
p.order = 0; p.order = 0;
} }
@ -44,31 +46,43 @@ function layout_toc() {
} else { } else {
return false; return false;
} }
}).sort('order'); }).sort("order");
%> if (wikis.length > 1) {
<% if (wikis.length > 1) { %> wikis.each(function(p) {
<% wikis.each(function(p) { %> let isActive = '';
<% if (p.path == page.path) { %> if (p.path == page.path) {
<div class='doc-tree active'> isActive += ' active';
<a class='doc-tree-link active' href="<%- url_for(p.path) %>"><span class="toc-text"><%- p.title || p.seo_title %></span></a> }
<%- layout_toc() %> el += '<div class="doc-tree' + isActive + '">';
</div> el += '<a class="doc-tree-link' + isActive + '" href="' + url_for(p.path) + '">';
<% } else { %> el += '<span class="toc-text">' + (p.title || p.seo_title) + '</span>';
<div class='doc-tree'> el += '</a>';
<a class='doc-tree-link' href="<%- url_for(p.path) %>"><span class="toc-text"><%- p.title || p.seo_title %></span></a> if (p.path == page.path) {
</div> el += layoutToc();
<% } %> }
<% }); %> el += '</div>';
<% } else { %> });
<div class='doc-tree active'> } else {
<%- layout_toc() %> el += '<div class="doc-tree active">';
</div> el += layoutToc();
<% } %> el += '</div>';
<% } else { %> }
<div class='doc-tree active'> } else {
<%- layout_toc() %> el += '<div class="doc-tree active">';
</div> el += layoutToc();
<% } %> el += '</div>';
</div> }
</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]})
})
%>