forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
website.js
162 lines (136 loc) · 4.57 KB
/
website.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
Error.stackTraceLimit = Infinity;
const acquit = require('acquit');
const fs = require('fs');
const pug = require('pug');
const pkg = require('./package');
const linktype = require('./docs/helpers/linktype');
const href = require('./docs/helpers/href');
const klass = require('./docs/helpers/klass');
const transform = require('acquit-require');
require('acquit-ignore')();
const markdown = require('marked');
const highlight = require('highlight.js');
markdown.setOptions({
highlight: function(code) {
return highlight.highlight('JavaScript', code).value;
}
});
const tests = [
...acquit.parse(fs.readFileSync('./test/webpack.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/geojson.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/docs/transactions.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/schema.alias.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/model.middleware.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/docs/date.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/es-next/lean.test.es6.js').toString()),
...acquit.parse(fs.readFileSync('./test/es-next/cast.test.es6.js').toString()),
...acquit.parse(fs.readFileSync('./test/es-next/findoneandupdate.test.es6.js').toString()),
...acquit.parse(fs.readFileSync('./test/docs/custom-casting.test.js').toString()),
...acquit.parse(fs.readFileSync('./test/es-next/getters-setters.test.es6.js').toString()),
...acquit.parse(fs.readFileSync('./test/es-next/virtuals.test.es6.js').toString())
];
function getVersion() {
return require('./package.json').version;
}
function getLatestLegacyVersion(startsWith) {
const hist = fs.readFileSync('./History.md', 'utf8').replace(/\r/g, '\n').split('\n');
for (const rawLine of hist) {
const line = (rawLine || '').trim();
if (!line) {
continue;
}
const match = /^\s*([^\s]+)\s/.exec(line);
if (match && match[1] && match[1].startsWith(startsWith)) {
return match[1];
}
}
throw new Error('no match found');
}
// use last release
pkg.version = getVersion();
pkg.latest4x = getLatestLegacyVersion('4.');
pkg.latest38x = getLatestLegacyVersion('3.8');
require('./docs/splitApiDocs');
const filemap = Object.assign({}, require('./docs/source'), require('./docs/tutorials'));
const files = Object.keys(filemap);
const wrapMarkdown = md => `
extends ../layout
append style
link(rel="stylesheet", href="/docs/css/inlinecpc.css")
script(type="text/javascript" src="/docs/js/native.js")
style.
p { line-height: 1.5em }
block content
:markdown
${md.split('\n').map(line => ' ' + line).join('\n')}
`;
const cpc = `
<script>
_native.init("CK7DT53U",{
targetClass: 'native-inline'
});
</script>
<div class="native-inline">
<a href="#native_link#"><span class="sponsor">Sponsor</span> #native_company# — #native_desc#</a>
</div>
`;
function pugify(filename, options, newfile) {
options = options || {};
options.package = pkg;
options.linktype = linktype;
options.href = href;
options.klass = klass;
let contents = fs.readFileSync(filename).toString();
if (options.acquit) {
contents = transform(contents, tests);
}
if (options.markdown) {
const lines = contents.split('\n');
lines.splice(2, 0, cpc);
contents = lines.join('\n');
contents = wrapMarkdown(contents);
newfile = filename.replace('.md', '.html');
}
options.marked = markdown;
options.markedCode = function(v) {
return markdown('```javascript\n' + v + '\n```');
};
options.filename = filename;
options.filters = {
markdown: function(block) {
return markdown(block);
}
};
pug.render(contents, options, function(err, str) {
if (err) {
console.error(err.stack);
return;
}
newfile = newfile || filename.replace('.pug', '.html');
fs.writeFile(newfile, str, function(err) {
if (err) {
console.error('could not write', err.stack);
} else {
console.log('%s : rendered ', new Date, newfile);
}
});
});
}
files.forEach(function(file) {
const filename = __dirname + '/' + file;
pugify(filename, filemap[file]);
if (process.argv[2] === '--watch') {
fs.watchFile(filename, { interval: 1000 }, function(cur, prev) {
if (cur.mtime > prev.mtime) {
pugify(filename, filemap[file]);
}
});
}
});
const _acquit = require('./docs/source/acquit');
const acquitFiles = Object.keys(_acquit);
acquitFiles.forEach(function(file) {
const filename = __dirname + '/docs/acquit.pug';
pugify(filename, _acquit[file], __dirname + '/docs/' + file);
});