-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsdoc2md.js
85 lines (67 loc) · 2.52 KB
/
jsdoc2md.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
'use strict'
import jsdoc2md from 'jsdoc-to-markdown'
import fs from 'fs'
import path from 'path'
/* input and output paths */
// Function to get all files that match the glob pattern
const findByExtensionSync = (dir, ext) => {
const matchedFiles = [];
const files = fs.readdirSync(dir);
for (const file of files) {
const fileExt = path.extname(file);
if (fileExt === `.${ext}`) {
matchedFiles.push(path.join(dir,file));
}
}
return matchedFiles;
};
// main function
(async ()=> {
// Get all files to read js docs
const inputFiles = findByExtensionSync('js/src', 'js')
const proInputFiles = findByExtensionSync('js/src/pro', 'js');
const partials = './wiki/partials/'
const partial = fs.readdirSync (partials).map (file => partials + file)
const outputFile = './wiki/spec.md'
const helper = './wiki/helpers.cjs'
console.log ('📰 loading js docs...')
let templateData = await Promise.all(inputFiles.map (file => jsdoc2md.getTemplateData({ files: file })));
templateData = templateData.filter (x => x.length > 0)
// TODO: handle alias exchanges
let proTemplateData = await Promise.all(proInputFiles.map (file => jsdoc2md.getTemplateData({ files: file })));
proTemplateData = proTemplateData.filter (x => x.length > 0)
// assign pro classes to REST template data
proTemplateData.forEach((proData) => {
const classArray = templateData.find ((template) => template[0].id === proData[0].memberof);
if (classArray) {
const classArray = templateData.find ((template) => template[0].id === proData[0].memberof);
classArray.push(...proData);
}
})
console.log ('📰 rendering docs for each exchange...')
const template = fs.readFileSync ('./wiki/spec.hbs', 'utf8')
const outputs = await Promise.all (templateData.map (data => jsdoc2md.render ({ template, data, partial, helper })))
console.log ('📰 creating index of exchange classes')
const classes = templateData.map (data => data[0].id).sort ()
const alphabet = Array.from ( Array (26)).map((e, i) => String.fromCharCode(i + 97));
const index = {}
let i = -1
for (const char of alphabet) {
do {
index[char] = classes[++i]
} while (char > classes[i])
}
index.b = 'binance'
index.o = 'okx'
index.h = 'huobi'
// add a glossary 🧐
const result = []
for (const char of alphabet) {
result.push (`[${char}](#${index[char]})`)
}
const markdown = '## glossary\n' + result.join (' ') + '\n'
outputs.unshift (markdown)
outputs.push (markdown)
fs.writeFileSync (outputFile, outputs.join ('\n---\n'))
console.log ('📰 finished rendering docs! 🙌 😶🌫')
})()