forked from gramachain/gramachain.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
72 lines (59 loc) · 1.78 KB
/
generate.js
File metadata and controls
72 lines (59 loc) · 1.78 KB
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
const fs = require('fs')
const moment = require('moment')
const path = require('path')
const showdown = require('showdown')
const articlesPath = path.join(__dirname, 'articles')
const blogPath = path.join(__dirname, 'docs', 'blog')
const converter = new showdown.Converter({
tables: true,
})
const getFile = name => {
let parts = name.split('.')
parts = parts[0].split('-')
const date = `${ parts[0] }-${ parts[1] }-${ parts[2] }`
return {
date,
name,
title: `${ parts.slice(3).join(' ') }`,
unix: moment(date, 'YYYY-MM-DD').unix(),
}
}
const getHTML = path => {
const markdown = fs.readFileSync(path, 'utf8')
let cleanMarkdown = ''
let end = false
let start = false
let summary = ''
for (let i = 0; i < markdown.length; i++) {
const c = markdown.charAt(i)
if (c === '+' && !start) {
start = true
} else if (c === '+' && start) {
end = true
} else if (start && !end) {
summary += c
} else if (end) {
cleanMarkdown += c
}
}
const html = converter.makeHtml(cleanMarkdown)
return { html, summary }
}
fs.readdir(articlesPath, (err, files) => {
if (err) {
console.error(err)
return
}
const articles = {}
files.forEach(file => {
const f = getFile(file)
const { html, summary } = getHTML(path.join(articlesPath, f.name))
f.html = html
f.summary = summary
articles[f.name] = f
//const htmlPath = `${ f.title.replace(' ', '-') }.html`
//fs.writeFileSync(path.join(blogPath, htmlPath), html)
console.log('Generation complete!')
})
fs.writeFileSync(path.join(__dirname, 'src', 'articles.json'), JSON.stringify(articles))
})