-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
128 lines (96 loc) · 3.06 KB
/
main.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
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const commandLineArgs = require('command-line-args')
const showdown = require('showdown')
const DocSetGenerator = require('docset-generator').DocSetGenerator
const showdownHighlight = require('showdown-highlight')
const rimraf = require('rimraf')
const template = require('./index.html')
// define input and output path in CLI
const args = [
{ name: 'input', alias: 'i', type: String, defaultValue: './markdown'},
{ name: 'output', alias: 'o', type: String, defaultValue: './docset'},
]
let {input, output} = commandLineArgs(args)
// set showdown to `github flavor` because I love github :)
showdown.setFlavor('github')
let converter = new showdown.Converter({
extensions: [showdownHighlight]
})
// temp HTML directory
const html = './.html'
const extReg = /([.]md)|([.]markdown)/
// ensure directories exist
if (!fs.existsSync(html)) {
fs.mkdirSync(html)
}
if (!fs.existsSync(output)) {
fs.mkdirSync(output)
}
let directories = fs.readdirSync(input)
// directories must be types: https://kapeli.com/docsets#supportedentrytypes
for (let j = 0, l = directories.length; j < l; j++) {
let directory = directories[j]
let markdowns = fs.readdirSync(path.join(input, directory))
//
for (let i = 0, len = markdowns.length; i < len; i++) {
let markdown = markdowns[i]
let ext = path.extname(markdown)
if( ext !== '.md' && ext !== '.markdown') {
continue
}
let markdownPath = path.join(input, directory, markdown)
// read markdown file
fs.readFile(markdownPath, 'utf8', (err, data)=>{
// convert to HTML
let text = converter.makeHtml(data)
let directoryPath = path.join(html, directory)
let htmlPath = path.join(directoryPath, markdown.replace(extReg, '') + '.html')
// ensure `write directories` exist
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath)
}
// write to temp HTML file
fs.writeFile(htmlPath, template.replace('{{text}}', text), (err) => {
if (err) throw err
console.log('-- Markdown To HTML:', htmlPath)
if (j === l - 1 && i === len - 1) {
setTimeout(html2Docset, 100)
}
})
})
}
}
// convert temp HTML to docset
function html2Docset(){
let entries = []
for (let j = 0, len = directories.length; j < len; j++) {
let directory = directories[j]
let markdowns = fs.readdirSync(path.join(input, directory))
for (let i = 0, len = markdowns.length; i < len; i++) {
let markdown = markdowns[i]
let name = markdown.replace(extReg, '')
let docPath = path.join(directory, name + '.html')
console.log('-- -- HTML To Docset:', docPath)
entries.push({
name: name,
type: directory,
path: docPath
})
}
}
// create docset use `DocSetGenerator` which is much better than [dashing](https://github.com/technosophos/dashing).
// Long live javascript!
var docSetGenerator = new DocSetGenerator({
destination: output,
name: 'GLSL',
documentation: html,
entries: entries
})
docSetGenerator.create()
// finally, delete temp HTML
rimraf(html, (err) => {
console.log('-- -- -- Done!')
})
}