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

123 lines
3.4 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];
2024-01-14 01:39:02 +08:00
var hasTOC = true
function layoutTocBody() {
2021-02-19 23:33:19 +08:00
if (toc(page.content).length > 0) {
2024-01-13 21:10:26 +08:00
hasTOC = true
2021-02-19 23:33:19 +08:00
return toc(page.content, {
list_number: item.list_number,
min_depth: item.min_depth,
max_depth: item.max_depth
});
}
2024-01-13 21:10:26 +08:00
hasTOC = false
return ''
2021-02-19 23:33:19 +08:00
}
2021-07-25 22:47:18 +08:00
function layoutTocHeader(title) {
2024-01-14 01:39:02 +08:00
var el = ''
2024-01-17 22:57:52 +08:00
el += `<div class="widget-header dis-select">`
2024-01-14 01:39:02 +08:00
el += `<span class="name">${title || __("meta.toc")}</span>`
el += `</div>`
return el
2021-07-25 22:47:18 +08:00
}
function layoutDocTree(pages) {
2024-01-14 01:39:02 +08:00
var el = ''
2023-12-06 13:22:07 +08:00
for (let p of pages) {
if (p.title == null || p.title.length == 0) {
2024-01-14 01:39:02 +08:00
continue
2023-12-06 13:22:07 +08:00
}
2024-01-14 01:39:02 +08:00
let isActive = ''
2021-07-25 22:47:18 +08:00
if (p.path === page.path) {
2024-01-14 01:39:02 +08:00
isActive += ' active'
2021-07-25 22:47:18 +08:00
}
2024-01-14 01:39:02 +08:00
el += `<div class="doc-tree${isActive}">`
2021-07-25 22:47:18 +08:00
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'
}
2024-01-14 01:39:02 +08:00
el += `<a class="doc-tree-link${isActive}" href="${href}">`
el += `<span class="toc-text">${p.title}</span>`
2024-01-17 00:27:48 +08:00
if (isActive.length > 0) {
el += `<svg t="1705415018387" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="14793" width="200" height="200"><path d="M256 896a42.666667 42.666667 0 0 1-20.906667-5.546667A42.666667 42.666667 0 0 1 213.333333 853.333333V227.413333A97.28 97.28 0 0 1 307.2 128h409.6A97.28 97.28 0 0 1 810.666667 227.413333V853.333333a42.666667 42.666667 0 0 1-21.333334 36.693334 42.666667 42.666667 0 0 1-42.666666 0l-241.92-136.96-227.413334 136.533333A42.666667 42.666667 0 0 1 256 896z" p-id="14794"></path></svg>`
}
2024-01-14 01:39:02 +08:00
el += `</a>`
2021-07-25 22:47:18 +08:00
}
if (p.path === page.path) {
2024-01-14 01:39:02 +08:00
el += layoutTocBody()
2021-07-25 22:47:18 +08:00
}
2024-01-14 01:39:02 +08:00
el += `</div>`
2023-12-06 13:22:07 +08:00
}
2024-01-14 01:39:02 +08:00
return el
2021-07-25 22:47:18 +08:00
}
2021-04-07 21:34:11 +08:00
function layoutDiv(fallback) {
2024-01-14 01:39:02 +08:00
var type = ''
2021-07-25 22:47:18 +08:00
if (proj && proj.pages) {
if (proj.pages.length > 1) {
2024-01-14 01:39:02 +08:00
type = 'multi'
2021-07-25 22:47:18 +08:00
} else {
2024-01-14 01:39:02 +08:00
type = 'single'
2021-07-25 22:47:18 +08:00
}
} else {
2024-01-14 01:39:02 +08:00
let toc_content = toc(page.content)
2021-04-07 21:34:11 +08:00
if (toc_content && toc_content.length > 0) {
2024-01-14 01:39:02 +08:00
type = 'single'
2021-04-07 21:34:11 +08:00
}
}
2021-07-25 22:47:18 +08:00
2024-01-14 01:39:02 +08:00
var el = ''
2021-07-25 22:47:18 +08:00
if (type.length > 0) {
2024-01-14 01:39:02 +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 布局
2024-01-14 01:39:02 +08:00
el += layoutTocHeader(page.toc_title)
el += `<div class="widget-body fs14">`
el += `<div class="doc-tree active">`
el += layoutTocBody()
el += `</div>`
el += `</div>`
if (hasTOC == false) {
return ''
}
2021-07-25 22:47:18 +08:00
} 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) {
2024-01-14 01:39:02 +08:00
continue
2023-12-20 12:17:35 +08:00
}
2023-12-06 13:20:04 +08:00
if (sec.title?.length > 0) {
2024-01-14 01:39:02 +08:00
el += layoutTocHeader(sec.title)
2023-12-06 13:20:04 +08:00
}
2024-01-14 01:39:02 +08:00
el += `<div class="widget-body fs14">`
el += layoutDocTree(sec.pages)
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) {
2024-01-14 01:39:02 +08:00
el += layoutTocHeader(page.toc_title)
}
el += `<div class="widget-body fs14">`
el += layoutDocTree(proj.pages)
el += `</div>`
if (hasTOC == false) {
return ''
2023-11-28 17:41:52 +08:00
}
2021-04-07 21:34:11 +08:00
}
}
2024-01-14 01:39:02 +08:00
el += `</widget>`
2021-04-07 21:34:11 +08:00
} else if (item.fallback) {
2024-01-17 00:27:48 +08:00
const fallback = theme.data.widgets[item.fallback]
el += partial(fallback.layout, {item: fallback})
2021-04-07 21:34:11 +08:00
}
2024-01-14 01:39:02 +08:00
return el
2021-04-07 21:34:11 +08:00
}
%>
2021-07-25 22:47:18 +08:00
<%- layoutDiv() %>