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

111 lines
2.7 KiB
Plaintext
Raw Normal View History

2021-02-19 23:33:19 +08:00
<%
2023-12-06 13:20:04 +08:00
let proj = theme.wiki.tree[page.wiki];
2021-07-25 22:47:18 +08:00
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">';
2022-10-22 13:03:47 +08:00
el += '<span class="name">' + (title || page.title || __("meta.toc")) + '</span>';
2021-07-25 22:47:18 +08:00
el += '</div>';
return el;
}
function layoutDocTree(pages) {
var el = '';
2023-12-06 13:22:07 +08:00
for (let p of pages) {
if (p.title == null || p.title.length == 0) {
continue;
}
2021-07-25 22:47:18 +08:00
let isActive = '';
if (p.path === page.path) {
isActive += ' active';
}
el += '<div class="doc-tree' + isActive + '">';
if (proj.pages.length > 1) {
2022-11-19 16:48:44 +08:00
let href = url_for(p.path);
2022-11-19 23:03:52 +08:00
if (p.is_homepage) {
2022-11-19 16:48:44 +08:00
href += '#start'
}
el += '<a class="doc-tree-link' + isActive + '" href="' + href + '">';
2023-12-06 13:22:07 +08:00
el += '<span class="toc-text">' + p.title + '</span>';
2021-07-25 22:47:18 +08:00
el += '</a>';
}
if (p.path === page.path) {
el += layoutToc();
}
el += '</div>';
2023-12-06 13:22:07 +08:00
}
2021-07-25 22:47:18 +08:00
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) {
2024-01-02 19:50:42 +08:00
el += `<widget class="widget-wrapper toc ${type}" id="data-toc" collapse="${item.collapse}">`;
2021-07-25 22:47:18 +08:00
if (page.layout !== 'wiki') {
// post 布局
2021-11-19 22:04:14 +08:00
el += layoutTocHeader(page.toc_title);
2021-07-26 22:26:46 +08:00
el += '<div class="widget-body fs14">';
2021-07-25 22:47:18 +08:00
el += '<div class="doc-tree active">';
el += layoutToc();
el += '</div>';
el += '</div>';
} else if (proj) {
// wiki 布局
2023-12-06 13:22:07 +08:00
if (proj.sections && proj.sections.length > 0 && proj.pages.length > 1) { // 多 pages
2023-12-20 12:17:35 +08:00
for (let sec of proj.sections) {
if (sec.pages.length == 0) {
continue;
}
2023-12-06 13:20:04 +08:00
if (sec.title?.length > 0) {
el += layoutTocHeader(sec.title);
}
2021-07-26 22:26:46 +08:00
el += '<div class="widget-body fs14">';
2021-07-25 22:47:18 +08:00
el += layoutDocTree(sec.pages);
2021-04-07 21:34:11 +08:00
el += '</div>';
2023-12-20 12:17:35 +08:00
}
2023-12-06 13:22:07 +08:00
} else { // 单 page
2023-11-28 17:41:52 +08:00
if (proj.pages.length == 1) {
el += layoutTocHeader(page.toc_title);
}
2021-07-26 22:26:46 +08:00
el += '<div class="widget-body fs14">';
2021-07-25 22:47:18 +08:00
el += layoutDocTree(proj.pages);
2021-04-07 21:34:11 +08:00
el += '</div>';
}
}
2022-11-23 21:54:21 +08:00
el += '</widget>';
2021-04-07 21:34:11 +08:00
} else if (item.fallback) {
2022-10-20 21:34:54 +08:00
el += partial(item.fallback, {item: theme.data.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() %>