-
Notifications
You must be signed in to change notification settings - Fork 2
/
newPage
43 lines (33 loc) · 1.02 KB
/
newPage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/local/bin/node
'use strict'
var fs = require('fs')
var prompt = require('prompt')
var path = require('path');
var mkdirp = require('mkdirp');
var _ = require('underscore');
var partialRight = require('jsutils').partialRight;
var editor = require('editor');
function trim(str){
return _.isString(str) ? str.trim() : str;
}
var trimEach = partialRight(_.map, trim);
prompt.message = 'Page';
prompt.start();
prompt.get(['author','title', 'folder', 'summary', 'tags'], function(err, data){
data.date = new Date();
if(data.tags)
data.tags = trimEach(data.tags.split(','));
var dir = path.join(__dirname, 'pages', data.folder);
var file = path.join(dir, data.title.replace(/[^\w\d\s]/g, '').replace(/\s/g,'-').toLowerCase());
mkdirp(dir, function (){
var mkd = file + '.mkd';
fs.exists(mkd, function(exists){
if(exists)
return editor(mkd);
fs.writeFile(file + '.json', JSON.stringify(data, null, '\t'), function(err){
fs.writeFile(mkd, '');
editor(mkd);
});
});
});
});