diff --git a/README.md b/README.md index 175a0ea..c4a37b4 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ bangumi: # 追番设置 lazyload: true loading: showMyComment: false + pagination: false metaColor: color: webp: @@ -78,6 +79,7 @@ cinema: # 追剧设置 - **progress**: 获取番剧数据时是否显示进度条,默认`true` - **extraOrder**: 手动添加的番剧/追剧数据是否优先显示,`1`为优先,其它为不优先 - **showMyComment**: 使用`bgm`源时显示自己的评价及评论,默认`false` +- **pagination**: 分页优化,只将第一页的数据渲染到`html`文件中,其余数据将通过异步请求加载,避免番剧过多时html文件过大导致页面加载缓慢,建议番剧较多时使用,默认`false` - **extra_options**: 此配置会扩展到Hexo`page`变量中 ## 使用 diff --git a/index.js b/index.js index cf5a1ff..1df83e0 100644 --- a/index.js +++ b/index.js @@ -72,11 +72,26 @@ hexo.extend.console.register('bangumi', 'Generate pages of bangumis for Hexo', o if (this.config.bangumi.source === 'bgm') { var _this$config$bangumi$; - getBgmData(this.config.bangumi.vmid, (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true, this.source_dir, this.config.bangumi.proxy); + getBgmData({ + vmid: this.config.bangumi.vmid, + showProgress: (_this$config$bangumi$ = this.config.bangumi.progress) !== null && _this$config$bangumi$ !== void 0 ? _this$config$bangumi$ : true, + sourceDir: this.source_dir, + extraOrder: this.config.bangumi.extraOrder, + pagination: this.config.bangumi.pagination, + proxy: this.config.bangumi.proxy + }); } else { var _this$config$bangumi$2; - getBiliData(this.config.bangumi.vmid, 'bangumi', (_this$config$bangumi$2 = this.config.bangumi.progress) !== null && _this$config$bangumi$2 !== void 0 ? _this$config$bangumi$2 : true, this.source_dir, this.config.bangumi.webp); + getBiliData({ + vmid: this.config.bangumi.vmid, + type: 'bangumi', + showProgress: (_this$config$bangumi$2 = this.config.bangumi.progress) !== null && _this$config$bangumi$2 !== void 0 ? _this$config$bangumi$2 : true, + sourceDir: this.source_dir, + extraOrder: this.config.bangumi.extraOrder, + pagination: this.config.bangumi.pagination, + useWebp: this.config.bangumi.webp + }); } } else { log.info('Unknown command, please use "hexo bangumi -h" to see the available commands'); @@ -105,7 +120,15 @@ hexo.extend.console.register('cinema', 'Generate pages of bilibili cinemas for H return; } - getBiliData(this.config.cinema.vmid, 'cinema', (_this$config$cinema$p = this.config.cinema.progress) !== null && _this$config$cinema$p !== void 0 ? _this$config$cinema$p : true, this.source_dir, this.config.cinema.webp); + getBiliData({ + vmid: this.config.cinema.vmid, + type: 'cinema', + showProgress: (_this$config$cinema$p = this.config.cinema.progress) !== null && _this$config$cinema$p !== void 0 ? _this$config$cinema$p : true, + sourceDir: this.source_dir, + extraOrder: this.config.cinema.extraOrder, + pagination: this.config.cinema.pagination, + useWebp: this.config.cinema.webp + }); } else { log.info('Unknown command, please use "hexo cinema -h" to see the available commands'); } diff --git a/lib/bangumi-generator.js b/lib/bangumi-generator.js index 1b07cb9..a0af65a 100644 --- a/lib/bangumi-generator.js +++ b/lib/bangumi-generator.js @@ -30,7 +30,7 @@ var log = require('hexo-log')({ module.exports = /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(locals) { - var _config$type, _config$type$lazyload, _config$type$source, _config$type$showMyCo, _config$type2; + var _config$type, _config$type$lazyload, _config$type$source, _config$type$showMyCo, _config$type$paginati, _config$type2; var type, config, @@ -126,6 +126,8 @@ module.exports = /*#__PURE__*/function () { lazyload: (_config$type$lazyload = config[type].lazyload) !== null && _config$type$lazyload !== void 0 ? _config$type$lazyload : true, source: (_config$type$source = config[type].source) !== null && _config$type$source !== void 0 ? _config$type$source : 'bili', showMyComment: (_config$type$showMyCo = config[type].showMyComment) !== null && _config$type$showMyCo !== void 0 ? _config$type$showMyCo : false, + pagination: (_config$type$paginati = config[type].pagination) !== null && _config$type$paginati !== void 0 ? _config$type$paginati : false, + ejsTemplate: fs.readFileSync(path.join(__dirname, "templates/".concat(config[type].source === 'bgm' ? 'bgm' : 'bili', "-template.ejs"))).toString().replace('class="bangumi-item"', 'class="bangumi-item bangumi-hide"'), wantWatch: wantWatch, watched: watched, watching: watching, diff --git a/lib/get-bgm-data.js b/lib/get-bgm-data.js index 615fa72..610e1f7 100644 --- a/lib/get-bgm-data.js +++ b/lib/get-bgm-data.js @@ -4,10 +4,16 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); +var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); + var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } + /* eslint-disable no-underscore-dangle */ var fs = require('hexo-fs'); @@ -390,28 +396,30 @@ var getBangumiData = /*#__PURE__*/function () { }(); module.exports.getBgmData = /*#__PURE__*/function () { - var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid, showProgress, sourceDir, proxy) { - var startTime, wantWatch, watching, watched, endTime, bangumis; + var _getBgmData = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(_ref6) { + var vmid, showProgress, sourceDir, extraOrder, pagination, proxy, startTime, wantWatch, watching, watched, endTime, bangumis, allBangumis, _JSON$parse, wantWatchExtra, watchingExtra, watchedExtra; + return _regenerator["default"].wrap(function _callee4$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: + vmid = _ref6.vmid, showProgress = _ref6.showProgress, sourceDir = _ref6.sourceDir, extraOrder = _ref6.extraOrder, pagination = _ref6.pagination, proxy = _ref6.proxy; log.info('Getting bangumis, please wait...'); startTime = new Date().getTime(); - _context5.next = 4; + _context5.next = 5; return getItemsId(vmid, 'wish', showProgress, sourceDir, proxy); - case 4: + case 5: wantWatch = _context5.sent; - _context5.next = 7; + _context5.next = 8; return getItemsId(vmid, 'do', showProgress, sourceDir, proxy); - case 7: + case 8: watching = _context5.sent; - _context5.next = 10; + _context5.next = 11; return getItemsId(vmid, 'collect', showProgress, sourceDir, proxy); - case 10: + case 11: watched = _context5.sent; endTime = new Date().getTime(); log.info("".concat(wantWatch.length + watching.length + watched.length, " bangumis have been loaded in ").concat(endTime - startTime, " ms")); @@ -427,22 +435,63 @@ module.exports.getBgmData = /*#__PURE__*/function () { fs.writeFile(path.join(sourceDir, '/_data/bangumis.json'), JSON.stringify(bangumis), function (err) { if (err) { - log.info('Failed to write data to bangumis.json'); + log.info('Failed to write data to _data/bangumis.json'); console.error(err); } else { log.info('Bangumi bangumis data has been saved'); } }); - case 16: + if (pagination) { + allBangumis = _objectSpread({}, bangumis); // extra bangumis + + if (fs.existsSync(path.join(sourceDir, '/_data/extra_bangumis.json'))) { + _JSON$parse = JSON.parse(fs.readFileSync(path.join(this.source_dir, "/_data/extra_".concat(type, "s.json")))), wantWatchExtra = _JSON$parse.wantWatchExtra, watchingExtra = _JSON$parse.watchingExtra, watchedExtra = _JSON$parse.watchedExtra; + + if (wantWatchExtra) { + if (extraOrder === 1) { + allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(wantWatchExtra), (0, _toConsumableArray2["default"])(allBangumis.wantWatch)); + } else { + allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(allBangumis.wantWatch), (0, _toConsumableArray2["default"])(wantWatchExtra)); + } + } + + if (watchingExtra) { + if (extraOrder === 1) { + allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(watchingExtra), (0, _toConsumableArray2["default"])(allBangumis.watching)); + } else { + allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(allBangumis.watching), (0, _toConsumableArray2["default"])(watchingExtra)); + } + } + + if (watchedExtra) { + if (extraOrder === 1) { + allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(watchedExtra), (0, _toConsumableArray2["default"])(allBangumis.watched)); + } else { + allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(allBangumis.watched), (0, _toConsumableArray2["default"])(watchedExtra)); + } + } + } + + fs.writeFile(path.join(sourceDir, '/bangumis.json'), JSON.stringify(allBangumis), function (err) { + if (err) { + log.info('Failed to write data to bangumis.json'); + console.error(err); + } else { + log.info('Bangumi bangumis data has been saved'); + } + }); + } + + case 18: case "end": return _context5.stop(); } } - }, _callee4); + }, _callee4, this); })); - function getBgmData(_x10, _x11, _x12, _x13) { + function getBgmData(_x10) { return _getBgmData.apply(this, arguments); } diff --git a/lib/get-bili-data.js b/lib/get-bili-data.js index 55e1053..2fae7c9 100644 --- a/lib/get-bili-data.js +++ b/lib/get-bili-data.js @@ -2,12 +2,20 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); +var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); + var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); +var _this = void 0; + +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } + function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } @@ -249,21 +257,14 @@ var processData = /*#__PURE__*/function () { }(); module.exports.getBiliData = /*#__PURE__*/function () { - var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(vmid, type, showProgress, sourceDir) { - var useWebp, - typeNum, - startTime, - wantWatch, - watching, - watched, - endTime, - bangumis, - _args4 = arguments; + var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(_ref4) { + var vmid, type, showProgress, sourceDir, extraOrder, pagination, _ref4$useWebp, useWebp, typeNum, startTime, wantWatch, watching, watched, endTime, bangumis, allBangumis, _JSON$parse, wantWatchExtra, watchingExtra, watchedExtra; + return _regenerator["default"].wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: - useWebp = _args4.length > 4 && _args4[4] !== undefined ? _args4[4] : true; + vmid = _ref4.vmid, type = _ref4.type, showProgress = _ref4.showProgress, sourceDir = _ref4.sourceDir, extraOrder = _ref4.extraOrder, pagination = _ref4.pagination, _ref4$useWebp = _ref4.useWebp, useWebp = _ref4$useWebp === void 0 ? true : _ref4$useWebp; typeNum = type === 'cinema' ? 2 : 1; log.info("Getting bilibili ".concat(type, ", please wait...")); startTime = new Date().getTime(); @@ -296,14 +297,55 @@ module.exports.getBiliData = /*#__PURE__*/function () { fs.writeFile(path.join(sourceDir, "/_data/".concat(type, "s.json")), JSON.stringify(bangumis), function (err) { if (err) { - log.info("Failed to write data to ".concat(type, "s.json")); + log.info("Failed to write data to _data/".concat(type, "s.json")); console.error(err); } else { log.info("Bilibili ".concat(type, "s data has been saved")); } }); - case 18: + if (pagination) { + allBangumis = _objectSpread({}, bangumis); // extra bangumis + + if (fs.existsSync(path.join(sourceDir, "/_data/extra_".concat(type, "s.json")))) { + _JSON$parse = JSON.parse(fs.readFileSync(path.join(_this.source_dir, "/_data/extra_".concat(type, "s.json")))), wantWatchExtra = _JSON$parse.wantWatchExtra, watchingExtra = _JSON$parse.watchingExtra, watchedExtra = _JSON$parse.watchedExtra; + + if (wantWatchExtra) { + if (extraOrder === 1) { + allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(wantWatchExtra), (0, _toConsumableArray2["default"])(allBangumis.wantWatch)); + } else { + allBangumis.wantWatch = [].concat((0, _toConsumableArray2["default"])(allBangumis.wantWatch), (0, _toConsumableArray2["default"])(wantWatchExtra)); + } + } + + if (watchingExtra) { + if (extraOrder === 1) { + allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(watchingExtra), (0, _toConsumableArray2["default"])(allBangumis.watching)); + } else { + allBangumis.watching = [].concat((0, _toConsumableArray2["default"])(allBangumis.watching), (0, _toConsumableArray2["default"])(watchingExtra)); + } + } + + if (watchedExtra) { + if (extraOrder === 1) { + allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(watchedExtra), (0, _toConsumableArray2["default"])(allBangumis.watched)); + } else { + allBangumis.watched = [].concat((0, _toConsumableArray2["default"])(allBangumis.watched), (0, _toConsumableArray2["default"])(watchedExtra)); + } + } + } + + fs.writeFile(path.join(sourceDir, "/".concat(type, "s.json")), JSON.stringify(bangumis), function (err) { + if (err) { + log.info("Failed to write data to ".concat(type, "s.json")); + console.error(err); + } else { + log.info("Bilibili ".concat(type, "s data has been saved")); + } + }); + } + + case 19: case "end": return _context4.stop(); } @@ -311,7 +353,7 @@ module.exports.getBiliData = /*#__PURE__*/function () { }, _callee4); })); - return function (_x14, _x15, _x16, _x17) { - return _ref4.apply(this, arguments); + return function (_x14) { + return _ref5.apply(this, arguments); }; }(); diff --git a/lib/templates/bangumi.ejs b/lib/templates/bangumi.ejs index 9524450..98ea1be 100644 --- a/lib/templates/bangumi.ejs +++ b/lib/templates/bangumi.ejs @@ -3,6 +3,10 @@
<%- quote; %>
<% } %> +<% if (pagination) { %> + + +<% } %> @@ -19,25 +23,56 @@