Skip to content

Commit 6db04b5

Browse files
author
Will O'Brien
committed
init
0 parents  commit 6db04b5

File tree

9 files changed

+235
-0
lines changed

9 files changed

+235
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Will O'Brien
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
gulp-doctoc
2+
==============
3+
4+
Inject a table of contents into your markdown files.
5+
6+
Install
7+
-----------
8+
9+
Install with [npm](https://npmjs.org/package/gulp-doctoc)
10+
11+
```javascript
12+
npm install --save-dev gulp-doctoc
13+
```
14+
15+
Usage
16+
---------
17+
18+
```javascript
19+
var toc = require('gulp-doctoc'),
20+
marked = require('gulp-marked');
21+
22+
gulp.task('markdown', function(){
23+
24+
gulp.src('./**/*.md')
25+
.pipe(toc())
26+
.pipe(marked())
27+
.pipe(gulp.dest('./public/'));
28+
29+
});
30+
31+
```
32+
33+
Thanks
34+
-------
35+
36+
Thanks to [@thlorenz](https://github.com/thlorenz) for [doctoc](https://github.com/thlorenz/doctoc). He did all the hard work.
37+
38+
License
39+
--------
40+
41+
MIT (same as [doctoc](https://github.com/thlorenz))

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
var through = require('through2'),
4+
doctoc = require('doctoc/lib/transform'),
5+
gutil = require('gulp-util');
6+
7+
module.exports = function(opts){
8+
if(opts == null){opts = {};}
9+
var title = opts.title || "";
10+
11+
var addToc = function (file){
12+
var withToc = doctoc(file.contents.toString(), null, null, title);
13+
file.contents = new Buffer(withToc.data, 'utf8');
14+
return file;
15+
};
16+
17+
return through.obj(function(file, enc, cb){
18+
if(file.isNull()) {
19+
this.push(file);
20+
return cb();
21+
}
22+
if(file.isStream()) {
23+
this.emit('error', new gutil.PluginError('gulp-doctoc', 'Streaming not supported'));
24+
return cb();
25+
}
26+
try {
27+
this.push(addToc(file));
28+
} catch (e) {
29+
console.log(e);
30+
this.emit('error', new gutil.PluginError('gulp-doctoc', 'Error adding table of contents', e));
31+
}
32+
return cb();
33+
});
34+
35+
};

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "gulp-doctoc",
3+
"version": "0.1.0",
4+
"description": "Add a table of contents to a markdown file.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/will-ob/gulp-doctoc"
12+
},
13+
"keywords": [
14+
"gulpplugin"
15+
],
16+
"author": "Will O'Brien <[email protected]>",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/will-ob/gulp-doctoc/issues"
20+
},
21+
"dependencies": {
22+
"doctoc": "^0.12.0",
23+
"gulp-util": "~2.2.14",
24+
"through2": "~0.4.1"
25+
},
26+
"devDependencies": {
27+
"mocha": "~1.18.2",
28+
"assert": "~1.1.1",
29+
"gulp-util": "~2.2.14"
30+
}
31+
}

test/fixture/after-w-title.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
7+
8+
- [Hello](#hello)
9+
- [World](#world)
10+
- [Hi](#hi)
11+
12+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
13+
14+
## Hello
15+
16+
### World
17+
18+
### Hi
19+
20+
21+

test/fixture/after.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
- [World](#world)
10+
- [Hi](#hi)
11+
12+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
13+
14+
## Hello
15+
16+
### World
17+
18+
### Hi
19+
20+
21+

test/fixture/before.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
# Article
3+
4+
<!-- START doctoc -->
5+
<!-- END doctoc -->
6+
7+
## Hello
8+
9+
### World
10+
11+
### Hi
12+
13+
14+

test/test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
var assert = require('assert'),
4+
gutil = require('gulp-util'),
5+
gdoctoc = require('../index'),
6+
fs = require('fs');
7+
8+
it('should add TOC to the markdown', function (cb) {
9+
10+
var stream = gdoctoc();
11+
12+
stream.on('data', function (file) {
13+
assert.equal(file.relative, 'before.md');
14+
assert.equal(file.path, 'test/fixture/before.md');
15+
assert.equal(file.contents.toString(), fs.readFileSync('./test/fixture/after.md', 'utf8'));
16+
cb();
17+
});
18+
19+
stream.write(new gutil.File({
20+
cwd: 'test',
21+
base: 'test/fixture',
22+
path: 'test/fixture/before.md',
23+
contents: fs.readFileSync('./test/fixture/before.md')
24+
}));
25+
26+
stream.end();
27+
});
28+
29+
it('should allow title change', function (cb) {
30+
31+
var stream = gdoctoc({title: " "});
32+
33+
stream.on('data', function (file) {
34+
assert.equal(file.relative, 'before.md');
35+
assert.equal(file.path, 'test/fixture/before.md');
36+
assert.equal(file.contents.toString(), fs.readFileSync('./test/fixture/after-w-title.md', 'utf8'));
37+
cb();
38+
});
39+
40+
stream.write(new gutil.File({
41+
cwd: 'test',
42+
base: 'test/fixture',
43+
path: 'test/fixture/before.md',
44+
contents: fs.readFileSync('./test/fixture/before.md')
45+
}));
46+
47+
stream.end();
48+
});
49+
50+

0 commit comments

Comments
 (0)