From aaaea76b67fa3425d51d4bee4eccf7dcf571c78f Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Mon, 8 Nov 2021 20:28:21 +0100 Subject: [PATCH 1/8] Handle multiple input files - close #162 --- .verb.md | 4 ++-- README.md | 4 ++-- cli.js | 35 ++++++++++++++++++----------------- test/test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/.verb.md b/.verb.md index bdd7611..6075df0 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 files to parse for table of contents, or "-" to read from stdin. - -i: Edit the file directly, injecting the TOC at ; + -i: Edit the files 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..9cc0e82 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 files to parse for table of contents, or "-" to read from stdin. - -i: Edit the file directly, injecting the TOC at ; + -i: Edit the files 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..db1fcb2 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 files to parse for table of contents,', ' or "-" to read from stdin.', '', - ' -i: Edit the file directly, injecting the TOC at ', + ' -i: Edit the files 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..c12dc59 100644 --- a/test/test.js +++ b/test/test.js @@ -427,3 +427,43 @@ 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)`); + }); + }); + }); +}); + +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; + } + }; +} From b468318f2b81cb349e9762e5f4ddcf274cadff54 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:01 +0100 Subject: [PATCH 2/8] Update .verb.md Co-authored-by: Maksim Beliaev --- .verb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verb.md b/.verb.md index 6075df0..f30b611 100644 --- a/.verb.md +++ b/.verb.md @@ -9,7 +9,7 @@ Assuming you want to add a TOC to README.md: ``` Usage: markdown-toc [options] - input: The Markdown files 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 files directly, injecting the TOC at ; From 153878499971815c6cc4d907ce7f834b55899c47 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:06 +0100 Subject: [PATCH 3/8] Update README.md Co-authored-by: Maksim Beliaev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9cc0e82..45bc9e9 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ $ npm install --save markdown-toc ``` Usage: markdown-toc [options] - input: The Markdown files 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 files directly, injecting the TOC at ; From 2a69a63e98e47012f5c5a24843fa0820ad1d89da Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:12 +0100 Subject: [PATCH 4/8] Update README.md Co-authored-by: Maksim Beliaev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 45bc9e9..df51a90 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Usage: markdown-toc [options] input: The Markdown file(s) to parse for table of contents, or "-" to read from stdin. - -i: Edit the files 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 From 866a46e4171bf4de13b466e16df9e5c0e42446c5 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:18 +0100 Subject: [PATCH 5/8] Update cli.js Co-authored-by: Maksim Beliaev --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index db1fcb2..0458e54 100755 --- a/cli.js +++ b/cli.js @@ -19,7 +19,7 @@ if (args._.length === 0) { ' input: The Markdown files to parse for table of contents,', ' or "-" to read from stdin.', '', - ' -i: Edit the files 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', From 8cdcd82174a20f93b5cd494d056dc65972101478 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:24 +0100 Subject: [PATCH 6/8] Update .verb.md Co-authored-by: Maksim Beliaev --- .verb.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.verb.md b/.verb.md index f30b611..1229b49 100644 --- a/.verb.md +++ b/.verb.md @@ -12,7 +12,7 @@ Usage: markdown-toc [options] input: The Markdown file(s) to parse for table of contents, or "-" to read from stdin. - -i: Edit the files 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 From df4b2a5763718d479d0b8ab9b77dfd0cd75b9911 Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:30 +0100 Subject: [PATCH 7/8] Update cli.js Co-authored-by: Maksim Beliaev --- cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli.js b/cli.js index 0458e54..3a7d3a0 100755 --- a/cli.js +++ b/cli.js @@ -16,7 +16,7 @@ if (args._.length === 0) { console.error([ 'Usage: markdown-toc [options] ', '', - ' input: The Markdown files 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(s) directly, injecting the TOC at ', From 10f70b7deb3f2641ed4a9e79c699f2922fb53edd Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:49:57 +0100 Subject: [PATCH 8/8] Update test/test.js Co-authored-by: Maksim Beliaev --- test/test.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/test.js b/test/test.js index c12dc59..003c6cb 100644 --- a/test/test.js +++ b/test/test.js @@ -439,13 +439,15 @@ describe('cli.js', function() { 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)`); + 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')); }); }); });