-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
110 lines (96 loc) · 2.87 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
var assert = require('assert');
var copy = require('copy-to');
var path = require('path');
var fs = require('co-fs');
var utility = require('utility');
var cachePages = {};
var cacheLayout;
var defaultOpts = {
cache: false,
titleHolder: '{TITLE}',
bodyHolder: '{BODY}',
indexName: 'index',
baseUrl: '/'
};
module.exports = function (options) {
assert(options && options.root, 'options.root required');
copy(defaultOpts).to(options);
options.baseUrl = options.baseUrl.replace(/\/$/, '') + '/';
options.layout = options.layout || path.join(options.root, 'layout.html');
// support custom markdown render
if (typeof options.render !== 'function') {
if (options.remarkableOptions) {
throw new Error('koa-markdown is using markdown-it as default for markdown render, ' +
'please pass `options.mdOptions` instead');
}
var md = require('markdown-it')(options.mdOptions);
options.render = function (content) {
return md.render(content);
};
}
return function* markdown(next) {
if (this.method !== 'GET') {
return yield next;
}
var pathname = this.path;
// get md file path
// index file
if (pathname + '/' === options.baseUrl
|| pathname === options.baseUrl) {
pathname = options.baseUrl + options.indexName;
}
// folder index file
if (pathname.lastIndexOf('/') === (pathname.length - 1) ) {
pathname = pathname + options.indexName;
};
// check if match base url
if (pathname.indexOf(options.baseUrl) !== 0) return yield next;
pathname = pathname.replace(options.baseUrl, '');
pathname = path.join(options.root, pathname + '.md');
// generate html
var html = yield getPage(pathname);
if (html === null) {
return yield next;
}
this.type = 'html';
this.body = html;
};
function* getPage(filepath) {
if (options.cache && filepath in cachePages) {
return cachePages[filepath];
}
var r;
try {
r = yield [getLayout(), getContent(filepath)];
} catch (err) {
if (err.code === 'ENOENT') {
return null;
}
throw err;
}
var layout = r[0];
var content = r[1];
var html = utility.replace(layout, options.titleHolder, content.title);
html = utility.replace(html, options.bodyHolder, content.body);
if (options.cache) {
cachePages[filepath] = html;
}
return html;
}
function* getLayout() {
if (options.cache && cacheLayout) return cacheLayout;
var layout = yield fs.readFile(options.layout, 'utf8');
if (options.cache) cacheLayout = layout;
return layout;
}
function* getContent(filepath) {
var content = yield fs.readFile(filepath, 'utf8');
var title = content.slice(0, content.indexOf('\n')).trim().replace(/^[#\s]+/, '');
var body = options.render(content);
return {
title: title,
body: body
};
}
};