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

103 lines
2.5 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">';
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 = '';
pages.forEach((p, i) => {
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 + '">';
2021-07-25 22:47:18 +08:00
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) {
2022-11-23 21:54:21 +08:00
el += '<widget class="widget-wrapper toc ' + type + '" id="toc">';
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 布局
if (proj.sections && proj.sections.length > 0) {
proj.sections.forEach((sec, i) => {
// 多 section
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>';
});
} else {
2021-07-25 22:47:18 +08:00
// 单 section
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 += 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() %>