From 40f1779c50783582dccd400597aee8233d337103 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Wed, 7 Apr 2021 21:34:11 +0800 Subject: [PATCH] toc fallback --- _config.yml | 1 + layout/_partial/sidebar/widgets/toc.ejs | 142 +++++++++++++----------- 2 files changed, 79 insertions(+), 64 deletions(-) diff --git a/_config.yml b/_config.yml index c10d510..0f4da40 100755 --- a/_config.yml +++ b/_config.yml @@ -38,6 +38,7 @@ sidebar: list_number: false min_depth: 2 max_depth: 5 + fallback: recent # Use a backup widget when toc does not exist. # welcome welcome: layout: markdown diff --git a/layout/_partial/sidebar/widgets/toc.ejs b/layout/_partial/sidebar/widgets/toc.ejs index ea6a9fb..fe1e9d1 100644 --- a/layout/_partial/sidebar/widgets/toc.ejs +++ b/layout/_partial/sidebar/widgets/toc.ejs @@ -1,14 +1,5 @@ <% -var enable = false; -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() { +function layoutToc() { if (toc(page.content).length > 0) { return toc(page.content, { list_number: item.list_number, @@ -17,58 +8,81 @@ function layout_toc() { }); } } - %> -<% if (enable == true) { %> -
-
- - <% if (page.toc_title) { %> - <%- page.toc_title %> - <% } else if (page.layout == 'wiki') { %> - <%- page.wiki || item.wiki %> - <% } else { %> - <%- __('meta.toc') %> - <% } %> - -
-
- <% 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; +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; + } + var el = ''; + if (t.length > 0) { + el += '
'; + el += '
'; + el += ''; + if (page.toc_title) { + el += page.toc_title; + } else if (toc == 'wiki') { + el += page.wiki || item.wiki; + } else { + el += __("meta.toc"); + } + el += ''; + 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; } - }).sort('order'); - %> - <% if (wikis.length > 1) { %> - <% wikis.each(function(p) { %> - <% if (p.path == page.path) { %> -
- <%- p.title || p.seo_title %> - <%- layout_toc() %> -
- <% } else { %> - - <% } %> - <% }); %> - <% } else { %> -
- <%- layout_toc() %> -
- <% } %> - <% } else { %> -
- <%- layout_toc() %> -
- <% } %> -
-
-<% } %> + 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 += '
'; + } + } else { + el += '
'; + el += layoutToc(); + el += '
'; + } + el += '
'; + el += '
'; + return el; + } else if (item.fallback) { + return fallback(item.fallback); + } +} + +%> +<%- +layoutDiv(function(widget){ + return partial(widget, {item: theme.sidebar.widgets[widget]}) +}) +%>