-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
138 lines (130 loc) · 4.16 KB
/
build.js
File metadata and controls
138 lines (130 loc) · 4.16 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
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
/**
* Created by chenze on 16/7/16.
*/
"use strict"
//TODO convert to Promise or gulp
//TODO abstract setting to config.yml
const _ = require("lodash");
const yaml = require('js-yaml');
const fs = require('fs');
const file = require('./lib/file.js');
const hljs = require('highlight.js');
// const imagemin = require('imagemin');
// const imageminMozjpeg = require('imagemin-mozjpeg');
// const imageminPngquant = require('imagemin-pngquant');
const Reg = /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?([\w\W]*)*/;
// const md = require('markdown-it')({
// html: true,
// highlight: function (str, lang) {
// if (lang && hljs.getLanguage(lang)) {
// try {
// return `<pre class="hljs" lang=${lang}><code>${hljs.highlight(lang, str, true).value}</code></pre>`;
// } catch (__) {}
// }
// return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
// }
// });
var _data = {};
var _list = [];
var _photo = [];
/*
* parse yaml file
*/
function yamlToPhoto(path) {
try {
return yaml.safeLoad(fs.readFileSync(path, 'utf8'));
} catch (e) {
console.log(`[Load ${path} Error]: ${e}`);
}
}
/*
* compress image only support {.png|.jpg}
*/
function minImage(entry,output) {
output = output || './api/images';
imagemin([entry], output, {
plugins: [
imageminMozjpeg(),
imageminPngquant({quality: '65-80'})
]
})
}
/*
* generate html snapshot
*/
function htmlSubstring(s, n) {
var m, r = /<([^>\s]*)[^>]*>/g,
stack = [],
lasti = 0,
result = '';
//for each tag, while we don't have enough characters
while ((m = r.exec(s)) && n) {
//get the text substring between the last tag and this one
var temp = s.substring(lasti, m.index).substr(0, n);
//append to the result and count the number of characters added
result += temp;
n -= temp.length;
lasti = r.lastIndex;
if (n) {
result += m[0];
if (m[1].indexOf('/') === 0) {
//if this is a closing tag, than pop the stack (does not account for bad html)
stack.pop();
} else if (m[1].lastIndexOf('/') !== m[1].length - 1) {
//if this is not a self closing tag than push it in the stack
stack.push(m[1]);
}
}
}
//add the remainder of the string, if needed (there are no more tags in here)
result += s.substr(lasti, n);
//fix the unclosed tags
while (stack.length) {
result += '</' + stack.pop() + '>';
}
return result;
}
/*
* generate post or photo id
*/
function generateId(title,date){
title = title || '';
var d = new Date(date),result = '';
var _title = title[title.length-1];
var _date = ''+d.getFullYear()+d.getMonth()+d.getDate();
Array.from(_title+_date).forEach(v=>{
result += v.codePointAt();
})
return result;
}
fs.readdir(__dirname+'/post',function (err,data) {
if(err){console.log(`[Read Directory Error]: ${err}`)}
_data.blog = [];
data.forEach(function(v){
var path = __dirname +'/post/' +v;
var text,meta,content;
text = Reg.exec(fs.readFileSync(path, 'utf8'));
try{
meta = yaml.safeLoad(text[2]);
meta.id = generateId(meta.title,meta.date);
content = md.render(text[3]);
// meta.snapshot = content.replace(/(<([^>]+)>)/ig,'').substring(0,200);
meta.snapshot = htmlSubstring(content,200);
_list.push(meta);
file.mkNestFileSync(`./api/${meta.id}.json`,JSON.stringify({meta,content}));
}catch(e){
console.log(`[Read Markdown Error]:In ${v}: ${e}`);
}
});
yamlToPhoto('./photo/photo.yml').forEach((v,k,arr)=>{
v.id = generateId(v.title,v.date);
minImage('./photo/images/*.{jpg,png}')
if(k == arr.length-1){
file.mkNestFileSync(`./api/photoList.json`,JSON.stringify(arr));
}
});
_list = _.sortBy(_list,function(n){
return - new Date(n.date).getTime();
});
file.mkNestFileSync(`./api/list.json`,JSON.stringify(_list));
});