diff --git a/README.md b/README.md index 661fc6e..3561a7f 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ Server Started 127.0.0.1:8000 $ markline server -p 80 data.md Server Started 127.0.0.1:80 +$ markline server -w data.md +Server Started 127.0.0.1:8000 + $ markline build data.md $ markline build data.md --dist _site diff --git a/bin/markline b/bin/markline index 3736152..bc51866 100755 --- a/bin/markline +++ b/bin/markline @@ -10,8 +10,7 @@ commander .option('build', 'build markdown to markline page.') .option('server', 'preview markdown to markline page.') .option('-p, --port [port]', 'server port for preview markline.') - // TODO: watch markdown file, auto reload preview page. - //.option('-w, --watch', 'watch markdown file change, and auto reload for preview markline.') + .option('-w, --watch', 'watch markdown file change, and auto reload for preview markline.') .option('--dist [dist]', 'build markdown file to markline static site. default is `dist` in current working directory.') .parse(process.argv); diff --git a/index.js b/index.js index 4782ce5..fcba8c3 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,9 @@ var path = require("path"); var gulp = require("gulp"); var replace = require("gulp-replace"); var del = require("del"); +var Event = require("events").EventEmitter; + +var evt = new Event(); var DEFAULT_DIST = "dist"; var DEFAULT_PORT = 8000; @@ -35,6 +38,7 @@ function build(cwd, options){ function server(cwd, options){ var port = options.port || DEFAULT_PORT; var encode = options.encode || DEFAULT_ENCODE; + var watch = options.watch || false; var fileName = options.args.join(""); if (!fs.existsSync(fileName)) { @@ -42,15 +46,10 @@ function server(cwd, options){ return; } - var http = require('http').createServer(function (req, res) { + var app = require('http').createServer(function (req, res) { var url = req.url; - if (url === "/") { - fs.readFile(path.join(__dirname, "template", "index.html"), encode, function(err, html){ - res.writeHead(200, {'Content-Type': 'text/html'}); - res.end(html.replace("{FILE_NANE}", fileName)); - }); - } else if (url === "/" + fileName) { + if (url === "/" + fileName) { fs.readFile(path.join(cwd, fileName), encode, function(err, markdown){ res.writeHead(200, {'Content-Type': 'text/markdown'}); res.end(markdown); @@ -64,7 +63,7 @@ function server(cwd, options){ }; if (url === "/") { - url = "index.html"; + url = "/index.html"; } var ext = url.split("."); @@ -75,7 +74,21 @@ function server(cwd, options){ fs.readFile(filePath, encode, function(err, html){ res.writeHead(200, {'Content-Type': MIME_TYPE[ext]}); - res.end(html.replace("{FILE_NANE}", fileName)); + if (url === "/index.html") { + html = html.replace('{FILE_NANE}', fileName); + + if (watch) { + html = html.replace('', [ + '', + '' + ].join('\n') + ); + } + } + res.end(html); }); } else { @@ -87,6 +100,20 @@ function server(cwd, options){ }).listen(port).on("error", function(error){ console.error(error); }); + + if (watch){ + var io = require('socket.io')(app); + io.sockets.on('connection', function(socket) { + socket.emit('hello', {message: 'markline'}); + + fs.watch(fileName, function(event, fileName) { + if (!socket.disconnected) { + socket.emit('reload', {message: fileName}); + } + }); + }); + } + console.log("Server Started 127.0.0.1:" + port); } diff --git a/package.json b/package.json index 922e8af..9928b94 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "commander": "~1.1.1", "del": "^0.1.3", "gulp": "^3.8.8", - "gulp-replace": "^0.4.0" + "gulp-replace": "^0.4.0", + "socket.io": "^1.1.0" }, "devDependencies": { "mocha": "1.17.1",