fix(gen-tree): cache toc list, fixed #88

This commit is contained in:
qingwei.li 2017-02-21 19:59:40 +08:00 committed by cinwell.li
parent b4fc6a9d72
commit 3394ebb9a5
2 changed files with 4 additions and 7 deletions

View File

@ -12,7 +12,7 @@ let markdownCompiler = marked
let contentBase = ''
let currentPath = ''
let renderer = new marked.Renderer()
const TOC = {}
const cacheTree = {}
let toc = []
/**
@ -120,11 +120,9 @@ export function sidebar (text, level) {
export function subSidebar (el, level) {
if (el) {
toc[0] && toc[0].level === 1 && toc.shift()
const tree = genTree(TOC[currentPath] || toc, level)
const tree = cacheTree[currentPath] || genTree(toc, level)
el.parentNode.innerHTML += treeTpl(tree, '<ul class="app-sub-sidebar">')
}
if (toc.length) {
TOC[currentPath] = toc.slice()
cacheTree[currentPath] = tree
}
toc = []
}

View File

@ -15,8 +15,7 @@ export function genTree (toc, maxLevel) {
if (level > maxLevel) return
if (last[len]) {
last[len].children = last[len].children || []
last[len].children.push(headline)
last[len].children = (last[len].children || []).concat(headline)
} else {
headlines.push(headline)
}