最终效果:

代码来源: Bitcron 文章内插入豆瓣条目 - 林小沐

添加 douban shortcode

创建文件 /layouts/shortcodes/douban.html ,代码如下:

1
2
3
<div class="douban_show">
  <div id="db{{ .Get 0 }}" date-dbid="{{ .Get 0 }}" class="douban_item post-preview"></div>
</div>

添加 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
$(document).ready(function () {
    $('.douban_item').each(function () {
        var id = $(this).attr('date-dbid').toString();
        if (id.length < 9) {
            var url = "https://api.douban.com/v2/movie/subject/" + id + "?apikey=0dad551ec0f84ed02907ff5c42e8ec70";
            $.ajax({
                url: url,
                type: 'GET',
                dataType: "jsonp",
                success: function (data) {
                    var db_casts = "",
                        db_genres = "";
                    for (var i in data.casts) {
                        db_casts += data.casts[i].name + " ";
                    }
                    for (var i in data.genres) {
                        db_genres += data.genres[i] + " ";
                    }
                    var db_star = Math.ceil(data.rating.average)
                    $('#db' + id).html(
                        "<div class='post-preview--meta'><div class='post-preview--middle'><h4 class='post-preview--title'><a target='_blank' href='" +
                        data.alt + "'>《" + data.title + "》</a></h4><div class='rating'><div class='rating-star allstar" +
                        db_star + "'></div><div class='rating-average'>" + data.rating.average +
                        "</div></div><time class='post-preview--date'>导演:" + data.directors[0].name + " / 主演:" +
                        db_casts + " / 类型:" + db_genres + " / " + data.year +
                        "</time><section style='max-height:75px;overflow:hidden;' class='post-preview--excerpt'>" +
                        data.summary +
                        "</section></div></div><div class='post-preview--image' style='background-image:url(" + data.images
                        .large + ");'></div>");
                }
            });
        } else if (id.length > 9) {
            var url = "https://api.douban.com/v2/book/isbn/" + id +
                "?fields=alt,title,subtitle,rating,author,publisher,pubdate,summary,images&apikey=0dad551ec0f84ed02907ff5c42e8ec70";
            $.ajax({
                url: url,
                type: 'GET',
                dataType: 'JSONP',
                success: function (data) {
                    var db_star = Math.ceil(data.rating.average)
                    $('#db' + id).html(
                        "<div class='post-preview--meta'><div class='post-preview--middle'><h4 class='post-preview--title'><a target='_blank' href='" +
                        data.alt + "'>《" + data.title + "》" + data.subtitle +
                        "</a></h4><div class='rating'><div class='rating-star allstar" + db_star +
                        "'></div><div class='rating-average'>" + data.rating.average +
                        "</div></div><time class='post-preview--date'>" + data.author[0] + " / " + data.publisher +
                        " / " + data.pubdate +
                        "</time><section style='max-height:75px;overflow:hidden;' class='post-preview--excerpt'>" +
                        data.summary +
                        "</section></div></div><div class='post-preview--image' style='background-image:url(" + data.images
                        .large + ");'></div>");
                }
            });
        } else {
            console.log("出错" + id)
        }
    });
});

添加 css

  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
/* douban item post-preview --------*/

.post-preview {
  max-width: 780px;
  margin: 1em auto;
  position: relative;
  display: flex;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25), 0 0 1px rgba(0, 0, 0, .25);
}

.post-preview--meta {
  width: 75%;
  padding: 25px;
}

.post-preview--middle {
  line-height: 28px;
}

.post-preview--title {
  font-size: 18px;
  margin: 0;
}

.post-preview--title a {
  text-decoration: none;
}

.post-preview--date {
  font-size: 14px;
  color: #999;
}

.post-preview--excerpt {
  font-size: 14px;
  line-height: 1.825;
}

.post-preview--excerpt p {
  margin-bottom: 0;
}

.post-preview--image {
  width: 25%;
  float: right;
  background-size: cover;
  background-position: center center;
  border-top-right-radius: 2px;
  border-bottom-right-radius: 2px;
}

@media (max-width:550px) {
  .post-preview {
    width: 95%;
  }
  .post-preview--image {
    width: 40%;
  }
  .post-preview--meta {
    width: 60%;
  }
  .post-preview--excerpt {
    display: none;
  }
}

.rating {
  display: block;
  line-height: 15px;
}

.rating-star {
  display: inline-block;
  width: 75px;
  height: 15px;
  background-repeat: no-repeat;
  background-image: url(/image/douban_star.png);
  overflow: hidden;
}

.allstar10 {
  background-position: 0px 0px;
}

.allstar9 {
  background-position: 0px -15px;
}

.allstar8 {
  background-position: 0px -30px;
}

.allstar7 {
  background-position: 0px -45px;
}

.allstar6 {
  background-position: 0px -60px;
}

.allstar5 {
  background-position: 0px -75px;
}

.allstar4 {
  background-position: 0px -90px;
}

.allstar3 {
  background-position: 0px -105px;
}

.allstar2 {
  background-position: 0px -120px;
}

.allstar1 {
  background-position: 0px -135px;
}

.allstar0 {
  background-position: 0px -150px;
}

.rating-average {
  color: #777;
  display: inline-block;
  font-size: 13px;
  margin-left: 10px;
}

注意: 豆瓣评分五角星图片需要另外载入。

保存图片为 /static/image/douban_star.png

使用方法

1
{{< douban id >}}

补充说明

关于添加 js, css 代码,一般 hugo 主题都有 customJScustomCSS 选项(比如我的主题),我们可以添加到其中。

我不确定有几个人需要这个 douban shortcode ,因此,暂时没有整合到 hugo-theme-jane 中,如果有人需要,欢迎在此文章下留言。

最后,感谢原创作者林小沐!