-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.js
executable file
·314 lines (271 loc) · 8.54 KB
/
make.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env node
const {
execSync,
} = require('child_process')
const posthtml = require('posthtml')
const toc = require('posthtml-toc')
const marked = require('marked')
const {
readdirSync,
readFileSync,
writeFileSync,
statSync,
} = require('fs')
const {
dirname,
} = require('path')
let doctype
let headOrig
let nav
const minihtml = function(tree) {
tree.match(/^\n(\s+)?$/, node => {
return ''
})
}
posthtml()
.use(function(tree) {
tree.match(/!doctype/, node => (doctype = node))
tree.match({ tag: 'head' }, node => (headOrig = node))
tree.match({ tag: 'nav' }, node => (nav = node))
})
.process(readFileSync('./src/index.html').toString(), { sync: true })
const getFiles = function(dir, result = [], RE) {
const files = readdirSync(dir)
for (let i in files) {
let name = dir + '/' + files[i]
if (statSync(name).isDirectory()) {
getFiles(name, result, RE)
} else if (RE.test(name)) {
result.push(name)
}
}
return result
}
const getTime = function(file) {
const time = execSync(`git log --format=%at -n 1 ${file}`).toString().replace('\n', '')
const timeStart = execSync(`git log --reverse --format=%at ${file}|head -1`).toString().replace('\n', '')
const now = time.length ? new Date(parseInt(time) * 1000) : new Date()
const start = timeStart.length ? new Date(parseInt(timeStart) * 1000) : new Date()
return [start, now]
}
const makeHead = function(tree, options = {}) {
const head = Object.assign({}, headOrig)
const {
title,
description,
canonical,
page,
modifiedTime,
publishedTime
} = options
const url = 'https://ivan.voischev.ru' + canonical
tree.walk.bind(head)(function(node) {
if (title === undefined) {
throw new Error('Отсутствует заголовок: ' + page)
}
if (description === undefined) {
throw new Error('Отсутствует описание: ' + page)
}
if (canonical === undefined) {
throw new Error('Отсутствует каноническая ссылка: ' + page)
}
if (node.tag === 'title') {
node.content = title
}
if (!node.attrs) {
return node
}
if (node.tag === 'meta' && node.attrs.name === 'description') {
node.attrs.content = description
}
if (node.attrs.rel === 'canonical') {
node.attrs.href = url
}
return node
})
if (!modifiedTime && !publishedTime) {
return head
}
head.content = head.content.concat([
{ tag: 'meta', attrs: { property: 'og:type', content: 'article' }},
{ tag: 'meta', attrs: { property: 'og:title', content: title }},
{ tag: 'meta', attrs: { property: 'og:description', content: description }},
{ tag: 'meta', attrs: { property: 'og:url', content: url }},
{ tag: 'meta', attrs: { property: 'article:autor', content: 'Иван Воищев' }},
{ tag: 'meta', attrs: { property: 'article:modified_time', content: modifiedTime }},
{ tag: 'meta', attrs: { property: 'article:published_time', content: publishedTime }},
])
return head
}
const makePage = function(mdPath, cb) {
const [startTime, time] = getTime(mdPath)
const publishedTime = startTime.toISOString()
const modifiedTime = time.toISOString()
const html = marked.parse(readFileSync(mdPath).toString())
const dir = dirname(mdPath) + '/'
const url = dir.replace(/\.\/src/, '')
let title
let description
const page = posthtml()
.use(function(tree) {
tree.match({ tag: 'h1' }, function(node) {
if (title) {
return node
}
title = node.content[0]
return node
})
tree.match({ tag: 'p' }, function(node) {
if (description) {
return node
}
description = node.content[0]
return [
node,
{ tag: 'div', attrs: { id: 'yandex_rtb_R-A-2278012-1' }},
{ tag: 'script', content: [
'window.yaContextCb.push(()=>{',
'Ya.Context.AdvManager.render({',
'renderTo:"yandex_rtb_R-A-2278012-1",',
'blockId:"R-A-2278012-1"',
'})',
'})',
].join('')}
];
})
const head = makeHead(tree, {
title,
description,
canonical: url,
page: mdPath,
modifiedTime,
publishedTime,
})
return [
doctype,
{
tag: 'html',
attrs: { prefix: 'og:http://ogp.me/ns# article:http://ogp.me/ns/article#' },
content: [
head,
{
tag: 'body',
content: [].concat(
nav,
{
tag: 'main',
content: [
{
tag: 'article',
content: tree,
},
],
},
),
},
],
},
]
})
.use(toc({
title: 'Содержание',
}))
.use(minihtml)
.process(html, { sync: true })
.html
cb({
url,
title,
description,
page,
time,
dir,
})
}
const makeCatalog = function(options = {}) {
const {
title,
description,
dir,
canonical,
} = options
const sortFn = options.sortFn || function (a, b) {
return b.time - a.time
}
const links = []
const files = getFiles(dir, [], /\.md$/)
for (let i = 0; i < files.length; i++) {
const file = files[i]
makePage(file, function(data = {}) {
const {
url,
title,
page,
time,
dir,
} = data
writeFileSync(dir + 'index.html', page)
links.push({
url,
title,
time,
})
})
}
const result = posthtml()
.use(function(tree) {
const head = makeHead(tree, {
title,
description,
canonical,
page: dir,
})
return [
doctype,
head,
{
tag: 'body',
content: [].concat(
nav,
{
tag: 'main',
content: {
tag: 'ul',
content: links.sort(sortFn).map(function(item) {
return {
tag: 'li',
content: {
tag: 'a',
attrs: {
href: item.url,
},
content: item.title,
},
}
}),
},
},
),
},
]
})
.use(minihtml)
.process([], { skipParse: true, sync: true })
.html
writeFileSync(dir + '/index.html', result)
}
makeCatalog({
title: 'Заметки',
description: 'Все заметки',
dir: './src/t',
canonical: '/t/',
})
makeCatalog({
title: 'Инвестиции',
description: 'Итоги инвестиций',
dir: './src/invest',
canonical: '/invest/',
sortFn: function(a, b) {
return +b.url.match(/\d+/) - +a.url.match(/\d+/)
},
})