Skip to content

Commit

Permalink
Handle multiple input files - close #162
Browse files Browse the repository at this point in the history
  • Loading branch information
Cimon Lucas (LCM) authored and Cimon Lucas (LCM) committed Nov 8, 2021
1 parent f920342 commit 3ff3533
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .verb.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Assuming you want to add a TOC to README.md:
```
Usage: markdown-toc [options] <input>
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 <input> file directly, injecting the TOC at <!-- toc -->;
-i: Edit the <input> files directly, injecting the TOC at <!-- toc -->;
(Without this flag, the default is to print the TOC to stdout.)
--json: Print the TOC in JSON format
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ $ npm install --save markdown-toc
```
Usage: markdown-toc [options] <input>
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 <input> file directly, injecting the TOC at <!-- toc -->;
-i: Edit the <input> files directly, injecting the TOC at <!-- toc -->;
(Without this flag, the default is to print the TOC to stdout.)
--json: Print the TOC in JSON format
Expand Down
35 changes: 18 additions & 17 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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> ',
'',
' 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 <input> file directly, injecting the TOC at <!-- toc -->',
' -i: Edit the <input> files directly, injecting the TOC at <!-- toc -->',
' (Without this flag, the default is to print the TOC to stdout.)',
'',
' --json: Print the TOC in JSON format',
Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 40 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
}

0 comments on commit 3ff3533

Please sign in to comment.