-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
67 lines (56 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
var Dredd = require('dredd');
var express = require('express');
var fs = require('fs');
var markdown = require('marked');
var path = require('path');
var server = module.exports.server = express();
server.use(express.static(__dirname));
server.get('/', function (req, res) {
module.exports.getSpecHtml(function (err, html) {
if (err) {
throw err;
}
res.end(html);
});
});
module.exports.getSpecHtml = function (callback) {
var blueprintPath = require.resolve('./todos.apib');
fs.readFile(blueprintPath, function (err, blueprint) {
if (err) {
return callback(err);
}
var cssPath = require.resolve('github-markdown-css/github-markdown.css');
var body = markdown(blueprint.toString().replace(/^FORMAT.*/, ''));
callback(null, [
'<!doctype html>',
'<html lang="en">',
' <head>',
' <meta charset="utf-8">',
' <title>TodoMVC API Blueprint</title>',
' <link rel="stylesheet" href="' + path.relative(__dirname, cssPath) + '">',
' <style>',
' body { background: #eee; }',
' .markdown-body { background: #fff; width:790px; margin: 30px auto; padding: 30px; }',
' </style>',
' </head>',
' <body>',
' <article class="markdown-body">' + body + '</article>',
' </body>',
'</html>'
].join('\n'));
});
};
module.exports.validate = function (serverUrl, cb) {
if (typeof serverUrl === 'function') {
cb = serverUrl;
serverUrl = 'http://localhost:8080';
}
new Dredd({
blueprintPath: path.join(__dirname, 'todos.apib'),
server: serverUrl,
options: {
hookfiles: path.join(__dirname, 'hooks.js')
}
}).run(cb);
};