Skip to content

Add ability to ignore headers when generating toc #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/more-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,25 @@ A custom sidebar can also automatically generate a table of contents by setting
</script>
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
```

## 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.
24 changes: 21 additions & 3 deletions lib/docsify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ("<h" + level + " id=\"" + slug + "\"><a href=\"" + url + "\" data-id=\"" + slug + "\" class=\"anchor\"><span>" + text + "</span></a></h" + level + ">")
};
Expand All @@ -3017,7 +3031,7 @@ renderer.link = function (href, title, text) {
if (title) {
title = " title=\"" + title + "\"";
}
return ("<a href=\"" + href + "\"" + title + blank + ">" + text + "</a>")
return ("<a href=\"" + href + "\"" + (title || '') + blank + ">" + text + "</a>")
};
renderer.paragraph = function (text) {
if (/^!&gt;/.test(text)) {
Expand Down Expand Up @@ -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, '<ul class="app-sub-sidebar">');
cacheTree[currentPath] = tree$$1;
Expand Down
2 changes: 0 additions & 2 deletions lib/docsify.min.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/emoji.min.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/external-script.min.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/front-matter.min.js

This file was deleted.

1 change: 0 additions & 1 deletion lib/plugins/ga.min.js

This file was deleted.

Loading