-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
340 lines (306 loc) · 11.9 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
module.exports = function(options = {}) {
const fs = require('fs-extra');
const gulp = require('gulp');
const findPkgDir = require('find-pkg-dir');
const modulePath = findPkgDir(__dirname);
let config = fs.readJsonSync('./specs.json');
let assets = fs.readJsonSync(modulePath + '/src/asset-map.json');
const katexRules = ['math_block', 'math_inline']
const replacerRegex = /\[\[\s*([^\s\[\]:]+):?\s*([^\]\n]+)?\]\]/img;
const replacerArgsRegex = /\s*,+\s*/;
const replacers = [
{
test: 'insert',
transform: function(path){
if (!path) return '';
return fs.readFileSync(path, 'utf8');
}
}
];
function applyReplacers(doc){
return doc.replace(replacerRegex, function(match, type, args){
let replacer = replacers.find(r => type.trim().match(r.test));
return replacer ? replacer.transform(...args.trim().split(replacerArgsRegex)) : match;
});
}
function normalizePath(path){
return path.trim().replace(/\/$/g, '') + '/';
}
function renderRefGroup(type){
let group = specGroups[type];
if (!group) return '';
let html = Object.keys(group).sort().reduce((html, name) => {
let ref = group[name];
return html += `
<dt id="ref:${name}">${name}</dt>
<dd>
<cite><a href="${ref.href}">${ref.title}</a></cite>.
${ref.authors.join('; ')}; ${ref.rawDate}. <span class="reference-status">Status: ${ref.status}</span>.
</dd>
`;
}, '<dl class="reference-list">')
return `\n${html}\n</dl>\n`;
}
function findKatexDist(){
const relpath = "node_modules/katex/dist";
const paths = [
path.join(process.cwd(), relpath),
path.join(__dirname, relpath),
];
for(const abspath of paths) {
if(fs.existsSync(abspath)) {
return abspath
}
}
throw Error("katex distribution could not be located");
}
try {
var toc;
var specGroups = {};
const noticeTypes = {
note: 1,
issue: 1,
example: 1,
warning: 1,
todo: 1
};
const spaceRegex = /\s+/g;
const specNameRegex = /^spec$|^spec[-]*\w+$/i;
const terminologyRegex = /^def$|^ref/i;
const specCorpus = fs.readJsonSync(modulePath + '/assets/compiled/refs.json');
const containers = require('markdown-it-container');
const md = require('markdown-it')({
html: true,
linkify: true,
typographer: true
})
.use(require('./src/markdown-it-extensions.js'), [
{
filter: type => type.match(specNameRegex),
parse(token, type, name){
if (name) {
let _name = name.replace(spaceRegex, '-').toUpperCase();
let spec = specCorpus[_name] ||
specCorpus[_name.toLowerCase()] ||
specCorpus[name.toLowerCase()] ||
specCorpus[name];
if (spec) {
spec._name = _name;
let group = specGroups[type] = specGroups[type] || {};
token.info.spec = group[_name] = spec;
}
}
},
render(token, type, name){
if (name){
let spec = token.info.spec;
if (spec) return `[<a class="spec-reference" href="#ref:${spec._name}">${spec._name}</a>]`;
}
else return renderRefGroup(type);
}
},
{
filter: type => type.match(terminologyRegex),
parse(token, type, primary){
if (!primary) return;
if (type === 'def'){
return token.info.args.reduce((acc, syn) => {
return `<span id="term:${syn.replace(spaceRegex, '-').toLowerCase()}">${acc}</span>`;
}, primary);
}
else {
return `<a class="term-reference" href="#term:${primary.replace(spaceRegex, '-').toLowerCase()}">${primary}</a>`;
}
}
}
])
.use(require('markdown-it-attrs'))
.use(require('markdown-it-chart').default)
.use(require('markdown-it-deflist'))
.use(require('markdown-it-references'))
.use(require('markdown-it-icons').default, 'font-awesome')
.use(require('markdown-it-ins'))
.use(require('markdown-it-mark'))
.use(require('markdown-it-textual-uml'))
.use(require('markdown-it-sub'))
.use(require('markdown-it-sup'))
.use(require('markdown-it-task-lists'))
.use(require('markdown-it-multimd-table'), {
multiline: true,
rowspan: true,
headerless: true
})
.use(containers, 'notice', {
validate: function(params) {
let matches = params.match(/(\w+)\s?(.*)?/);
return matches && noticeTypes[matches[1]];
},
render: function (tokens, idx) {
let matches = tokens[idx].info.match(/(\w+)\s?(.*)?/);
if (matches && tokens[idx].nesting === 1) {
let id;
let type = matches[1];
if (matches[2]) {
id = matches[2].trim().replace(/\s+/g , '-').toLowerCase();
if (noticeTitles[id]) id += '-' + noticeTitles[id]++;
else noticeTitles[id] = 1;
}
else id = type + '-' + noticeTypes[type]++;
return `<div id="${id}" class="notice ${type}"><a class="notice-link" href="#${id}">${type.toUpperCase()}</a>`;
}
else return '</div>\n';
}
})
.use(require('markdown-it-prism'))
.use(require('markdown-it-toc-and-anchor').default, {
tocClassName: 'toc',
tocFirstLevel: 2,
tocLastLevel: 4,
tocCallback: (_md, _tokens, html) => toc = html,
anchorLinkSymbol: '§',
anchorClassName: 'toc-anchor'
})
.use(require('@traptitech/markdown-it-katex'))
async function render(spec, assets) {
try {
noticeTitles = {};
specGroups = {};
console.log('Rendering: ' + spec.title);
return new Promise(async (resolve, reject) => {
Promise.all((spec.markdown_paths || ['spec.md']).map(path => {
return fs.readFile(spec.spec_directory + path, 'utf8').catch(e => reject(e))
})).then(async docs => {
const features = (({ source, logo }) => ({ source, logo }))(spec);
let doc = docs.join("\n");
doc = applyReplacers(doc);
md[spec.katex ? "enable" : "disable"](katexRules);
fs.writeFile(path.join(spec.destination, 'index.html'), `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>${spec.title}</title>
<link href="https://fonts.googleapis.com/css2?family=Heebo:wght@300;400&display=swap" rel="stylesheet">
${assets.head}
</head>
<body features="${Object.keys(features).join(' ')}">
${assets.svg}
<main>
<header id="header" class="panel-header">
<span id="toc_toggle" panel-toggle="toc">
<svg icon><use xlink:href="#svg-nested-list"></use></svg>
</span>
<a id="logo" href="${spec.logo_link ? spec.logo_link : '#_'}">
<img src="${spec.logo}" />
</a>
<span issue-count animate panel-toggle="repo_issues">
<svg icon><use xlink:href="#svg-github"></use></svg>
</span>
</header>
<article id="content">
${md.render(doc)}
</article>
</main>
<slide-panels id="slidepanels">
<slide-panel id="repo_issues" options="right">
<header class="panel-header">
<span>
<svg icon><use xlink:href="#svg-github"></use></svg>
<span issue-count></span>
</span>
<span class="repo-issue-toggle" panel-toggle="repo_issues">✕</span>
</header>
<ul id="repo_issue_list"></ul>
</slide-panel>
<slide-panel id="toc">
<header class="panel-header">
<span>Table of Contents</span>
<span panel-toggle="toc">✕</span>
</header>
<div id="toc_list">
${toc}
</div>
</slide-panel>
</slide-panels>
</body>
<script>window.specConfig = ${JSON.stringify(spec)}</script>
${assets.body}
</html>
`, function(err, data){
if (err) {
reject(err);
}
else {
resolve();
}
});
});
});
}
catch(e) {
console.error(e);
}
}
config.specs.forEach(spec => {
spec.spec_directory = normalizePath(spec.spec_directory);
spec.destination = normalizePath(spec.output_path || spec.spec_directory);
fs.ensureDirSync(spec.destination);
let assetTags = {
svg: fs.readFileSync(modulePath + '/assets/icons.svg', 'utf8') || ''
};
let customAssets = (spec.assets || []).reduce((assets, asset) => {
let ext = asset.path.split('.').pop();
if (ext === 'css') {
assets.css += `<link href="${asset.path}" rel="stylesheet"/>`;
}
if (ext === 'js') {
assets.js[asset.inject || 'body'] += `<script src="${asset.path}" ${ asset.module ? 'type="module"' : '' } ></script>`;
}
return assets;
}, {
css: '',
js: { head: '', body: '' }
});
if (options.dev) {
assetTags.head = assets.head.css.map(path => `<link href="${path}" rel="stylesheet"/>`).join('') +
customAssets.css +
assets.head.js.map(path => `<script src="${path}"></script>`).join('') +
customAssets.js.head;
assetTags.body = assets.body.js.map(path => `<script src="${path}" data-manual></script>`).join('') +
customAssets.js.body;
}
else {
assetTags.head = `
<style>${fs.readFileSync(modulePath + '/assets/compiled/head.css', 'utf8')}</style>
${ customAssets.css }
<script>${fs.readFileSync(modulePath + '/assets/compiled/head.js', 'utf8')}</script>
${ customAssets.js.head }
`;
assetTags.body = `<script>${fs.readFileSync(modulePath + '/assets/compiled/body.js', 'utf8')}</script>
${ customAssets.js.body }`;
}
if (spec.katex) {
const katexDist = findKatexDist();
assetTags.body += `<script>/* katex */${fs.readFileSync(path.join(katexDist, 'katex.min.js'),
'utf8')}</script>`;
assetTags.body += `<style>/* katex */${fs.readFileSync(path.join(katexDist, 'katex.min.css'),
'utf8')}</style>`;
fs.copySync(path.join(katexDist, 'fonts'), path.join(spec.destination, 'fonts'));
}
if (!options.nowatch) {
gulp.watch(
[spec.spec_directory + '**/*', '!' + path.join(spec.destination, 'index.html')],
render.bind(null, spec, assetTags)
)
}
render(spec, assetTags).then(() => {
if (options.nowatch) process.exit(0)
}).catch(() => process.exit(1));
});
}
catch(e) {
console.error(e);
}
}