hexo-theme-stellar/layout/_partial/sidebar/widgets/toc.ejs

102 lines
2.4 KiB
Plaintext
Raw Normal View History

2021-02-19 23:33:19 +08:00
<%
2021-07-25 22:47:18 +08:00
let proj = theme.wiki.projects[page.wiki];
2021-04-07 21:34:11 +08:00
function layoutToc() {
2021-02-19 23:33:19 +08:00
if (toc(page.content).length > 0) {
return toc(page.content, {
list_number: item.list_number,
min_depth: item.min_depth,
max_depth: item.max_depth
});
}
2021-04-22 21:42:39 +08:00
return '';
2021-02-19 23:33:19 +08:00
}
2021-07-25 22:47:18 +08:00
function layoutTocHeader(title) {
var el = '';
el += '<div class="widget-header cap dis-select">';
if (title) {
el += '<span class="name">' + title + '</span>';
} else {
el += '<span class="name">' + __("meta.toc") + '</span>';
}
el += '</div>';
return el;
}
function layoutDocTree(pages) {
var el = '';
pages.forEach((p, i) => {
let isActive = '';
if (p.path === page.path) {
isActive += ' active';
}
el += '<div class="doc-tree' + isActive + '">';
if (proj.pages.length > 1) {
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>';
}
if (p.path === page.path) {
el += layoutToc();
}
el += '</div>';
});
return el;
}
2021-04-07 21:34:11 +08:00
function layoutDiv(fallback) {
2021-07-25 22:47:18 +08:00
var type = '';
if (proj && proj.pages) {
if (proj.pages.length > 1) {
type = 'multi';
} else {
type = 'single';
}
} else {
2021-04-07 21:34:11 +08:00
let toc_content = toc(page.content);
if (toc_content && toc_content.length > 0) {
2021-07-25 22:47:18 +08:00
type = 'single';
2021-04-07 21:34:11 +08:00
}
}
2021-07-25 22:47:18 +08:00
2021-04-07 21:34:11 +08:00
var el = '';
2021-07-25 22:47:18 +08:00
if (type.length > 0) {
2021-04-07 21:34:11 +08:00
el += '<div class="widget-wrap" id="toc">';
2021-07-25 22:47:18 +08:00
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);
2021-04-07 21:34:11 +08:00
el += '</div>';
});
} else {
2021-07-25 22:47:18 +08:00
// 单 section
el += '<div class="widget-body fs14 ' + type + '">';
el += layoutDocTree(proj.pages);
2021-04-07 21:34:11 +08:00
el += '</div>';
}
}
el += '</div>';
} else if (item.fallback) {
2021-07-25 22:47:18 +08:00
el += partial(item.fallback, {item: theme.sidebar.widgets[item.fallback]});
2021-04-07 21:34:11 +08:00
}
2021-07-25 22:47:18 +08:00
return el;
2021-04-07 21:34:11 +08:00
}
%>
2021-07-25 22:47:18 +08:00
<%- layoutDiv() %>