Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ node_modules
npm-debug.log
samples
tmp_samples
!test/fixtures/*.log
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ This is useful CI environments where you want to check if your docs are up to da

You can print to stdout by using the `-s` or `--stdout` option.

This option is only applicable when specifying a single filename which doctoc is to run on, if specifying a folder or multiple files the dry run option should be used.

### Only update existing ToC

Use `--update-only` or `-u` to only update the existing ToC. That is, the Markdown files without ToC will be left untouched. It is good if you want to use `doctoc` with `lint-staged`.
Expand Down
15 changes: 13 additions & 2 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';
var processAll = argv.all;
var stdOut = argv.s || argv.stdout
var stdOut = argv.s || argv.stdout || false;
var updateOnly = argv.u || argv['update-only']
var dryRun = argv.d || argv.dryrun || false;

Expand All @@ -125,10 +125,21 @@ else if (minHeaderLevel && minHeaderLevel > 2) { console.error('Min. heading lev

if (maxHeaderLevel && maxHeaderLevel < minHeaderLevel) { console.error('Max. heading level: ' + maxHeaderLevel + ' is less than the defined Min. heading level: ' + minHeaderLevel), printUsageAndExit(true); }

if (argv._.length > 1 && stdout) {
console.log('StdOut can not be used with multiple files/directories. Using dryrun instead.');
stdOut = false;
dryRun = true;
}

for (var i = 0; i < argv._.length; i++) {
var target = cleanPath(argv._[i])
, stat = fs.statSync(target)
, stat = fs.statSync(target);

if (stat.isDirectory() && stdOut){
console.log('StdOut can not be used on a directory. Using dryrun instead.');
stdOut = false;
dryRun = true;
}

if (stat.isDirectory()) {
console.log ('\nDocToccing "%s" and its sub directories for %s.', target, mode);
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/invalid_stdout/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Hello, world!

README to test doctoc with user-specified titles.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of Contents

- [Installation](#installation)
- [API](#api)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Installation
## API
## License
File renamed without changes.
11 changes: 11 additions & 0 deletions test/fixtures/stdout_run_on_directory.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
StdOut can not be used on a directory. Using dryrun instead.

DocToccing "test/fixtures/invalid_stdout" and its sub directories for github.com.

Found readme.md in "test/fixtures/invalid_stdout"

==================

"test/fixtures/invalid_stdout/readme.md" is up to date

Everything is OK.
19 changes: 17 additions & 2 deletions test/transform-stdout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('\nshould print to stdout with --stdout option', function (t) {
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, fs.readFileSync(__dirname + '/fixtures/stdout.log', 'utf8')
, 'spits out the correct table of contents')

t.end()
Expand All @@ -28,9 +28,24 @@ test('\nshould print to stdout with -s option', function (t) {
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, fs.readFileSync(__dirname + '/fixtures/stdout.log', 'utf8')
, 'spits out the correct table of contents')

t.end()
})
})

test('\nshould be a dry run even though the --stdout option provided due to being a directory', function (t) {

exec('node doctoc.js test/fixtures/invalid_stdout --stdout', function (error, stdout, stderr) {
if (error) {
console.error('exec error: ', error);
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout_run_on_directory.log', 'utf8')
, 'spits out the correct logs for stdout on directory')

t.end()
})
})