Skip to content

Commit

Permalink
Merge pull request jonschlinkert#95 from sapegin/cli-options
Browse files Browse the repository at this point in the history
Pass options via CLI
  • Loading branch information
doowb committed Sep 19, 2017
2 parents 9312cdc + d7a0ce3 commit 1ede564
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
26 changes: 20 additions & 6 deletions .verb.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
## CLI

```
Usage: markdown-toc [--json] [-i] <input>
Usage: markdown-toc [options] <input>
input: The markdown file to parse for table of contents,
or "-" to read from stdin.
input: The Markdown file to parse for table of contents,
or "-" to read from stdin.
--json: Print the TOC in json format
-i: Edit the <input> file directly, injecting the TOC at <!-- toc -->
(Without this flag, the default is to print the TOC to stdout.)
-i: Edit the <input> file 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
--append: Append a string to the end of the TOC
--bullets: Bullets to use for items in the generated TOC
(Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")
(Default is "*".)
--maxdepth: Use headings whose depth is at most maxdepth
(Default is 6.)
--no-firsth1: Include the first h1-level heading in a file
--no-stripHeadingTags: Do not strip extraneous HTML tags from heading
text before slugifying
```

## Highights
Expand Down
37 changes: 28 additions & 9 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@ var fs = require('fs');
var toc = require('./index.js');
var utils = require('./lib/utils');
var args = utils.minimist(process.argv.slice(2), {
boolean: ['i', 'json']
boolean: ['i', 'json', 'firsth1', 'stripHeadingTags'],
string: ['append', 'bullets'],
default: {
firsth1: true,
stripHeadingTags: true
}
});

if (args._.length !== 1) {
console.error([
'Usage: markdown-toc [--json] [-i] <input> ',
'Usage: markdown-toc [options] <input> ',
'',
' input: The Markdown file to parse for table of contents,',
' or "-" to read from stdin.',
'',
' -i: Edit the <input> file 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',
'',
' --append: Append a string to the end of the TOC',
'',
' --bullets: Bullets to use for items in the generated TOC',
' (Supports multiple bullets: --bullets "*" --bullets "-" --bullets "+")',
' (Default is "*".)',
'',
' input: The markdown file to parse for table of contents,',
' or "-" to read from stdin.',
' --maxdepth: Use headings whose depth is at most maxdepth',
' (Default is 6.)',
'',
' --json: Print the TOC in json format',
' --no-firsth1: Include the first h1-level heading in a file',
'',
' -i: Edit the <input> file directly, injecting the TOC at <!-- toc -->',
' (Without this flag, the default is to print the TOC to stdout.)'
' --no-stripHeadingTags: Do not strip extraneous HTML tags from heading',
' text before slugifying'
].join('\n'));
process.exit(1);
}
Expand All @@ -37,10 +56,10 @@ if (args._[0] !== '-') input = fs.createReadStream(args._[0]);

input.pipe(utils.concat(function(input) {
if (args.i) {
var newMarkdown = toc.insert(input.toString());
var newMarkdown = toc.insert(input.toString(), args);
fs.writeFileSync(args._[0], newMarkdown);
} else {
var parsed = toc(input.toString());
var parsed = toc(input.toString(), args);
output(parsed);
}
}));
Expand Down

0 comments on commit 1ede564

Please sign in to comment.