From d57b80e224d73089d20a70094670e8bd18f2c522 Mon Sep 17 00:00:00 2001 From: Chris Anselmo Date: Wed, 15 Mar 2017 03:50:26 -0400 Subject: [PATCH] Add ability to ignore headers when generating toc (#127) * Added ability to ignore some or all headings when generating toc in sidebar * Added documentation for ignoring sub headers * Fixed minor test issues --- docs/more-pages.md | 22 ++++++++++++++++++++++ lib/docsify.js | 24 +++++++++++++++++++++--- lib/docsify.min.js | 2 -- lib/plugins/emoji.min.js | 1 - lib/plugins/external-script.min.js | 1 - lib/plugins/front-matter.min.js | 1 - lib/plugins/ga.min.js | 1 - lib/plugins/search.min.js | 1 - lib/plugins/zoom-image.min.js | 1 - lib/themes/buble.css | 1 - lib/themes/dark.css | 1 - lib/themes/pure.css | 1 - lib/themes/vue.css | 1 - src/core/render/compiler.js | 22 ++++++++++++++++++++-- 14 files changed, 63 insertions(+), 17 deletions(-) delete mode 100644 lib/docsify.min.js delete mode 100644 lib/plugins/emoji.min.js delete mode 100644 lib/plugins/external-script.min.js delete mode 100644 lib/plugins/front-matter.min.js delete mode 100644 lib/plugins/ga.min.js delete mode 100644 lib/plugins/search.min.js delete mode 100644 lib/plugins/zoom-image.min.js delete mode 100644 lib/themes/buble.css delete mode 100644 lib/themes/dark.css delete mode 100644 lib/themes/pure.css delete mode 100644 lib/themes/vue.css diff --git a/docs/more-pages.md b/docs/more-pages.md index 59ceb33..dbe2e7f 100644 --- a/docs/more-pages.md +++ b/docs/more-pages.md @@ -68,3 +68,25 @@ A custom sidebar can also automatically generate a table of contents by setting ``` + +## Ignoring Subheaders + +When `subMaxLevel` is set, each header is automatically added to the table of contents by default. If you want to ignore a specific header, add `{docsify-ignore}` to it. + +```markdown +# Getting Started + +## Header {docsify-ignore} +This header won't appear in the sidebar table of contents. +``` + +To ignore all headers on a specific page, you can use `{docsify-ignore-all}` on the first header of the page. + +```markdown +# Getting Started {docsify-ignore-all} + +## Header +This header won't appear in the sidebar table of contents. +``` + +Both `{docsify-ignore}` and `{docsify-ignore-all}` will not be rendered on the page when used. diff --git a/lib/docsify.js b/lib/docsify.js index 7b60b40..e76a23f 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -2992,10 +2992,24 @@ markdown.update = function () { * @link https://github.com/chjj/marked#overriding-renderer-methods */ renderer.heading = function (text, level) { + var nextToc = { level: level, title: text }; + + if (/{docsify-ignore}/g.test(text)) { + text = text.replace('{docsify-ignore}',''); + nextToc.title = text; + nextToc.ignoreSubHeading = true; + } + + if (/{docsify-ignore-all}/g.test(text)) { + text = text.replace('{docsify-ignore-all}',''); + nextToc.title = text; + nextToc.ignoreAllSubs = true; + } + var slug = slugify(text); var url = toURL(currentPath, { id: slug }); - - toc.push({ level: level, slug: url, title: text }); + nextToc.slug = url; + toc.push(nextToc); return ("" + text + "") }; @@ -3017,7 +3031,7 @@ renderer.link = function (href, title, text) { if (title) { title = " title=\"" + title + "\""; } - return ("" + text + "") + return ("" + text + "") }; renderer.paragraph = function (text) { if (/^!>/.test(text)) { @@ -3061,7 +3075,11 @@ function sidebar (text, level) { */ function subSidebar (el, level) { if (el) { + toc[0] && toc[0].ignoreAllSubs && (toc = []); toc[0] && toc[0].level === 1 && toc.shift(); + toc.forEach(function (node, i) { + node.ignoreSubHeading && toc.splice(i, 1); + }); var tree$$1 = cacheTree[currentPath] || genTree(toc, level); el.parentNode.innerHTML += tree(tree$$1, '