Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple input files - close #162 #172

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
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 -->;
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
(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,
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
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 -->;
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
(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,',
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
' 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 -->',
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
' (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)`);
Lucas-C marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
});

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;
}
};
}