Skip to content
Browse files

0.0.5

  • Loading branch information...
1 parent 242a31b commit 5c4d86511b958a874d18211bf6850e8677215cb4 @youurayy committed
Showing with 36 additions and 13 deletions.
  1. +6 −0 README.md
  2. +30 −13 gfms.js
View
6 README.md
@@ -1,5 +1,11 @@
# Github Flavored Markdown Server (GFMS)
+### News
+
+You can now use the option `-a` to tell GFMS to render your documents via the [Github Markdown Rendering API](http://developer.github.com/v3/markdown/). For simplicity, the public access is used, which is limited to 60 requests per hour per an IP address.
+
+If the mode `-a` is not specified, GFMS will render your doc via Github API only when you manually reload it in the browser (and on the first load). This way you are less likely ot hit the hourly API limit, because you will only use the API to check for correctness occasionally. Use `-n` to disable this feature.
+
### (based on Node.js, Express.js, Jade, Stylus, ws-rpc and GFM for JavaScript)
---
View
43 gfms.js
@@ -9,6 +9,9 @@ var argv = require('optimist')
.boolean('a')
.describe('a', 'Render using Github API.')
.alias('a', 'api')
+ .boolean('n')
+ .describe('n', 'Disable usage of Github API when the doc is manually reloaded.')
+ .alias('n', 'no-api-on-reload')
.argv;
var express = require('express');
@@ -116,31 +119,37 @@ app.get('*', function(req, res, next) {
if(!watched[dir]) {
fs.watchFile(dir, { interval: 500 }, function(curr, prev) {
if(curr.mtime.getTime() !== prev.mtime.getTime()) {
+
console.log('file ' + dir + ' has changed');
- wss.message('update', { update: dir, content: markdown(fs.readFileSync(dir, 'utf8')) });
+
+ renderFile(dir, argv.a, _x(console.log, false, function(err, rendered) {
+ wss.message('update', { update: dir, content: err || rendered });
+ }));
}
});
watched[dir] = true;
}
- var contents = fs.readFileSync(dir, 'utf8');
- var func = argv.a ? renderWithGithub : renderWithShowdown;
-
- func(contents, _x(next, true, function(err, res) {
-
+ renderFile(dir, argv.a || argv.b, _x(next, true, function(err, rendered) {
res.render('file', {
- file: res,
+ file: rendered,
title: basename(dir),
styles: styles,
fullname: dir
});
-
}));
+
}
else
return next();
});
+function renderFile(file, api, cb) { // cb(err, res)
+ var contents = fs.readFileSync(file, 'utf8');
+ var func = api ? renderWithGithub : renderWithShowdown;
+ func(contents, _x(cb, true, cb));
+}
+
function renderWithShowdown(contents, cb) { // cb(err, res)
var res = markdown(contents);
cb(null, res);
@@ -149,7 +158,7 @@ function renderWithShowdown(contents, cb) { // cb(err, res)
function renderWithGithub(contents, cb) { // cb(err, res)
var opts = {
method: 'post',
- url: 'https://github.com/markdown',
+ url: 'https://api.github.com/markdown',
json: {
text: contents,
mode: 'markdown'
@@ -158,6 +167,7 @@ function renderWithGithub(contents, cb) { // cb(err, res)
};
request(opts, _x(cb, true, function(err, res, body) {
+ console.log('remaining API requests: %d', res.headers['x-ratelimit-remaining']);
cb(null, body);
}));
}
@@ -169,19 +179,26 @@ process.on('SIGINT', function() {
console.log('Getting .css links from Github...');
+if(!argv.a && !argv.n)
+ argv.b = true;
+
request('http://www.github.com', function(err, res, body) {
if(err || res.statusCode != 200)
throw 'Cannot load .css links from Github';
-
+
var m, re = /<link href="(.+?)" media="screen" rel="stylesheet" type="text\/css" \/>/g;
while(m = re.exec(body))
styles.push(m[1]);
-
+
if(!styles.length)
throw 'Cannot parse .css links from Github';
-
- server.listen(argv.p, argv.h);
+
+ if(argv.a)
+ console.log('Using Github API to render markdown for all updates.');
+ else if(argv.b)
+ console.log('Using Github API to render markdown for manual reload updates.');
+ server.listen(argv.p, argv.h);
console.log('GFMS serving ' + process.cwd() + ' at http://' + argv.h + ':' + argv.p + '/ - press CTRL+C to exit.');
});

0 comments on commit 5c4d865

Please sign in to comment.
Something went wrong with that request. Please try again.