-
Notifications
You must be signed in to change notification settings - Fork 7
/
build-docs.js
121 lines (103 loc) · 3.5 KB
/
build-docs.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
/*
Copyright 2015 Jesse Manek
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under
*/
var marked = require('marked')
var fs = require('fs')
var highlight = require('highlight.js')
var Handlebars = require('handlebars')
var path = require('path')
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: false,
pedantic: true,
sanitize: false,
smartLists: true,
smartypants: false,
highlight: function (code, lang) {
return highlight.highlight(lang, code).value
}
})
// Create syntax-highlighting alias 'shell' for 'bash'
var bash = highlight.getLanguage('bash')
highlight.registerLanguage('shell', function (highlight) {
return bash
})
// Easier than changing Slate's js
marked.defaults.langPrefix = 'highlight '
Handlebars.registerHelper('str', function (item) {
return '"' + item + '"'
})
Handlebars.registerHelper('html', function (content) {
return new Handlebars.SafeString(content)
})
fs.readFile('./docs/index.md', 'utf8', function (err, content) {
if (err) console.log(err)
content = content.split(/---/g)
if (content.length === 1) {
throw new Error('No markdown page settings found!')
}
var data = {}
var tokens = new marked.Lexer().lex(content[1])
var token
var listName
for (var idx = 0; idx < tokens.length; idx++) {
token = tokens[idx]
if (token.type === 'list_item_start') {
token = tokens[idx + 1].text
if (listName === 'language_tabs') {
var lang = token
var langSplit = lang.split(':')
if (langSplit.length === 1) token = {name: langSplit[0], text: langSplit[0]}
if (langSplit.length === 2) token = {name: langSplit[0], text: langSplit[1]}
}
data[listName].push(token)
idx += 2
}
if (token.type === 'paragraph') {
if (tokens[idx + 1] !== undefined && tokens[idx + 1].type === 'list_start') {
listName = token.text.slice(0, -1)
data[listName] = []
} else {
token = token.text.split(': ')
data[token[0]] = token[1]
}
}
}
if (data.includes) {
// create partials
for (var i = 0; i < data.includes.length; i++) {
var includeFileName = data.includes[i]
var includeFilePath = path.resolve(__dirname, 'docs/includes', includeFileName + '.md')
var includeContent = fs.readFileSync(includeFilePath, {encoding: 'utf8'})
var markedInclude = marked(includeContent)
Handlebars.registerPartial(includeFileName, markedInclude)
}
}
fs.readFile('./docs/layouts/layout.html', 'utf8', function (err, source) {
if (err) console.log(err)
if (data.includes) {
var includes = ''
for (var i = 0; i < data.includes.length; i++) {
var include = data.includes[i]
includes += '\n' + '{{> ' + include + ' }}'
}
source = source.replace(/{{{html content}}}/g, '{{{html content}}}' + includes)
}
var template = Handlebars.compile(source)
data['content'] = marked(content.slice(2).join(''))
fs.writeFile('./docs/index.html', template(data), function (err) {
if (err) console.log(err)
})
})
})