-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
172 lines (145 loc) · 3.51 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
var path = require('path'),
fs = require('fs'),
ncp = require('ncp'),
marked = require('marked'),
autoprefixer = require('autoprefixer-stylus')
var layouts = require('metalsmith-layouts'),
stylus = require('metalsmith-stylus'),
uglify = require('metalsmith-uglify'),
markdown = require('metalsmith-markdown'),
prism = require('metalsmith-prism'),
ignore = require('metalsmith-ignore'),
markedToc = require('./lib/markedToc'),
util = require('./lib/util')
var dirname = __dirname,
port = 3000,
env = 'release'
function dirPath() {
var args = [].slice.call(arguments)
args.unshift(dirname)
return path.join.apply(null, args)
}
function copyStatic() {
console.time('[metalsmith] build/static finished')
fs.mkdir(dirPath('dist'), function() {
fs.mkdir(dirPath('dist/static'), function() {
ncp(dirPath('static'), dirPath('dist/static'), function(err) {
if (err) return console.error(err)
console.timeEnd('[metalsmith] build/static finished')
})
})
})
}
var site = require('./src/site.json')
function build() {
site.baseUrl =
env === 'development'
? '//localhost:' + port + '/'
: '//eustia.liriliri.io/'
site.env = env
var metalsmith = require('metalsmith')(dirname)
var renderer = new marked.Renderer()
renderer.heading = heading
console.time('[metalsmith] build/site finished')
metalsmith
.metadata({
site: site
})
.source('src')
.clean(false)
.use(
markedToc({
omit: ['Eustia Documentation'],
maxDepth: 1,
slugify: slugify
})
)
.use(
markdown({
renderer: renderer,
langPrefix: 'language-'
})
)
.use(
stylus({
compress: true,
paths: [dirPath('layout/css')],
use: [autoprefixer()]
})
)
.use(ignore(['site.json']))
.use(
layouts({
engine: 'jade',
directory: 'layout',
pattern: '**/*.html'
})
)
.use(prism())
.use(uglify())
.destination('dist')
.build(function(err) {
if (err) throw err
console.timeEnd('[metalsmith] build/site finished')
})
}
function fullBuild() {
util.rmdir('dist', function() {
copyStatic()
build()
})
}
function server() {
copyStatic()
build()
var st = require('st'),
http = require('http')
var mount = st({
path: dirPath('dist'),
cache: false,
index: 'index.html'
})
http
.createServer(function(req, res) {
mount(req, res)
})
.listen(port, function() {
console.log('http://localhost:' + port + '/')
})
var chokidar = require('chokidar')
var options = {
persistent: true,
ignoreInitial: true,
followSymlinks: true,
usePolling: true,
alwaysStat: false,
depth: undefined,
interval: 100,
atomic: true,
ignorePermissionErrors: false
}
var layout = chokidar.watch(dirPath('layout'), options),
src = chokidar.watch(dirPath('src'), options),
staticFiles = chokidar.watch(dirPath('static'), options)
layout.on('change', build)
src.on('change', build)
staticFiles.on('change', copyStatic)
}
function heading(text, level) {
if (level !== 2) return '<h' + level + '>' + text + '</h' + level + '>'
return (
'<h' + level + ' id="' + slugify(text) + '">' + text + '</h' + level + '>'
)
}
function slugify(str) {
return str
.toLowerCase()
.replace(/\$/, 'dollar-')
.replace(/[^\w]+/g, '-')
}
if (process.argv[2] === 'serve') {
env = 'development'
server()
} else {
fullBuild()
}