forked from decentralized-identity/spec-up
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
449 lines (391 loc) · 15.7 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
module.exports = function (options = {}) {
const fs = require('fs-extra');
const path = require('path');
const gulp = require('gulp');
const {
fetchExternalSpecs,
validateReferences,
findExternalSpecByKey
} = require('./src/references.js');
const { runJsonKeyValidatorSync } = require('./src/json-key-validator.js');
runJsonKeyValidatorSync();
// const { createTermRelations } = require('./src/create-term-relations.js');
// createTermRelations();
const { createTermIndex } = require('./src/create-term-index.js');
createTermIndex();
const { insertTermIndex } = require('./src/insert-term-index.js');
insertTermIndex();
const findPkgDir = require('find-pkg-dir');
const modulePath = findPkgDir(__dirname);
let config = fs.readJsonSync('./output/specs-generated.json');
const createVersionsIndex = require('./src/create-versions-index.js');
createVersionsIndex(config.specs[0].output_path);
const { fixMarkdownFiles } = require('./src/fix-markdown-files.js');
const { prepareTref } = require('./src/prepare-tref.js');
let template = fs.readFileSync(path.join(modulePath, 'templates/template.html'), 'utf8');
let assets = fs.readJsonSync(modulePath + '/src/asset-map.json');
let externalReferences;
let references = [];
let definitions = [];
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');
}
}
];
prepareTref(path.join(config.specs[0].spec_directory, config.specs[0].spec_terms_directory));
// Synchronously process markdown files
fixMarkdownFiles(path.join(config.specs[0].spec_directory, config.specs[0].spec_terms_directory));
function createScriptElementWithXTrefDataForEmbeddingInHtml() {
// Test if xtrefs-data.js exists, else make it an empty string
const inputPath = path.join('output', 'xtrefs-data.js');
let xtrefsData = "";
if (fs.existsSync(inputPath)) {
xtrefsData = '<script>' + fs.readFileSync(inputPath, 'utf8') + '</script>';
}
return xtrefsData;
}
const xtrefsData = createScriptElementWithXTrefDataForEmbeddingInHtml();
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");
}
// Custom plugin to add class to <dl> and the last <dd> in each series after a <dt>
function addClassToDefinitionList(md) {
const originalRender = md.renderer.rules.dl_open || function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};
// Variable to keep track of whether the class has been added to the first <dl> after the target HTML
let classAdded = false;
md.renderer.rules.dl_open = function (tokens, idx, options, env, self) {
const targetHtml = 'terminology-section-start-h7vc6omi2hr2880';
let targetIndex = -1;
// Find the index of the target HTML
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].content && tokens[i].content.includes(targetHtml)) {
targetIndex = i;
break;
}
}
// Add class to the first <dl> only if it comes after the target HTML
if (targetIndex !== -1 && idx > targetIndex && !classAdded) {
tokens[idx].attrPush(['class', 'terms-and-definitions-list']);
classAdded = true;
}
let lastDdIndex = -1;
for (let i = idx + 1; i < tokens.length; i++) {
if (tokens[i].type === 'dl_close') {
// Add class to the last <dd> before closing <dl>
if (lastDdIndex !== -1) {
const ddToken = tokens[lastDdIndex];
const classIndex = ddToken.attrIndex('class');
if (classIndex < 0) {
ddToken.attrPush(['class', 'last-dd']);
} else {
ddToken.attrs[classIndex][1] += ' last-dd';
}
}
break;
}
if (tokens[i].type === 'dt_open') {
// Add class to the last <dd> before a new <dt>
if (lastDdIndex !== -1) {
const ddToken = tokens[lastDdIndex];
const classIndex = ddToken.attrIndex('class');
if (classIndex < 0) {
ddToken.attrPush(['class', 'last-dd']);
} else {
ddToken.attrs[classIndex][1] += ' last-dd';
}
lastDdIndex = -1; // Reset for the next series
}
}
if (tokens[i].type === 'dd_open') {
lastDdIndex = i;
}
}
return originalRender(tokens, idx, options, env, self);
};
}
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$|^xref|^tref$/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(terminologyRegex),
parse(token, type, primary) {
if (!primary) return;
if (type === 'def') {
definitions.push(token.info.args);
return token.info.args.reduce((acc, syn) => {
return `<span id="term:${syn.replace(spaceRegex, '-').toLowerCase()}">${acc}</span>`;
}, primary);
}
else if (type === 'xref') {
const url = findExternalSpecByKey(config, token.info.args[0]);
const term = token.info.args[1].replace(spaceRegex, '-').toLowerCase();
return `<a class="x-term-reference term-reference" data-local-href="#term:${token.info.args[0]}:${term}"
href="${url}#term:${term}">${token.info.args[1]}</a>`;
}
else if (type === 'tref') {
return `<span class="transcluded-xref-term" id="term:${token.info.args[1]}">${token.info.args[1]}</span>`;
}
else {
references.push(primary);
return `<a class="term-reference" href="#term:${primary.replace(spaceRegex, '-').toLowerCase()}">${primary}</a>`;
}
}
},
{
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);
}
}
])
.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: '#', // was: §
anchorClassName: 'toc-anchor'
})
.use(require('@traptitech/markdown-it-katex'))
md.use(addClassToDefinitionList);
async function render(spec, assets) {
try {
noticeTitles = {};
specGroups = {};
console.log('\n SPEC-UP-T: Rendering: ' + spec.title + "\n");
function interpolate(template, variables) {
return template.replace(/\${(.*?)}/g, (match, p1) => variables[p1.trim()]);
}
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);
if (spec.external_specs && !externalReferences) {
externalReferences = await fetchExternalSpecs(spec);
}
// Find the index of the terms-and-definitions-intro.md file
const termsIndex = (spec.markdown_paths || ['spec.md']).indexOf('terms-and-definitions-intro.md');
if (termsIndex !== -1) {
// Append the HTML string to the content of terms-and-definitions-intro.md. This string is used to create a div that is used to insert an alphabet index, and a div that is used as the starting point of the terminology index. The newlines are essential for the correct rendering of the markdown.
docs[termsIndex] += '\n\n<div id="alphabet-index-h7vc6omi2hr2880"></div>\n\n<div id="terminology-section-start-h7vc6omi2hr2880"></div>\n\n<hr>\n\n';
}
let doc = docs.join("\n");
doc = applyReplacers(doc);
md[spec.katex ? "enable" : "disable"](katexRules);
const render = md.render(doc);
const templateInterpolated = interpolate(template, {
title: spec.title,
description: spec.description,
author: spec.author,
toc: toc,
render: render,
assetsHead: assets.head,
assetsBody: assets.body,
assetsSvg: assets.svg,
features: Object.keys(features).join(' '),
externalReferences: JSON.stringify(externalReferences),
xtrefsData: xtrefsData,
specLogo: spec.logo,
specFavicon: spec.favicon,
specLogoLink: spec.logo_link,
spec: JSON.stringify(spec)
});
fs.writeFile(path.join(spec.destination, 'index.html'),
templateInterpolated, 'utf8'
, function (err, data) {
if (err) {
reject(err);
}
else {
resolve();
}
});
validateReferences(references, definitions, render);
references = [];
definitions = [];
});
});
}
catch (e) {
console.error("\n SPEC-UP-T: " + e + "\n");
}
}
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("\n SPEC-UP-T: " + e + "\n");
}
}