Skip to content

Commit

Permalink
Add maxDepth option to specify which levels are included
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 21, 2015
1 parent 81a288a commit 24163f6
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 8 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ function isClosingHeading(node, depth) {
* @param {Node} root
* @param {RegExp} expression
* @param {function(string): string} library
* @param {number} maxDepth
* @return {Object}
*/
function search(root, expression, library) {
function search(root, expression, library, maxDepth) {
var index = -1;
var length = root.children.length;
var depth = null;
Expand Down Expand Up @@ -164,7 +165,7 @@ function search(root, expression, library) {
}
}

if (!lookingForToc && value) {
if (!lookingForToc && value && child.depth <= maxDepth) {
map.push({
'depth': child.depth,
'value': value
Expand Down Expand Up @@ -464,6 +465,7 @@ function attacher(mdast, options) {
var library = settings.library || DEFAULT_LIBRARY;
var isNPM = library === NPM;
var isGitHub = library === GITHUB;
var depth = settings.maxDepth || 6;

if (isNPM || isGitHub) {
library = SLUGG;
Expand All @@ -486,7 +488,7 @@ function attacher(mdast, options) {
* @param {Node} node
*/
function transformer(node) {
var result = search(node, heading, library);
var result = search(node, heading, library, depth);

if (result.index === null || !result.map.length) {
return;
Expand Down
8 changes: 5 additions & 3 deletions mdast-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ function isClosingHeading(node, depth) {
* @param {Node} root
* @param {RegExp} expression
* @param {function(string): string} library
* @param {number} maxDepth
* @return {Object}
*/
function search(root, expression, library) {
function search(root, expression, library, maxDepth) {
var index = -1;
var length = root.children.length;
var depth = null;
Expand Down Expand Up @@ -166,7 +167,7 @@ function search(root, expression, library) {
}
}

if (!lookingForToc && value) {
if (!lookingForToc && value && child.depth <= maxDepth) {
map.push({
'depth': child.depth,
'value': value
Expand Down Expand Up @@ -466,6 +467,7 @@ function attacher(mdast, options) {
var library = settings.library || DEFAULT_LIBRARY;
var isNPM = library === NPM;
var isGitHub = library === GITHUB;
var depth = settings.maxDepth || 6;

if (isNPM || isGitHub) {
library = SLUGG;
Expand All @@ -488,7 +490,7 @@ function attacher(mdast, options) {
* @param {Node} node
*/
function transformer(node) {
var result = search(node, heading, library);
var result = search(node, heading, library, depth);

if (result.index === null || !result.map.length) {
return;
Expand Down
2 changes: 1 addition & 1 deletion mdast-toc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ Adds a [Table of Contents](#table-of-contents) to a Markdown document.
— Library to use.

* `heading` (`string?`, default: `"toc|table[ -]of[ -]contents?"`)
heading to look for, wrapped in
Heading to look for, wrapped in
`new RegExp('^(' + value + ')$', 'i');`.

* `maxDepth` (`number?`, default: `6`)
— Maximum heading depth to include in the table of contents,
This is inclusive, thus, when set to `3`, level three headings,
are included (those with three hashes, `###`).

## License

[MIT](LICENSE) © [Titus Wormer](http://wooorm.com)
3 changes: 3 additions & 0 deletions test/fixtures/maximum-depth-1/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"maxDepth": 1
}
15 changes: 15 additions & 0 deletions test/fixtures/maximum-depth-1/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Normal

# Table of Contents

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
17 changes: 17 additions & 0 deletions test/fixtures/maximum-depth-1/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Normal

# Table of Contents

- [Alpha](#alpha)

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
3 changes: 3 additions & 0 deletions test/fixtures/maximum-depth-3/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"maxDepth": 3
}
15 changes: 15 additions & 0 deletions test/fixtures/maximum-depth-3/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Normal

# Table of Contents

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
21 changes: 21 additions & 0 deletions test/fixtures/maximum-depth-3/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Normal

# Table of Contents

- [Alpha](#alpha)

- [Bravo](#bravo)

- [Charlie](#charlie)

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
3 changes: 3 additions & 0 deletions test/fixtures/maximum-depth-6/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"maxDepth": 6
}
15 changes: 15 additions & 0 deletions test/fixtures/maximum-depth-6/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Normal

# Table of Contents

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot
27 changes: 27 additions & 0 deletions test/fixtures/maximum-depth-6/output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Normal

# Table of Contents

- [Alpha](#alpha)

- [Bravo](#bravo)

- [Charlie](#charlie)

- [Delta](#delta)

- [Echo](#echo)

- [Foxtrot](#foxtrot)

# Alpha

## Bravo

### Charlie

#### Delta

##### Echo

###### Foxtrot

0 comments on commit 24163f6

Please sign in to comment.