diff --git a/.verb.md b/.verb.md index bdd7611..1229b49 100644 --- a/.verb.md +++ b/.verb.md @@ -9,10 +9,10 @@ Assuming you want to add a TOC to README.md: ``` Usage: markdown-toc [options] - input: The Markdown file to parse for table of contents, + input: The Markdown file(s) to parse for table of contents, or "-" to read from stdin. - -i: Edit the file directly, injecting the TOC at ; + -i: Edit the file(s) directly, injecting the TOC at ; (Without this flag, the default is to print the TOC to stdout.) --json: Print the TOC in JSON format diff --git a/README.md b/README.md index b8766c4..df51a90 100644 --- a/README.md +++ b/README.md @@ -38,10 +38,10 @@ $ npm install --save markdown-toc ``` Usage: markdown-toc [options] - input: The Markdown file to parse for table of contents, + input: The Markdown file(s) to parse for table of contents, or "-" to read from stdin. - -i: Edit the file directly, injecting the TOC at ; + -i: Edit the file(s) directly, injecting the TOC at ; (Without this flag, the default is to print the TOC to stdout.) --json: Print the TOC in JSON format diff --git a/cli.js b/cli.js index 87dd4c0..3a7d3a0 100755 --- a/cli.js +++ b/cli.js @@ -12,14 +12,14 @@ var args = utils.minimist(process.argv.slice(2), { } }); -if (args._.length !== 1) { +if (args._.length === 0) { console.error([ 'Usage: markdown-toc [options] ', '', - ' input: The Markdown file to parse for table of contents,', + ' input: The Markdown file(s) to parse for table of contents,', ' or "-" to read from stdin.', '', - ' -i: Edit the file directly, injecting the TOC at ', + ' -i: Edit the file(s) directly, injecting the TOC at ', ' (Without this flag, the default is to print the TOC to stdout.)', '', ' --json: Print the TOC in JSON format', @@ -54,22 +54,23 @@ if (args.i && args._[0] === '-') { process.exit(1); } -var input = process.stdin; -if (args._[0] !== '-') input = fs.createReadStream(args._[0]); +args._.forEach(function(filepath) { + var input = (filepath === '-') ? process.stdin : fs.createReadStream(filepath); -input.pipe(utils.concat(function(input) { - if (args.i) { - var newMarkdown = toc.insert(input.toString(), args); - fs.writeFileSync(args._[0], newMarkdown); - } else { - var parsed = toc(input.toString(), args); - output(parsed); - } -})); + input.pipe(utils.concat(function(input) { + if (args.i) { + var newMarkdown = toc.insert(input.toString(), args); + fs.writeFileSync(filepath, newMarkdown); + } else { + var parsed = toc(input.toString(), args); + output(parsed); + } + })); -input.on('error', function onErr(err) { - console.error(err); - process.exit(1); + input.on('error', function onErr(err) { + console.error(err); + process.exit(1); + }); }); function output(parsed) { diff --git a/test/test.js b/test/test.js index e93e41b..003c6cb 100644 --- a/test/test.js +++ b/test/test.js @@ -427,3 +427,45 @@ describe('toc.insert', function() { assert.equal(strip(toc.insert(str, { linkify: false })), read('test/expected/insert-no-links.md')); }); }); + +describe('cli.js', function() { + describe('when provided two Markdon files', function() { + var hook; + beforeEach(function(){ + hook = captureStream(process.stdout); + }); + it('should build a ToC for both files"', function() { + process.argv= ["node", "cli.js", "test/fixtures/basic.md", "test/fixtures/levels.md"]; + require('../cli'); + setTimeout(function() { + hook.unhook(); + assert.equal(hook.captured(), [ + '- [AAA](#aaa)', + '- [BBB](#bbb)', + '- [CCC](#ccc)', + '- [DDD](#ddd)- [AAA](#aaa)', + ' * [a.1](#a1)', + ' + [a.2](#a2)', + ' - [a.3](#a3)' + ].join('\n')); + }); + }); + }); +}); + +function captureStream(stream){ + var oldWrite = stream.write; + var buf = ''; + stream.write = function(chunk, encoding, callback){ + buf += chunk.toString(); // chunk is a String or Buffer + oldWrite.apply(stream, arguments); + } + return { + unhook: function unhook() { + stream.write = oldWrite; + }, + captured: function() { + return buf; + } + }; +}