diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 8c6cc83..52b2ab1 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -67,17 +67,19 @@ export function Fetch(Base) { _loadSideAndNav(path, qs, loadSidebar, cb) { return () => { - if (!loadSidebar) { - return cb(); - } - - const fn = result => { + const renderSidebar = result => { this._renderSidebar(result); cb(); }; - // Load sidebar - this.#loadNested(path, qs, loadSidebar, fn, this, true); + if (!loadSidebar) { + // Although, we don't load sidebar from sidebar file, we still need call the render to auto generate sidebar from headings toc + renderSidebar(); + return; + } + + // Load sidebar from the sidebar file + this.#loadNested(path, qs, loadSidebar, renderSidebar, this, true); }; } diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index caed155..d01e65a 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -251,8 +251,8 @@ export class Compiler { } /** - * Compile sidebar - * @param {String} text Text content + * Compile sidebar, it uses _sidebar.md ( or specific file) or the content's headings toc to render sidebar. + * @param {String} text Text content from the sidebar file, maybe empty * @param {Number} level Type of heading (h tag) * @returns {String} Sidebar element */ @@ -261,50 +261,53 @@ export class Compiler { const currentPath = this.router.getCurrentPath(); let html = ''; + // compile sidebar from _sidebar.md if (text) { - html = this.compile(text); - } else { - for (let i = 0; i < toc.length; i++) { - if (toc[i].ignoreSubHeading) { - const deletedHeaderLevel = toc[i].level; - toc.splice(i, 1); - // Remove headers who are under current header - for ( - let j = i; - j < toc.length && deletedHeaderLevel < toc[j].level; - j++ - ) { - toc.splice(j, 1) && j-- && i++; - } - - i--; + return this.compile(text); + } + // compile sidebar from content's headings toc + for (let i = 0; i < toc.length; i++) { + if (toc[i].ignoreSubHeading) { + const deletedHeaderLevel = toc[i].depth; + toc.splice(i, 1); + // Remove headers who are under current header + for ( + let j = i; + j < toc.length && deletedHeaderLevel < toc[j].depth; + j++ + ) { + toc.splice(j, 1) && j-- && i++; } - } - const tree = this.cacheTree[currentPath] || genTree(toc, level); - html = treeTpl(tree, /* html */ ''); - this.cacheTree[currentPath] = tree; + i--; + } } + const tree = this.cacheTree[currentPath] || genTree(toc, level); + html = treeTpl(tree); + this.cacheTree[currentPath] = tree; return html; } + /** + * When current content redirect to a new path file, clean pre content headings toc + */ + resetToc() { + this.toc = []; + } + /** * Compile sub sidebar * @param {Number} level Type of heading (h tag) * @returns {String} Sub-sidebar element */ subSidebar(level) { - if (!level) { - this.toc = []; - return; - } - const currentPath = this.router.getCurrentPath(); const { cacheTree, toc } = this; toc[0] && toc[0].ignoreAllSubs && toc.splice(0); - toc[0] && toc[0].level === 1 && toc.shift(); + // remove the first heading from the toc if it is a top-level heading + toc[0] && toc[0].depth === 1 && toc.shift(); for (let i = 0; i < toc.length; i++) { toc[i].ignoreSubHeading && toc.splice(i, 1) && i--; diff --git a/src/core/render/gen-tree.js b/src/core/render/gen-tree.js index f0e6dde..94030ae 100644 --- a/src/core/render/gen-tree.js +++ b/src/core/render/gen-tree.js @@ -10,7 +10,7 @@ export function genTree(toc, maxLevel) { const last = {}; toc.forEach(headline => { - const level = headline.level || 1; + const level = headline.depth || 1; const len = level - 1; if (level > maxLevel) { diff --git a/src/core/render/index.js b/src/core/render/index.js index 4b8d60a..06f900b 100644 --- a/src/core/render/index.js +++ b/src/core/render/index.js @@ -84,9 +84,6 @@ export function Render(Base) { this._renderTo(markdownElm, html); - // Render sidebar with the TOC - !docsifyConfig.loadSidebar && this._renderSidebar(); - // Execute markdown