前言

看这个教程的前提是你的博客已经安装了hexo-douban,hexo-douban是一个在Hexo页面中嵌入豆瓣个人主页的小插件,具体请百度,这里不做安装教程

如何将hexo-douban多个模块组合在一起

为什么要组合在一起呢,因为我觉得一个模块一个页面很乱!然后自己看过的电影书籍也不多,很有必要组合在一起!

效果如下

举个栗子,如果你想把电影和书籍整合到一个界面,主要思路为:

把movie的文件整合到book中,所以你在以前书籍的页面也能看到电影,同理,你想把game,music整合也可以

步骤如下

打开博客根目录>node-modules>hexo-douban>movies-generator.js。将movies文件resolv方法复制到books-generator.js,并修改方法名为resolv1,如下

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
function resolv1(url, timeout) {
var response = '';
try {
response = request(url, {
timeout: timeout,
dataType: 'xml'
});
} catch (err) {
offline = true;
}

if (offline) {
return {
list: [],
next: ""
};
}

var doc = new Dom({
errorHandler: {
warning: function(e) {},

error: function(e) {},

fatalError: function(e) {}
}
}).parseFromString(response.data.toString());

var items = xpath.select('//div[@class="grid-view"]/div[@class="item"]', doc);
var next = xpath.select('string(//span[@class="next"]/a/@href)', doc);
if (next.startsWith("/")) {
next = "https://movie.douban.com" + next;
}

var list = [];
for (var i in items) {
var parser = new Dom().parseFromString(items[i].toString());
var title = xpath.select1('string(//li[@class="title"]/a/em)', parser);
var alt = xpath.select1('string(//li[@class="title"]/a/@href)', parser);
var image = xpath.select1('string(//div[@class="item"]/div[@class="pic"]/a/img/@src)', parser).replace('ipst', 'spst');

var tags = xpath.select1('string(//li/span[@class="tags"])', parser);
tags = tags ? tags.substr(3) : '';
var date = xpath.select1('string(//li/span[@class="date"])', parser);
date = date ? date : '';

var recommend = xpath.select1('string(//li/span[starts-with(@class,"rating")]/@class)', parser);
recommend = renderStar(recommend.substr(6, 1));
var comment = xpath.select1('string(//li/span[@class="comment"])', parser);
comment = comment ? comment : '';

var info = xpath.select1('string(//li[@class="intro"])', parser);
info = info ? info : '';

//image = 'https://images.weserv.nl/?url=' + image.substr(8, image.length - 8) + '&w=100';

list.push({
title: title,
alt: alt,
image: image,
tags: tags,
date: date,
recommend: recommend,
comment: comment,
info: info
});
}

return {
'list': list,
'next': next
};
}

初始化几个movie文件的变量,将movie文件的三个代码块复制到book文件,并修改wish为wish1,resolv为resolv1,如下

1
2
3
4
5
6
7
8
9
var wish = [];  //想读
var read = []; //看完
var reading = []; //在看
var wish1 = [] //想看
var watching = [] //在看
var watched = [] //看完
var headers = {
'Cookie': []
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//想看
var wishUrl = 'https://movie.douban.com/people/' + config.douban.user + '/wish';

for (var nextWish = wishUrl; nextWish;) {
var resWish = resolv1(nextWish, timeout);
nextWish = resWish.next;
wish1 = wish1.concat(resWish.list);
}
//看过
var watchedUrl = 'https://movie.douban.com/people/' + config.douban.user + '/collect';

for (var nextWatched = watchedUrl; nextWatched;) {
var resWatched = resolv1(nextWatched, timeout);
nextWatched = resWatched.next;
watched = watched.concat(resWatched.list);
}
//在看
var watchingUrl = 'https://movie.douban.com/people/' + config.douban.user + '/do';

for (var nextWatching = watchingUrl; nextWatching;) {
var resWatching = resolv1(nextWatching, timeout);
nextWatching = resWatching.next;
watching = watching.concat(resWatching.list);
}

在下面contents代码块添加上字段,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var contents = ejs.renderFile(path.join(__dirname, 'templates/book.ejs'), {
'wish1': wish1, //想看
'watching': watching, //在看
'watched': watched, //看过
'wish': wish, //想读
'read': read, //在读
'reading': reading, //读过
'__': __,
'root': root
},
function(err, result) {
if (err) console.log(err);
return result;
});

接下来将node-modules>hexo-douban>templates>movie.ejs的代码段复制到book.ejs,文件全部代码如下,效果为只显示看过,读过。

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
<blockquote>
<p>
<%- 我的书单; %>
</p>
</blockquote>

<style>
<% include index.css %>
</style>
<div id="hexo-douban-item1">
<% read.forEach(function(item){ %>
<% include bookRead.ejs %>
<% }); %>
<% include pagination.ejs %>
</div>
<blockquote>
<p>
<%- 我的电影; %>
</p>
</blockquote>
<div id="hexo-douban-item2">
<% watched.forEach(function(item){ %>
<% include movieWatched.ejs %>
<% }); %>
<% include pagination.ejs %>
</div>
<script>
<% include index.js %>
<% include pagination.js %>
</script>

hexo三连

1
hexo clean && hexo g && hexo s