Skip to content

Commit 34f5665

Browse files
author
Will O'Brien
committed
Handle empty files
1 parent 651c3f7 commit 34f5665

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ module.exports = function(opts){
99
var title = opts.title || "";
1010

1111
var addToc = function (file){
12-
var withToc = doctoc(file.contents.toString(), null, null, title);
13-
file.contents = new Buffer(withToc.data, 'utf8');
12+
var mdWithToc = doctoc(file.contents.toString(), null, null, title).data;
13+
mdWithToc = mdWithToc || file.contents.toString();
14+
file.contents = new Buffer(mdWithToc, 'utf8');
1415
return file;
1516
};
1617

test/fixture/empty.md

Whitespace-only changes.

test/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,23 @@ it('should allow title change', function (cb) {
4747
stream.end();
4848
});
4949

50+
it('should empty files', function (cb) {
5051

52+
var stream = gdoctoc();
53+
54+
stream.on('data', function (file) {
55+
assert.equal(file.relative, 'empty.md');
56+
assert.equal(file.path, 'test/fixture/empty.md');
57+
assert.equal(file.contents.toString(), fs.readFileSync('./test/fixture/empty.md', 'utf8'));
58+
cb();
59+
});
60+
61+
stream.write(new gutil.File({
62+
cwd: 'test',
63+
base: 'test/fixture',
64+
path: 'test/fixture/empty.md',
65+
contents: fs.readFileSync('./test/fixture/empty.md')
66+
}));
67+
68+
stream.end();
69+
});

0 commit comments

Comments
 (0)