-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
59 lines (52 loc) · 1.69 KB
/
index.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
"use strict";
const pkg = require("./package.json");
const url_for = hexo.extend.helper.get("url_for").bind(hexo);
const path = require("path");
const tabs = require("./lib/tabs")(hexo);
// register tabs
hexo.extend.tag.register("tabs", tabs, { ends: true });
hexo.extend.tag.register("subtabs", tabs, { ends: true });
hexo.extend.tag.register("subsubtabs", tabs, { ends: true });
// config
const config = require("./lib/config")(hexo);
let cssHref = `/css/${pkg.name}.css`;
let jsSrc = `/js/${pkg.name}.js`;
if (config.cdn) {
cssHref = `${config.cdn}${pkg.name}@${pkg.version}/css/index.css`;
jsSrc = `${config.cdn}${pkg.name}@${pkg.version}/js/index.js`;
} else {
const fs = require("hexo-fs");
hexo.extend.generator.register(`${pkg.name}-css`, function (locals) {
return {
path: url_for(cssHref),
data: function () {
return fs.createReadStream(path.join(__dirname, "./css/index.css"));
},
};
});
hexo.extend.generator.register(`${pkg.name}-js`, function (locals) {
return {
path: url_for(jsSrc),
data: function () {
return fs.createReadStream(path.join(__dirname, "./js/index.js"));
},
};
});
}
const linkTag = `<link href="${cssHref}" rel="stylesheet"/>`;
const scriptTag = `<script src="${jsSrc}"></script>`;
/**
* 插入布局页面
* @param {'default' | 'home' | 'post' | 'page' | 'archive' | 'category' | 'tag'} layout
*/
function insertToLayout(layout) {
hexo.extend.injector.register("head_begin", linkTag, layout);
hexo.extend.injector.register("body_end", scriptTag, layout);
}
if (Array.isArray(config.layout)) {
config.layout.forEach((layout) => {
insertToLayout(layout);
});
} else {
insertToLayout(layout);
}