-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
175 lines (155 loc) · 5.12 KB
/
build.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
const Metalsmith = require('metalsmith');
const markdown = require('metalsmith-markdown');
const layouts = require('metalsmith-layouts');
const collections = require('metalsmith-collections');
const more = require('metalsmith-more');
const permalinks = require('metalsmith-permalinks');
const assets = require('metalsmith-assets');
const atomfeed = require('metalsmith-feed-atom');
const sitemap = require('metalsmith-sitemap');
const fs = require('fs-extra');
const path = require('path');
const moment = require('moment');
moment.locale('zh-cn');
const slug = require('slug');
var Handlebars = require('handlebars');
// header footer 模板
Handlebars.registerPartial({
'footer': fs.readFileSync('./layouts/partials/footer.hbs').toString()
});
Handlebars.registerPartial({
'header': fs.readFileSync('./layouts/partials/header.hbs').toString()
});
// tag button helper
// {{tags_button this.tags "tag_button"}}
Handlebars.registerHelper('tags_button', (tags, tagClass) => {
// let str = "<div id = 'tags'>";
let str = "";
if (typeof tags === "string") {
tags = tags.split(',');
}
for (let i in tags) {
str += ` <a title="${tags[i]}" class="${tagClass}" href="/tags/#${tags[i]}">${tags[i]}</a>`
}
// str += "</div>"
return new Handlebars.SafeString(str);
// <a class="pure-button" href="#">A Pure Button</a>
})
Handlebars.registerHelper('formatDate', function(context, block) {
var f = "LL";
var result = "发表于 " + moment(context).format(f);
return result;
});
// 第一个运行的plugin
function initPlugin() {
return function(files, metalsmith, done) {
let f;
for (var file in files) {
f = files[file];
// console.log(file)
if (path.extname(file) === '.md') {
f.basename = slug(path.basename(file, '.md')); // 写入文件名,作为url的一部分
if (path.dirname(file) === ".")
f.identifier = f.basename;
else
f.identifier = slug(path.dirname(file) + '-' + f.basename); // 唯一标识,给diqus评论系统用 like: article-as3-to-typescript
f.lastmod = files[file].date; // use in sitemap
} else {
// 非 md文件不处理
delete files[file];
// fs.copy(path.join('./src/posts', file), path.join('./build/', file), err => {
// console.log(err);
// })
}
}
done()
}
}
// 自写的 tag plugin ,放在一页里
function tags() {
return function(files, metalsmith, done) {
let f;
let tagMap = {};
for (var file in files) {
f = files[file];
// tags
if (f.tags) {
if (typeof(f.tags) === "string") {
f.tags = f.tags.split(',');
}
f.tags.forEach(function(tag) {
if (!tagMap[tag]) {
tagMap[tag] = [];
}
tagMap[tag].push(f);
}, this);
}
}
let m = metalsmith.metadata();
m.tagMap = tagMap;
metalsmith.metadata(m);
done()
}
}
const config = {
title: "CodingBlog",
description: "一个程序员专用的私房极简静态博客系统",
tags: "独立博客,静态博客,Markdown博客",
author: "Nshen <[email protected]>",
url: "http:https://nshen.net/",
updated: new Date().toISOString(),
buildTime: String(moment().format('LLL'))
}
console.log("开始构建,请稍后...");
const metalsmith = Metalsmith(__dirname);
metalsmith.metadata({ site: config })
.source('./src/posts')
.destination('./build')
.use(initPlugin())
.use(collections({
articles: {
pattern: 'article/*.md',
sortBy: 'date',
reverse: true
},
projects: {
pattern: 'project/*.md',
sortBy: 'date',
reverse: true
},
all: {
pattern: '*/*.md',
sortBy: 'date',
reverse: true
}
}))
.use(markdown({
gfm: true,
breaks: true,
langPrefix: 'language-' // prism.js 需要前缀
}))
.use(more({ ext: 'html' }))
.use(permalinks({ pattern: ':categories/:date/:basename', date: 'YYYY-MM-DD', relative: true }))
.use(tags())
.use(layouts({ engine: 'handlebars', default: 'article.hbs', rename: true }))
// 拷贝素材目录
.use(assets({
source: './src/assets', // relative to the root directory
destination: './assets' // relative to the build directory
}))
.use(assets({
source: './src/image',
destination: './image'
}))
.use(assets({
source: './src/copy2root',
destination: './'
}))
.use(atomfeed({ collection: 'all', limit: 20, destination: 'atom.xml', metadata: config }))
.use(sitemap({ hostname: config.url, changefreq: 'weekly', priority: 0.5 }))
.build((err, files) => {
if (err) {
console.log(err);
}
console.log("完成构建!");
});