Skip to content

Commit 056978e

Browse files
author
Will O'Brien
committed
Pass through 'depth' option
1 parent 7613035 commit 056978e

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@ gulp.task('markdown', function(){
3030

3131
```
3232

33+
By default, doctoc puts the TOC at the top of the file. To place it somewhere else, add:
34+
35+
```
36+
<!-- START doctoc -->
37+
<!-- END doctoc -->
38+
```
39+
40+
Custom `title` and `depth` options are also accepted
41+
42+
```
43+
// ...
44+
.pipe(toc({
45+
title: "Table des matières",
46+
depth: 3
47+
}))
48+
// ...
49+
```
50+
3351
Thanks
3452
-------
3553

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ var through = require('through2'),
66

77
module.exports = function(opts){
88
if(opts == null){opts = {};}
9-
var title = opts.title || "";
9+
var title = opts.title || "",
10+
depth = opts.depth || 4;
1011

1112
var addToc = function (file){
12-
var mdWithToc = doctoc(file.contents.toString(), null, null, title).data;
13+
var mdWithToc = doctoc(file.contents.toString('utf8'), null, depth, title).data;
1314
mdWithToc = mdWithToc || file.contents.toString();
1415
file.contents = new Buffer(mdWithToc, 'utf8');
1516
return file;

test/fixture/after-w-depth.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
# Article
3+
4+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
5+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
6+
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
7+
8+
- [Hello](#hello)
9+
10+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
11+
12+
## Hello
13+
14+
### World
15+
16+
### Hi
17+
18+
19+

test/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,24 @@ it('should empty files', function (cb) {
6767

6868
stream.end();
6969
});
70+
71+
it('should allow depth change', function (cb) {
72+
73+
var stream = gdoctoc({depth: 2});
74+
75+
stream.on('data', function (file) {
76+
assert.equal(file.relative, 'before.md');
77+
assert.equal(file.path, 'test/fixture/before.md');
78+
assert.equal(file.contents.toString(), fs.readFileSync('./test/fixture/after-w-depth.md', 'utf8'));
79+
cb();
80+
});
81+
82+
stream.write(new gutil.File({
83+
cwd: 'test',
84+
base: 'test/fixture',
85+
path: 'test/fixture/before.md',
86+
contents: fs.readFileSync('./test/fixture/before.md')
87+
}));
88+
89+
stream.end();
90+
});

0 commit comments

Comments
 (0)