Skip to content

Commit a6ee89c

Browse files
committed
fix(file.js, doctoc.js): don't match mdx files unless --syntax mdx
1 parent e852602 commit a6ee89c

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

doctoc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ for (var i = 0; i < argv._.length; i++) {
114114

115115
if (stat.isDirectory()) {
116116
console.log ('\nDocToccing "%s" and its sub directories for %s.', target, mode);
117-
files = file.findMarkdownFiles(target);
117+
files = file.findMarkdownFiles(target, syntax);
118118
} else {
119119
console.log ('\nDocToccing single file "%s" for %s.', target, mode);
120120
files = [{ path: target }];

lib/file.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,59 @@ var path = require('path')
22
, fs = require('fs')
33
, _ = require('underscore');
44

5-
var markdownExts = ['.md', '.markdown', '.mdx'];
5+
var markdownExts = ['.md', '.markdown'];
6+
var mdxExts = ['.mdx'];
67
var ignoredDirs = ['.', '..', '.git', 'node_modules'];
78

8-
function separateFilesAndDirs(fileInfos) {
9+
function separateFilesAndDirs(fileInfos, syntax = "md") {;
910
return {
1011
directories : _(fileInfos).filter(function (x) {
1112
return x.isDirectory() && !_(ignoredDirs).include(x.name);
1213
}),
13-
markdownFiles : _(fileInfos).filter(function (x) {
14-
return x.isFile() && _(markdownExts).include(path.extname(x.name));
14+
markdownFiles : _(fileInfos).filter(function (x) {
15+
return x.isFile() && _(syntax === 'mdx'? mdxExts : markdownExts).include(path.extname(x.name));
1516
})
1617
};
1718
}
1819

19-
function findRec(currentPath) {
20+
function findRec(currentPath, syntax) {
2021
function getStat (entry) {
2122
var target = path.join(currentPath, entry),
2223
stat = fs.statSync(target);
2324

24-
return _(stat).extend({
25+
return _(stat).extend({
2526
name: entry,
2627
path: target
2728
});
2829
}
29-
30+
3031
function process (fileInfos) {
31-
var res = separateFilesAndDirs(fileInfos);
32+
var res = separateFilesAndDirs(fileInfos, syntax);
3233
var tgts = _(res.directories).pluck('path');
3334

34-
if (res.markdownFiles.length > 0)
35+
if (res.markdownFiles.length > 0)
3536
console.log('\nFound %s in "%s"', _(res.markdownFiles).pluck('name').join(', '), currentPath);
36-
else
37+
else
3738
console.log('\nFound nothing in "%s"', currentPath);
3839

39-
return {
40+
return {
4041
markdownFiles : res.markdownFiles,
4142
subdirs : tgts
4243
};
4344
}
4445

4546
var stats = _(fs.readdirSync(currentPath)).map(getStat)
4647
, res = process(stats)
47-
, markdownsInSubdirs = _(res.subdirs).map(findRec)
48+
, markdownsInSubdirs = _(res.subdirs).map((subdir)=> findRec(subdir, syntax))
4849
, allMarkdownsHereAndSub = res.markdownFiles.concat(markdownsInSubdirs);
4950

5051
return _(allMarkdownsHereAndSub).flatten();
5152
}
5253

5354
// Finds all markdown files in given directory and its sub-directories
54-
// @param {String } dir - the absolute directory to search in
55-
exports.findMarkdownFiles = function(dir) {
56-
return findRec(dir);
55+
// @param {String } dir - the absolute directory to search in
56+
exports.findMarkdownFiles = function(dir, syntax) {
57+
return findRec(dir, syntax);
5758
};
5859

5960
/* Example:

0 commit comments

Comments
 (0)