diff --git a/README.md b/README.md index 468ff82..1144aa7 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ [![NPM](https://nodei.co/npm/hexo-douban.png)](https://nodei.co/npm/hexo-douban/) ## 原理 + hexo-douban 目前升级到了 2.x 版本,将原先由插件客户端自行获取数据的逻辑抽到了一个隐藏的服务端中进行,以统一解决数据获取、数据缓存、风控对抗等问题,提高页面生成的成功率和效率。 ## 安装 @@ -26,6 +27,7 @@ $ npm install hexo-douban --save douban: id: 162448367 builtin: false + item_per_page: 10 book: path: books/index.html title: 'This is my book title' @@ -51,6 +53,7 @@ douban: - **id**: 你的豆瓣ID(纯数字格式,不是自定义的域名)。获取方法可以参考[怎样获取豆瓣的数字 ID ?](https://www.zhihu.com/question/19634899) - **builtin**: 是否将`hexo douban`命令默认嵌入进`hexo g`、`hexo s`,使其自动执行`hexo douban` 命令。默认关闭。当你的豆瓣条目较多时,也建议关闭。 +- **item_per_page**: 每页展示的条目数,默认 10 。 - **path**: 生成页面后的路径,默认生成在 //yourblog/books/index.html 等下面。如需自定义路径,则可以修改这里。 - **title**: 该页面的标题。 - **quote**: 写在页面开头的一段话,支持html语法。 @@ -60,6 +63,7 @@ douban: 如果只想显示某一个页面(比如movie),那就把其他的配置项注释掉即可。 ## 使用 + ``` $ hexo douban -h Usage: hexo douban @@ -73,9 +77,11 @@ Options: -m, --movies Generate douban movies only -s, --songs Generate douban songs only ``` + 如果不加参数,那么默认参数为`-bgms`。当然,前提是配置文件中均有这些类型的配置。 -**需要注意的是**,通常大家都喜欢用`hexo d`来作为`hexo deploy`命令的简化,但是当安装了`hexo douban`之后,就不能用`hexo d`了,因为`hexo douban`跟`hexo deploy`的前缀都是`hexo d`。 +**需要注意的是**,通常大家都喜欢用`hexo d`来作为`hexo deploy`命令的简化,但是当安装了`hexo douban`之后,就不能用`hexo d`了,因为`hexo douban`跟`hexo deploy` +的前缀都是`hexo d`。 第一次使用 hexo douban 时,后台会异步进行数据获取,一般需要等待一段时间(后台访问你的标记页面)才能查到数据。顺利情况下,平均一个页面会花10s。 @@ -83,11 +89,10 @@ Options: 后续如果你的豆瓣数据更新了,hexo douban 同样也会自动进行更新(同样需要等待一段时间才会查到更新数据),不过出于安全考虑,一个用户id**每小时至多只会同步一次**。 - 由于数据获取的策略原因,目前数据只支持新增,不支持删除。也就是说,即使你在豆瓣中删除了某个“想看”,这里同步到的数据中仍然包含这个条目。 - ## 升级 + 我会不定期更新一些功能或者修改一些Bug,所以如果想使用最新的特性,可以用下面的方法来更新: 1. 修改 package.json 内 hexo-douban 的版本号至最新 @@ -96,10 +101,13 @@ Options: 或者使用`npm install hexo-douban --update --save`直接更新。 ## 显示 + 如果上面的配置和操作都没问题,就可以在生成站点之后打开 `//yourblog/books` 和 `//yourblog/movies`, `//yourblog/games`, 来查看结果。 ## 菜单 + 如果上面的显示没有问题就可以在主题的配置文件 `_config.yml` 里添加如下配置来为这些页面添加菜单链接. + ```yaml menu: Home: / @@ -109,27 +117,35 @@ menu: Games: /games #This is your games page Songs: /songs #This is your songs page ``` + ## 截图 + 我们在下面这些常见的主题里测试了插件的使用效果: ### hexo-theme-landscape + ![landscape](screenshot/landscape.png) ### hexo-theme-next + ![next](screenshot/next.png) ### hexo-theme-yilia + ![yilia](screenshot/yilia.png) ### hexo-theme-indigo + ![indigo](screenshot/indigo.png) ### hexo-theme-aath + ![aath](screenshot/aath.png) ## 接口 -如果有非hexo环境的部署需求(例如我自己就是用的 Ghost ,简单效果页:[豆瓣](https://blog.mythsman.com/douban)),或者仅仅想对自己的豆瓣数据进行备份,可以尝试使用下面的接口,复用后端维护的数据提取服务 [mouban](https://github.com/mythsman/mouban): +如果有非hexo环境的部署需求(例如我自己就是用的 Ghost +,简单效果页:[豆瓣](https://blog.mythsman.com/douban)),或者仅仅想对自己的豆瓣数据进行备份,可以尝试使用下面的接口,复用后端维护的数据提取服务 [mouban](https://github.com/mythsman/mouban): ``` # 将 {your_douban_id} 改为你的豆瓣数字ID @@ -172,12 +188,15 @@ https://mouban.mythsman.com/guest/user_song?id={your_douban_id}&action=collect ``` ## 免责声明 + 本项目仅供学习交流使用,不得用于任何商业用途。 数据来源于互联网公开内容,没有获取任何私有和有权限的信息(个人信息等),由此引发的任何法律纠纷与本人无关。 ## 反馈 + 系统刚上线,可能还不够完善。如果大家在使用的过程中数据有问题、或者有什么问题和意见,欢迎随时提issue。如果你觉得这个插件很好用,欢迎右上角点下小✨~ ## Lisense + MIT diff --git a/lib/books-generator.js b/lib/books-generator.js index 1010fe2..19181ed 100644 --- a/lib/books-generator.js +++ b/lib/books-generator.js @@ -104,6 +104,11 @@ module.exports = async function (locals) { timeout = config.douban.timeout; } + let item_per_page = 10 + if (config.douban.item_per_page) { + item_per_page = config.douban.item_per_page + } + const startTime = new Date().getTime(); let data = await fetchData(config.douban.id, config.url, timeout); @@ -120,6 +125,7 @@ module.exports = async function (locals) { 'wish': data.wish, 'collect': data.collect, 'doing': data.do, + 'item_per_page': item_per_page, '__': __, 'root': root }, diff --git a/lib/games-generator.js b/lib/games-generator.js index d542dcf..d5f3c7b 100644 --- a/lib/games-generator.js +++ b/lib/games-generator.js @@ -102,6 +102,11 @@ module.exports = async function (locals) { timeout = config.douban.timeout; } + let item_per_page = 10 + if (config.douban.item_per_page) { + item_per_page = config.douban.item_per_page + } + const startTime = new Date().getTime(); let data = await fetchData(config.douban.id, config.url, timeout); @@ -118,6 +123,7 @@ module.exports = async function (locals) { 'wish': data.wish, 'collect': data.collect, 'doing': data.do, + 'item_per_page': item_per_page, '__': __, 'root': root }, diff --git a/lib/movies-generator.js b/lib/movies-generator.js index 71a492e..3cded24 100644 --- a/lib/movies-generator.js +++ b/lib/movies-generator.js @@ -102,6 +102,11 @@ module.exports = async function (locals) { timeout = config.douban.timeout; } + let item_per_page = 10 + if (config.douban.item_per_page) { + item_per_page = config.douban.item_per_page + } + const startTime = new Date().getTime(); let data = await fetchData(config.douban.id, config.url, timeout); @@ -118,6 +123,7 @@ module.exports = async function (locals) { 'wish': data.wish, 'collect': data.collect, 'doing': data.do, + 'item_per_page': item_per_page, '__': __, 'root': root }, diff --git a/lib/songs-generator.js b/lib/songs-generator.js index d644ccf..ab47873 100644 --- a/lib/songs-generator.js +++ b/lib/songs-generator.js @@ -111,6 +111,11 @@ module.exports = async function (locals) { timeout = config.douban.timeout; } + let item_per_page = 10 + if (config.douban.item_per_page) { + item_per_page = config.douban.item_per_page + } + const startTime = new Date().getTime(); let data = await fetchData(config.douban.id, config.url, timeout); @@ -127,6 +132,7 @@ module.exports = async function (locals) { 'wish': data.wish, 'collect': data.collect, 'doing': data.do, + 'item_per_page': item_per_page, '__': __, 'root': root }, diff --git a/lib/templates/book.ejs b/lib/templates/book.ejs index 6c2e25f..7a13238 100644 --- a/lib/templates/book.ejs +++ b/lib/templates/book.ejs @@ -42,9 +42,4 @@ - - - +<%- include('index',{item_per_page:item_per_page}) -%> diff --git a/lib/templates/game.ejs b/lib/templates/game.ejs index d7792d2..47be096 100644 --- a/lib/templates/game.ejs +++ b/lib/templates/game.ejs @@ -43,8 +43,5 @@ - +<%- include('index',{item_per_page:item_per_page}) -%> diff --git a/lib/templates/index.ejs b/lib/templates/index.ejs new file mode 100644 index 0000000..449f11c --- /dev/null +++ b/lib/templates/index.ejs @@ -0,0 +1,126 @@ + \ No newline at end of file diff --git a/lib/templates/index.js b/lib/templates/index.js deleted file mode 100644 index 46e9e77..0000000 --- a/lib/templates/index.js +++ /dev/null @@ -1,46 +0,0 @@ -/** - * Created by myths on 18-2-8. - */ - -Element.prototype.siblings = function () { - let siblingElement = []; - let sibs = this.parentNode.children; - for (let i = 0; i < sibs.length; i++) { - if (sibs[i] !== this) { - siblingElement.push(sibs[i]); - } - } - return siblingElement; -}; - -function tabClick() { - //修改标签样式 - this.classList.add('hexo-douban-active'); - let sibs = this.siblings(); - for (let j = 0; j < sibs.length; j++) { - sibs[j].classList.remove('hexo-douban-active'); - } - //显示对应板块 - let itemId = this.id.replace('tab', 'item'); - let target = document.getElementById(itemId); - target.classList.remove('hexo-douban-hide'); - target.classList.add('hexo-douban-show'); - sibs = document.getElementById(itemId).siblings(); - for (let k = 0; k < sibs.length; k++) { - sibs[k].classList.remove('hexo-douban-show'); - sibs[k].classList.add('hexo-douban-hide'); - } -} - -function initIndex() { - let tabs = document.getElementsByClassName("hexo-douban-tab"); - for (let i = 0; i < tabs.length; i++) { - tabs[i].onclick = tabClick; - tabs[i].onclick.apply(tabs[i]); - } - tabs[2].click(); -} - -initIndex(); - - diff --git a/lib/templates/movie.ejs b/lib/templates/movie.ejs index ebf9091..34690ce 100644 --- a/lib/templates/movie.ejs +++ b/lib/templates/movie.ejs @@ -41,8 +41,5 @@ - +<%- include('index',{item_per_page:item_per_page}) -%> diff --git a/lib/templates/pagination.ejs b/lib/templates/pagination.ejs index c2e5adf..c2a8b7d 100644 --- a/lib/templates/pagination.ejs +++ b/lib/templates/pagination.ejs @@ -1,7 +1,7 @@
<%= __('top') %> <%= __('prev') %> - 1 / 3 + 1 / 1 <%= __('next') %> <%= __('end') %>
\ No newline at end of file diff --git a/lib/templates/pagination.js b/lib/templates/pagination.js deleted file mode 100644 index ef5458d..0000000 --- a/lib/templates/pagination.js +++ /dev/null @@ -1,72 +0,0 @@ -function makePageNum(num, arr) { - return (num + 1) + ' / ' + (Math.ceil(arr.length / 10 === 0 ? 1 : Math.ceil(arr.length / 10))); -} - -function firstBtn() { - let sibs = this.parentNode.siblings(); - displayPage(sibs, 0); - this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(0, sibs); -} - -function previousBtn() { - let sibs = this.parentNode.siblings(); - let currNum = this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText; - currNum = currNum.substr(0, currNum.indexOf('/') - 1); - currNum = parseInt(currNum, 10) - 1; - if (currNum > 0) { - currNum--; - } - displayPage(sibs, currNum); - this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(currNum, sibs); -} - -function nextBtn() { - let sibs = this.parentNode.siblings(); - let currNum = this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText; - currNum = currNum.substr(0, currNum.indexOf('/') - 1); - currNum = parseInt(currNum, 10) - 1; - if (currNum < Math.ceil(sibs.length / 10) - 1) { - currNum++; - } - displayPage(sibs, currNum); - this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(currNum, sibs); -} - -function lastBtn() { - let sibs = this.parentNode.siblings(); - displayPage(sibs, Math.ceil(sibs.length / 10) - 1); - this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(Math.ceil(sibs.length / 10) - 1 === -1 ? 0 : Math.ceil(sibs.length / 10) - 1, sibs); -} - -function displayPage(arr, num) { - for (let i = 0; i < arr.length; i++) { - if (Math.floor(i / 10) === num) { - arr[i].classList.remove('hexo-douban-hide'); - } else { - arr[i].classList.add('hexo-douban-hide'); - } - } -} - -function initPagination() { - let firstpages = document.getElementsByClassName("hexo-douban-firstpage"); - let previouspages = document.getElementsByClassName("hexo-douban-previouspage"); - let nextpages = document.getElementsByClassName("hexo-douban-nextpage"); - let lastpages = document.getElementsByClassName("hexo-douban-lastpage"); - let pagenums = document.getElementsByClassName("hexo-douban-pagenum"); - - for (let i = 0; i < firstpages.length; i++) { - //add listener - firstpages[i].onclick = firstBtn; - previouspages[i].onclick = previousBtn; - nextpages[i].onclick = nextBtn; - lastpages[i].onclick = lastBtn; - - //set page num - let size = pagenums[i].parentNode.siblings().length; - pagenums[i].innerText = '1 / ' + (Math.ceil(size / 10) === 0 ? 1 : Math.ceil(size / 10)); - firstpages[i].click(); - } -} - -initPagination(); diff --git a/lib/templates/song.ejs b/lib/templates/song.ejs index 5f04911..71ccc39 100644 --- a/lib/templates/song.ejs +++ b/lib/templates/song.ejs @@ -43,8 +43,5 @@ - +<%- include('index',{item_per_page:item_per_page}) -%> diff --git a/package.json b/package.json index cc24e09..15d171c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hexo-douban", - "version": "2.1.5", + "version": "2.1.6", "description": "Generate douban pages of books , movies and games for Hexo.", "main": "index", "scripts": {},