Last active
August 29, 2015 14:19
-
-
Save esnya/7bab1f995d1eb534189a to your computer and use it in GitHub Desktop.
markdown ノート用 gulpfile.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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