Skip to content

Commit

Permalink
feat(chapters): exclude empty directories from chapters.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanyf committed Sep 11, 2016
1 parent c608187 commit bde0f47
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bin/cmd.js
Expand Up @@ -16,6 +16,12 @@ const argv = require('yargs')
describe: 'output directory',
type: 'string'
})
.option('output', {
alias: 's',
default: 'site',
describe: 'site name',
type: 'string'
})
.option('debug', {
default: false,
describe: 'debug mode',
Expand Down
28 changes: 28 additions & 0 deletions lib/chapters.js
Expand Up @@ -39,6 +39,31 @@ function mkFileHandler(prefix) {
};
}

function excludeEmptyDirectory(dirArr) {
let hasEmptyDirectory = false;
dirArr = dirArr.filter(function (d, i, arr) {
if (i === (arr.length - 1)) {
if (d.substr(-1) === path.sep) {
hasEmptyDirectory = true;
return false;
}
return true;
}
if (d.substr(-3).toLowerCase() === '.md') {
return true;
}
if (arr[i + 1].indexOf(d) !== 0) {
hasEmptyDirectory = true;
return false;
}
return true;
});
if (hasEmptyDirectory) {
dirArr = excludeEmptyDirectory(dirArr);
}
return dirArr;
}

function chapters(option) {
log.success('[BEGIN] handle chapters file');

Expand Down Expand Up @@ -100,6 +125,9 @@ function chapters(option) {
return false;
});

/* exclude empty directories from doc_items */
doc_items = excludeEmptyDirectory(doc_items);

/*
if (doc_items.length === 0) {
log.error('There is NO valid markdown files in the doc directory');
Expand Down
1 change: 1 addition & 0 deletions lib/config.js
Expand Up @@ -41,6 +41,7 @@ function config(option) {
// merge option into config
if (option.dir) cfg.dir = option.dir;
if (option.output) cfg.output = option.output;
if (option.site) cfg.site = option.site;
debug('config object %o', cfg);

// If config file doesn't exist, copy the defalut
Expand Down
6 changes: 6 additions & 0 deletions loppo.yml.default
Expand Up @@ -3,3 +3,9 @@ dir: docs

# output directory of generated documents
output: dist

# site theme
theme: oceandeep

# site name
site: Documents
42 changes: 42 additions & 0 deletions test/chapters.test.js
Expand Up @@ -56,6 +56,48 @@ test(
}
);

test(
test_title +
'exclude empty directory',
function (t) {
const TEST_PATH = path.resolve(__dirname, './fixture/chapters/exclude-empty-directory');
process.chdir(TEST_PATH);
const result = chapters({ dir: 'docs' });
t.equal(result.chapters[0]['a.md'], 'A');
t.equal(result.chapters.length, 1);
t.ok(fs.existsSync(path.join(TEST_PATH, CHAPTERS_FILE)));
const content = yaml.load(fs.readFileSync(path.join(TEST_PATH, CHAPTERS_FILE), 'utf8'));
t.equal(content[0]['a.md'], 'A');
t.equal(content.length, 1);
t.end();
fs.unlinkSync(path.join(TEST_PATH, CHAPTERS_FILE));
}
);

test(
test_title +
'exclude complex empty directory',
function (t) {
const TEST_PATH = path.resolve(__dirname, './fixture/chapters/exclude-complex-empty-directory');
process.chdir(TEST_PATH);
const result = chapters({ dir: 'docs' });
t.equal(result.chapters[0]['a.md'], 'A');
t.equal(result.chapters[1]['images/'], 'images');
t.equal(result.chapters[2]['images/dir3/'], 'dir3');
t.equal(result.chapters[3]['images/dir3/b.md'], 'B');
t.equal(result.chapters.length, 4);
t.ok(fs.existsSync(path.join(TEST_PATH, CHAPTERS_FILE)));
const content = yaml.load(fs.readFileSync(path.join(TEST_PATH, CHAPTERS_FILE), 'utf8'));
t.equal(content[0]['a.md'], 'A');
t.equal(content[1]['images/'], 'images');
t.equal(content[2]['images/dir3/'], 'dir3');
t.equal(content[3]['images/dir3/b.md'], 'B');
t.equal(content.length, 4);
t.end();
fs.unlinkSync(path.join(TEST_PATH, CHAPTERS_FILE));
}
);

test(
test_title +
'generating chapters.yml filter',
Expand Down
@@ -0,0 +1 @@
# A
@@ -0,0 +1 @@
# B
1 change: 1 addition & 0 deletions test/fixture/chapters/exclude-empty-directory/docs/a.md
@@ -0,0 +1 @@
# A

0 comments on commit bde0f47

Please sign in to comment.