Skip to content

Instantly share code, notes, and snippets.

@esnya
Last active August 29, 2015 14:19
Show Gist options
  • Save esnya/7bab1f995d1eb534189a to your computer and use it in GitHub Desktop.
Save esnya/7bab1f995d1eb534189a to your computer and use it in GitHub Desktop.
markdown ノート用 gulpfile.js
'use strict';
var gulp = require('gulp');
var pandoc = require('gulp-pandoc');
var webserver = require('gulp-webserver');
gulp.task('html', function () {
gulp.src('src/*.md')
.pipe(pandoc({
from: 'markdown',
to: 'html5',
args: ['--mathml', '-c', 'style.css', '-s'],
ext: '.html'
}))
.pipe(gulp.dest('html'));
});
gulp.task('webserver', ['html'], function () {
gulp.src('html')
.pipe(webserver({
livereload: true,
middleware: function (req, res, next) {
if (req.url == '/') {
var fs = require('fs');
fs.readdir('html', function (err, files) {
if (err) throw err;
res.write('<!DOCTYPE html><html><style>dt{float: left;}dt:after{content:":";margin-left:0.25em;margin-right:0.25em;}</style><body><dl>');
files.map(function (file) {
return ('' + file).match(/[0-9]+\.html$/);
}).filter(function (file) {
return file && fs.statSync('html/' + file).isFile() && /\.html$/.test(file);
}).forEach(function (file) {
res.write('<dt><a href="' + file + '">' + ('' + file).match(/^(.*)\.html$/)[1] + '</a></dt>');
var contents = fs.readFileSync('html/' + file);
res.write('<dd>' + ('' + contents).match(/<title>(.*)<\/title>/)[1] + '</dd>');
});
res.end('</dl></body><html>');
});
} else {
next();
}
},
open: true,
port: 9767
}));
});
gulp.task('watch', ['webserver'], function () {
gulp.watch('src/*.md', ['html']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment