forked from ForbesLindesay/promisejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
111 lines (108 loc) · 3.25 KB
/
content.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
111
'use strict';
var fs = require('fs');
var createMarkdown = require('markdown-it');
var Plugin = require('markdown-it-regexp');
module.exports = function getContent(url, options) {
function read(filename) {
var filePath = __dirname + '/translations/' + options.locale + filename
var exists = fs.existsSync(filePath)
if (!exists) {
var fallbackLocale = 'en'
var fallbackFilePath = __dirname + '/translations/' + fallbackLocale + filename
filePath = fallbackFilePath
}
return fs.readFileSync(
filePath,
'utf8'
).trim();
}
var src;
try {
src = read('/pages' + url + '.md');
} catch (ex) {
if (ex.code !== 'ENOENT') throw ex;
src = read('/pages' + url + '/index.md');
}
src = src.replace(/\$([A-Za-z\.\-\_]+)\$/g, function (_, path) {
return path.split('.').reduce(function (obj, part) {
return obj[part];
}, options);
});
function render(src, renderOptions) {
var pluginResults = {};
var pluginResultID = 0;
var plugins = [];
function preparePlugins(src) {
plugins.forEach(function (plugin) {
src = src.replace(plugin.regexp, function (_) {
var id = 'pluginplaceholdervalue' + (pluginResultID++) + 'pluginplaceholdervalue';
pluginResults[id] = plugin.replacer.apply(this, arguments);
var end = '';
while (end.length < (_.length - _.trim().length)) {
end += '\n';
}
return id + end;
});
});
return src;
}
function applyPlugins(src) {
var oldSrc = '';
while (src !== oldSrc) {
oldSrc = src;
src = src.replace(/\<p\>\s*(pluginplaceholdervalue\d+pluginplaceholdervalue)\s*\<\/p\>/g, '$1');
src = src.replace(/pluginplaceholdervalue\d+pluginplaceholdervalue/g, function (id) {
return pluginResults[id];
});
}
return src;
}
function plugin(regexp, replacer) {
plugins.push({regexp: regexp, replacer: replacer});
}
plugin(
/\:([a-z\-]+)((?:\n(?: .*)?)*)/g,
function (_, name, content) {
return require('./components/' + name)({
content: content.split('\n').map(function (line) {
return line.substr(line.length >= 2 ? 2 : 0);
}).join('\n').trim()
}, {
read: read,
render: render
});
}
);
plugin(
/\@([a-z\-]+)(\(.*\))?((?:\n(?: .*)?)*)/g,
function (_, name, attrs, content) {
return require('./components/' + name)({
attrs: attrs,
content: (
content.split('\n').map(function (line) {
return line.substr(line.length >= 2 ? 2 : 0);
}).join('\n').trim()
)
}, {
read: read,
render: render
});
}
);
plugin(
/(\#+)\[([A-Za-z\_]+)\] (.*)/g,
function (_, level, label, content) {
return (
'<h' + level.length + ' id="' + label + '">' +
content +
'</h' + level.length + '>'
);
}
);
var md = createMarkdown();
return applyPlugins(md[renderOptions && renderOptions.block ? 'render' : 'renderInline'](preparePlugins(src)));
}
options.content = render(src, {block: true});
options.read = read;
return options;
}