-
Notifications
You must be signed in to change notification settings - Fork 6
/
build-html.js
114 lines (105 loc) · 3.25 KB
/
build-html.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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp').sync
const glob = require('glob')
const gaze = require('gaze')
const mdtohtml = require('./lib/mdtohtml')
const WATCH = process.argv[2] === '--watch'
const INPUT_PATH = path.resolve(__dirname, 'content')
const OUTPUT_PATH = path.resolve(__dirname)
const INDEX_HTML = fs.readFileSync(path.resolve(__dirname, 'index.html'))
const INDEX_JSON = require('./index.json')
const indexView = require('./app/views/index')
const postView = require('./app/views/post')
// ~
function layout (content) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>dontkry.com</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="/index.css">
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-40418144-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
</head>
<body>
${content}
<script src="/index.js"></script>
</body>
</html>
`
}
function buildFiles (onlyFile) {
var posts = []
if (onlyFile) {
readFile(onlyFile, function () {})
} else {
glob('**/*.html.md', { cwd: INPUT_PATH }, function (err, files) {
var count = files.length
function done () {
count--
if (count <= 0) buildIndex()
}
files.forEach(function (filepath) {
readFile(filepath, done)
})
})
}
function readFile (filepath, done) {
fs.readFile(path.resolve(INPUT_PATH, filepath), 'utf8', function (err, file) {
if (err) return done(err)
const slug = filepath.slice(0, -3)
const md = mdtohtml(file)
if (!md) return done()
md.context.slug = '/' + slug
delete md.context.renderer
posts.push(md.context)
const html = layout(postView(md).toString())
const output = path.resolve(OUTPUT_PATH, slug)
mkdirp(path.dirname(output))
fs.writeFile(output, html, function () {
console.log('created ' + output)
done()
})
})
}
function buildIndex () {
posts.sort(function (a, b) {
return b.date - a.date
})
INDEX_JSON.posts = posts
const json = JSON.stringify(INDEX_JSON, null, 2)
fs.writeFile(path.resolve(OUTPUT_PATH, 'index.json'), json, function () {
console.log('created index.json')
})
const html = layout(indexView(INDEX_JSON).toString())
fs.writeFile(path.resolve(OUTPUT_PATH, 'index.html'), html, function () {
console.log('created index.html')
})
}
}
// ~
buildFiles()
if (WATCH) {
gaze('**/*.html.md', { cwd: INPUT_PATH }, function () {
this.on('all', function (e, file) {
if (e === 'changed') {
buildFiles(path.relative(INPUT_PATH, file))
} else {
buildFiles()
}
})
})
}