Skip to content

Commit

Permalink
fix(gen-tree): cache toc list, fixed #88
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 21, 2017
1 parent b4fc6a9 commit 3394ebb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
8 changes: 3 additions & 5 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let markdownCompiler = marked
let contentBase = ''
let currentPath = ''
let renderer = new marked.Renderer()
const TOC = {}
const cacheTree = {}
let toc = []

/**
Expand Down Expand Up @@ -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 = []
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/render/gen-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 3394ebb

Please sign in to comment.