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

123 lines
3.4 KiB
Plaintext

<%
let proj = theme.wiki.tree[page.wiki];
var hasTOC = true
function layoutTocBody() {
if (toc(page.content).length > 0) {
hasTOC = true
return toc(page.content, {
list_number: item.list_number,
min_depth: item.min_depth,
max_depth: item.max_depth
});
}
hasTOC = false
return ''
}
function layoutTocHeader(title) {
var el = ''
el += `<div class="widget-header cap dis-select">`
el += `<span class="name">${title || __("meta.toc")}</span>`
el += `</div>`
return el
}
function layoutDocTree(pages) {
var el = ''
for (let p of pages) {
if (p.title == null || p.title.length == 0) {
continue
}
let isActive = ''
if (p.path === page.path) {
isActive += ' active'
}
el += `<div class="doc-tree${isActive}">`
if (proj.pages.length > 1) {
let href = url_for(p.path);
if (p.is_homepage) {
href += '#start'
}
el += `<a class="doc-tree-link${isActive}" href="${href}">`
el += `<span class="toc-text">${p.title}</span>`
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>`
}
el += `</a>`
}
if (p.path === page.path) {
el += layoutTocBody()
}
el += `</div>`
}
return el
}
function layoutDiv(fallback) {
var type = ''
if (proj && proj.pages) {
if (proj.pages.length > 1) {
type = 'multi'
} else {
type = 'single'
}
} else {
let toc_content = toc(page.content)
if (toc_content && toc_content.length > 0) {
type = 'single'
}
}
var el = ''
if (type.length > 0) {
el += `<widget class="widget-wrapper toc ${type}" id="data-toc" collapse="${item.collapse}">`
if (page.layout !== 'wiki') {
// post 布局
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 ''
}
} else if (proj) {
// wiki 布局
if (proj.sections && proj.sections.length > 0 && proj.pages.length > 1) { // 多 pages
for (let sec of proj.sections) {
if (sec.pages.length == 0) {
continue
}
if (sec.title?.length > 0) {
el += layoutTocHeader(sec.title)
}
el += `<div class="widget-body fs14">`
el += layoutDocTree(sec.pages)
el += `</div>`
}
} else { // 单 page
if (proj.pages.length == 1) {
el += layoutTocHeader(page.toc_title)
}
el += `<div class="widget-body fs14">`
el += layoutDocTree(proj.pages)
el += `</div>`
if (hasTOC == false) {
return ''
}
}
}
el += `</widget>`
} else if (item.fallback) {
const fallback = theme.data.widgets[item.fallback]
el += partial(fallback.layout, {item: fallback})
}
return el
}
%>
<%- layoutDiv() %>