Skip to content

Commit ea7e404

Browse files
committed
Removed extra logs from stdOut
1 parent f76b8a6 commit ea7e404

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

doctoc.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ function cleanPath(path) {
1717
}
1818

1919
function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut) {
20-
console.log('\n==================\n');
20+
(stdOut) ? console.error('\n==================\n') : console.log('\n==================\n')
2121

2222
var transformed = files
2323
.map(function (x) {
2424
var content = fs.readFileSync(x.path, 'utf8')
25-
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix);
25+
, result = transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut);
2626
result.path = x.path;
2727
return result;
2828
});
@@ -41,9 +41,7 @@ function transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPref
4141
});
4242

4343
changed.forEach(function (x) {
44-
if (stdOut) {
45-
console.log('==================\n\n"%s" was not updated.', x.path)
46-
} else {
44+
if (!stdOut) {
4745
console.log('"%s" will be updated.', x.path);
4846
fs.writeFileSync(x.path, x.data, 'utf8');
4947
}
@@ -107,14 +105,26 @@ for (var i = 0; i < argv._.length; i++) {
107105
, stat = fs.statSync(target)
108106

109107
if (stat.isDirectory()) {
110-
console.log ('\nDocToccing "%s" and its sub directories for %s.', target, mode);
108+
if (!stdOut) {
109+
console.log('\nDocToccing "%s" and its sub directories for %s.', target, mode);
110+
} else {
111+
console.error('\nDocToccing "%s" and its sub directories for %s.', target, mode);
112+
}
111113
files = file.findMarkdownFiles(target);
112114
} else {
113-
console.log ('\nDocToccing single file "%s" for %s.', target, mode);
115+
if (!stdOut) {
116+
console.log('\nDocToccing single file "%s" for %s.', target, mode);
117+
} else {
118+
console.error('\nDocToccing single file "%s" for %s.', target, mode);
119+
}
114120
files = [{ path: target }];
115121
}
116122

117123
transformAndSave(files, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut);
118124

119-
console.log('\nEverything is OK.');
125+
if (!stdOut) {
126+
console.log('\nEverything is OK.');
127+
} else {
128+
console.error('\nEverything is OK.');
129+
}
120130
}

lib/transform.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ function getLinesToToc (lines, currentToc, info) {
9898
}
9999

100100
// Use document context as well as command line args to infer the title
101-
function determineTitle(title, notitle, lines, info) {
101+
function determineTitle(title, notitle, lines, info, stdOut) {
102102
var defaultTitle = '**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*';
103103

104-
if (notitle) return '';
104+
if (notitle || stdOut) return '';
105105
if (title) return title;
106106
return info.hasStart ? lines[info.startIdx + 2] : defaultTitle;
107107
}
108108

109-
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix) {
109+
exports = module.exports = function transform(content, mode, maxHeaderLevel, title, notitle, entryPrefix, stdOut) {
110110
mode = mode || 'github.com';
111111
entryPrefix = entryPrefix || '-';
112112

@@ -116,7 +116,7 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, tit
116116
var lines = content.split('\n')
117117
, info = updateSection.parse(lines, matchesStart, matchesEnd)
118118

119-
var inferredTitle = determineTitle(title, notitle, lines, info);
119+
var inferredTitle = determineTitle(title, notitle, lines, info, stdOut);
120120

121121
var currentToc = info.hasStart && lines.slice(info.startIdx, info.endIdx + 1).join('\n')
122122
, linesToToc = getLinesToToc(lines, currentToc, info);
@@ -137,18 +137,17 @@ exports = module.exports = function transform(content, mode, maxHeaderLevel, tit
137137
// 4 spaces required for proper indention on Bitbucket
138138
var indentation = (mode === 'bitbucket.org' || mode === 'gitlab.com') ? ' ' : ' ';
139139

140-
var toc =
141-
inferredTitle
142-
+ '\n\n'
143-
+ linkedHeaders
144-
.map(function (x) {
140+
var toc = linkedHeaders.map(function (x) {
145141
var indent = _(_.range(x.rank - lowestRank))
146142
.reduce(function (acc, x) { return acc + indentation; }, '');
147143

148144
return indent + entryPrefix + ' ' + x.anchor;
149145
})
150146
.join('\n')
151-
+ '\n';
147+
148+
if (!stdOut) {
149+
toc = inferredTitle + '\n\n' + toc + '\n';
150+
}
152151

153152
var wrappedToc = start + '\n' + toc + '\n' + end;
154153

0 commit comments

Comments
 (0)