-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentation.js
75 lines (72 loc) · 2.97 KB
/
documentation.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
const beautify = require('js-beautify').html
const fs = require('fs')
const Remarkable = require('remarkable')
const HTML = require('./html.js')
const path = require('path')
const template = fs.readFileSync('./documentation-template.html').toString()
module.exports = (rootPath, moduleName, documentationPath) => {
const md = new Remarkable.Remarkable()
let filePath
if (moduleName.startsWith('@userdashboard')) {
filePath = path.join(rootPath, `/node_modules/${moduleName}/readme.md`)
} else {
filePath = path.join(rootPath, '../readme.md')
}
const text = fs.readFileSync(filePath).toString()
const merged = template.replace('<title></title>', '<title>' + formatTitle(filePath) + '</title>').replace('<div class="content"></div>', `<div class="content">
${md.render(text)}
</div>`)
const doc = HTML.parse(merged)
doc.getElementsByTagName('h1')[0].child = [{
node: 'text',
text: formatTitle(filePath)
}]
const navbarTemplate = doc.getElementById('navbar')
doc.getElementById('navigation').child = navbarTemplate.child
navbarTemplate.parentNode.removeChild(navbarTemplate)
const codeTags = doc.getElementsByTagName('code')
if (codeTags && codeTags.length) {
for (const tag of codeTags) {
tag.setAttribute('data-language', 'js')
}
}
const html = beautify(doc.toString(), { indent_size: 2, space_in_empty_paren: true })
const filename = formatFileName(filePath)
fs.writeFileSync(`${documentationPath}/${filename}`, html)
console.log('writing documentation file', filename)
}
function formatTitle(filePath) {
if (filePath.indexOf('/dashboard/') > -1) {
return 'Dashboard documentation'
} else if (filePath.indexOf('/stripe-connect/') > -1) {
return 'Stripe Connect module documentation'
} else if (filePath.indexOf('/stripe-subscriptions/') > -1) {
return 'Stripe Subscriptions module documentation'
} else if (filePath.indexOf('/maxmind-geoip/') > -1) {
return 'MaxMind GeoIP module documentation'
} else if (filePath.indexOf('/organizations/') > -1) {
return 'Organizations module documentation'
} else if (filePath.indexOf('/example-web-app') > -1) {
return 'Example web app'
} else if (filePath.indexOf('example-subscription-web-app')) {
return 'Example subscription web app'
}
}
function formatFileName(filePath) {
if (filePath.indexOf('/dashboard/') > -1) {
return 'dashboard.html'
} else if (filePath.indexOf('/stripe-connect/') > -1) {
return 'stripe-connect-module.html'
} else if (filePath.indexOf('/stripe-subscriptions/') > -1) {
return 'stripe-subscriptions-module.html'
} else if (filePath.indexOf('/maxmind-geoip/') > -1) {
return 'maxmind-geoip-module.html'
} else if (filePath.indexOf('/organizations/') > -1) {
return 'organizations-module.html'
} else if (filePath.indexOf('/example-web-app') > -1) {
return 'example-web-app.html'
} else if (filePath.indexOf('example-subscription-web-app')) {
return 'example-subscription-web-app.html'
}
return filePath
}