forked from shuding/innsbruck-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcname.js
53 lines (47 loc) · 1.55 KB
/
cname.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
/**
* Created by shuding on 5/24/16.
*/
const fs = require('fs');
const path = require('path');
module.exports = {
db: null,
init: _db => {
this.db = _db;
},
render: (template, options) => {
let cname = options.blog.plugin ? options.blog.plugin['cname'] || '' : '';
let context = {};
if (template == 'settings') {
// settings page
// include font-awesome
context.head =
`<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">`;
context.settings = `<div class="input-group">
<h5>
CNAME
<a class="fa fa-exclamation-circle" href="https://help.github.com/articles/using-a-custom-domain-with-github-pages/" title="see Github pages custom CNAME document"></a>
</h5>
<p><input type="text" name="plugin.cname" placeholder="your.blog (without http:// or https://)" value="${cname}"></p>
</div>`;
// All <input name='plugin.xxx'> will write the data into DB automatically
}
return context;
},
hook: {
onSetting: () => {
// called after
let cname = this.db.object.blog.plugin ? this.db.object.blog.plugin['cname'] || '' : '';
let cnamePath = path.join(__dirname, '..', 'CNAME');
if (!cname) {
// empty, remove the CNAME file
if (fs.existsSync(cnamePath)) {
fs.unlinkSync(cnamePath);
}
} else {
// write to ../CNAME
fs.writeFileSync(cnamePath, cname);
}
}
}
};