Skip to content

Instantly share code, notes, and snippets.

@deptno
Last active August 29, 2015 14:19
Show Gist options
  • Save deptno/722b409c38586494b14c to your computer and use it in GitHub Desktop.
Save deptno/722b409c38586494b14c to your computer and use it in GitHub Desktop.
create .md file, post to jekyll with metadata template in terminal
#!/usr/bin/env node
var __config = { /* author: [email protected] */
path_root: '/Users/deptno/Workspace/github/deptno.github.io',
path_post: '_posts',
cmd_editor: 'mvim',
ext: '.md'
};
if (process.argv.length < 3) {
console.log(' * configure: $ ' + __config.cmd_editor + ' ' + process.argv[1]);
console.log(' modify __config variable');
console.log(' * usage: "post [posting type] [post-name]"');
console.log(' * example: "post blog new-article-example"');
process.exit(0);
}
var path = require('path');
var fs = require('fs');
var childProcess = require('child_process');
var type = process.argv[2];
var folder = type === 'article' ? 'article' : 'blog';
var title = process.argv[3];
var date = new Date().toISOString();
var pathWorking = path.join(__config.path_root, __config.path_post, folder);
var rxpFilter = /([0-9]+-[0-9]+-[0-9]+)-(.*)/;
var files = [];
var keywords = {
ls: function() {
process.chdir(pathWorking);
console.log(childProcess.execSync('ls').toString());
},
rm: function(files) {
try {
childProcess.execSync('rm ' + files.map(function(file) {
return path.join(pathWorking, '*' + file + __config.ext);
}));
this.ls();
} catch(ex) { ; }
}
};
__config.path_post = path.join(__config.path_post, folder);
if (!fs.existsSync(path.join(__config.path_root, __config.path_post))) {
console.info(path.join(__config.path_root, __config.path_post) + ' does not found');
process.exit(1);
}
if (keywords[title]) {
keywords[title](process.argv.slice(4));
process.exit(0);
}
try {
var result;
files = childProcess
.execSync('ls ' + path.join(pathWorking, '*' + title + __config.ext))
.toString()
.split('\n')
.filter(function(fileName) {
result = rxpFilter.exec(path.basename(fileName));
return result && result.length === 3;
});
} catch(ex) { ; }
var filename;
var content;
if (files.length >= 1) {
filename = files[0];
console.log('= modify, filename: ' + filename);
content = fs.readFileSync(filename).toString().replace(/modified:.*/, 'modified: ' + date);
} else {
filename = path.join(pathWorking, date.substring(0, 10) + '-' + title + __config.ext);
console.log('= create, filename: ' + filename);
content = [
'---',
'layout: post',
'title: ' + title,
'modified:',
'categories: ' + type,
'excerpt:',
'tags: []',
'images:',
'feature:',
'date: ' + date,
'---'
].join(require('os').EOL);
}
fs.writeFileSync(filename, content);
childProcess.spawn(__config.cmd_editor, [filename], {
stdio: 'inherit'
}).on('exit', function(e, code) {
process.chdir(__config.path_root);
console.info(childProcess.execSync('git add ' + path.join(__config.path_post, path.basename(filename))).toString());
process.exit(0);
});;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment