@@ -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
7576function output ( parsed ) {
0 commit comments