Skip to content

Commit aaaea76

Browse files
committed
Handle multiple input files - close #162
1 parent f920342 commit aaaea76

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

.verb.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ Assuming you want to add a TOC to README.md:
99
```
1010
Usage: markdown-toc [options] <input>
1111
12-
input: The Markdown file to parse for table of contents,
12+
input: The Markdown files to parse for table of contents,
1313
or "-" to read from stdin.
1414
15-
-i: Edit the <input> file directly, injecting the TOC at <!-- toc -->;
15+
-i: Edit the <input> files directly, injecting the TOC at <!-- toc -->;
1616
(Without this flag, the default is to print the TOC to stdout.)
1717
1818
--json: Print the TOC in JSON format

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ $ npm install --save markdown-toc
3838
```
3939
Usage: markdown-toc [options] <input>
4040
41-
input: The Markdown file to parse for table of contents,
41+
input: The Markdown files to parse for table of contents,
4242
or "-" to read from stdin.
4343
44-
-i: Edit the <input> file directly, injecting the TOC at <!-- toc -->;
44+
-i: Edit the <input> files directly, injecting the TOC at <!-- toc -->;
4545
(Without this flag, the default is to print the TOC to stdout.)
4646
4747
--json: Print the TOC in JSON format

cli.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ var args = utils.minimist(process.argv.slice(2), {
1212
}
1313
});
1414

15-
if (args._.length !== 1) {
15+
if (args._.length === 0) {
1616
console.error([
1717
'Usage: markdown-toc [options] <input> ',
1818
'',
19-
' input: The Markdown file to parse for table of contents,',
19+
' input: The Markdown files to parse for table of contents,',
2020
' or "-" to read from stdin.',
2121
'',
22-
' -i: Edit the <input> file directly, injecting the TOC at <!-- toc -->',
22+
' -i: Edit the <input> files directly, injecting the TOC at <!-- toc -->',
2323
' (Without this flag, the default is to print the TOC to stdout.)',
2424
'',
2525
' --json: Print the TOC in JSON format',
@@ -54,22 +54,23 @@ if (args.i && args._[0] === '-') {
5454
process.exit(1);
5555
}
5656

57-
var input = process.stdin;
58-
if (args._[0] !== '-') input = fs.createReadStream(args._[0]);
57+
args._.forEach(function(filepath) {
58+
var input = (filepath === '-') ? process.stdin : fs.createReadStream(filepath);
5959

60-
input.pipe(utils.concat(function(input) {
61-
if (args.i) {
62-
var newMarkdown = toc.insert(input.toString(), args);
63-
fs.writeFileSync(args._[0], newMarkdown);
64-
} else {
65-
var parsed = toc(input.toString(), args);
66-
output(parsed);
67-
}
68-
}));
60+
input.pipe(utils.concat(function(input) {
61+
if (args.i) {
62+
var newMarkdown = toc.insert(input.toString(), args);
63+
fs.writeFileSync(filepath, newMarkdown);
64+
} else {
65+
var parsed = toc(input.toString(), args);
66+
output(parsed);
67+
}
68+
}));
6969

70-
input.on('error', function onErr(err) {
71-
console.error(err);
72-
process.exit(1);
70+
input.on('error', function onErr(err) {
71+
console.error(err);
72+
process.exit(1);
73+
});
7374
});
7475

7576
function output(parsed) {

test/test.js

+40
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,43 @@ describe('toc.insert', function() {
427427
assert.equal(strip(toc.insert(str, { linkify: false })), read('test/expected/insert-no-links.md'));
428428
});
429429
});
430+
431+
describe('cli.js', function() {
432+
describe('when provided two Markdon files', function() {
433+
var hook;
434+
beforeEach(function(){
435+
hook = captureStream(process.stdout);
436+
});
437+
it('should build a ToC for both files"', function() {
438+
process.argv= ["node", "cli.js", "test/fixtures/basic.md", "test/fixtures/levels.md"];
439+
require('../cli');
440+
setTimeout(function() {
441+
hook.unhook();
442+
assert.equal(hook.captured(), `- [AAA](#aaa)
443+
- [BBB](#bbb)
444+
- [CCC](#ccc)
445+
- [DDD](#ddd)- [AAA](#aaa)
446+
* [a.1](#a1)
447+
+ [a.2](#a2)
448+
- [a.3](#a3)`);
449+
});
450+
});
451+
});
452+
});
453+
454+
function captureStream(stream){
455+
var oldWrite = stream.write;
456+
var buf = '';
457+
stream.write = function(chunk, encoding, callback){
458+
buf += chunk.toString(); // chunk is a String or Buffer
459+
oldWrite.apply(stream, arguments);
460+
}
461+
return {
462+
unhook: function unhook() {
463+
stream.write = oldWrite;
464+
},
465+
captured: function() {
466+
return buf;
467+
}
468+
};
469+
}

0 commit comments

Comments
 (0)