From ade254e66e17f140c4d749b7f72dcafcb6472245 Mon Sep 17 00:00:00 2001 From: ppoffice Date: Tue, 24 Dec 2019 00:36:39 -0500 Subject: [PATCH] refactor(*): all layout to jsx & rename dirs --- .eslintrc | 71 ++++++++++++++- .../common/ConfigGenerator.js | 0 .../common/ConfigValidator.js | 0 {includes => include}/common/utils.js | 0 include/filter/locals.js | 45 +++++++++ .../generator}/categories.js | 6 +- .../generator}/category.js | 12 +-- .../generator}/insight.js | 14 +-- .../generators => include/generator}/tags.js | 6 +- {includes/helpers => include/helper}/cdn.js | 29 +++--- include/helper/page.js | 31 +++++++ {includes => include}/specs/article.spec.js | 0 {includes => include}/specs/comment.spec.js | 0 {includes => include}/specs/config.spec.js | 0 {includes => include}/specs/donate.spec.js | 0 {includes => include}/specs/footer.spec.js | 0 {includes => include}/specs/icon_link.spec.js | 0 {includes => include}/specs/meta.spec.js | 0 {includes => include}/specs/navbar.spec.js | 0 {includes => include}/specs/plugins.spec.js | 0 {includes => include}/specs/providers.spec.js | 0 {includes => include}/specs/search.spec.js | 0 {includes => include}/specs/share.spec.js | 0 {includes => include}/specs/sidebar.spec.js | 0 {includes => include}/specs/widgets.spec.js | 0 .../tasks => include/task}/check_config.js | 2 +- .../tasks => include/task}/check_deps.js | 8 +- {includes/tasks => include/task}/welcome.js | 2 +- includes/helpers/config.js | 48 ---------- includes/helpers/page.js | 22 ----- layout/.eslintrc | 38 -------- layout/archive.jsx | 51 +++++------ layout/categories.jsx | 8 +- layout/category.jsx | 20 ++-- layout/comment/changyan.jsx | 4 +- layout/comment/disqus.jsx | 4 +- layout/comment/facebook.jsx | 4 +- layout/comment/gitalk.jsx | 4 +- layout/comment/gitment.jsx | 4 +- layout/comment/isso.jsx | 4 +- layout/comment/livere.jsx | 4 +- layout/comment/valine.jsx | 6 +- layout/common/article.jsx | 78 ++++++++-------- layout/common/comment.jsx | 12 +-- layout/common/donates.jsx | 14 ++- layout/common/footer.jsx | 28 +++--- layout/common/head.jsx | 91 ++++--------------- layout/common/navbar.jsx | 51 +++++------ layout/common/plugins.jsx | 6 +- layout/common/scripts.jsx | 2 - layout/common/search.jsx | 6 +- layout/common/share.jsx | 6 +- layout/common/widgets.jsx | 30 +++--- layout/donate/alipay.jsx | 12 +-- layout/donate/patreon.jsx | 10 +- layout/donate/paypal.jsx | 10 +- layout/donate/wechat.jsx | 12 +-- layout/index.jsx | 18 ++-- layout/layout.jsx | 22 ++--- layout/misc/meta.jsx | 2 - layout/misc/open_graph.jsx | 2 - layout/misc/paginator.jsx | 22 ++--- layout/page.jsx | 9 +- layout/plugin/animejs.jsx | 2 - layout/plugin/back_to_top.jsx | 4 +- layout/plugin/baidu_analytics.jsx | 2 - layout/plugin/busuanzi.jsx | 2 - layout/plugin/gallery.jsx | 2 - layout/plugin/google_analytics.jsx | 2 - layout/plugin/hotjar.jsx | 2 - layout/plugin/mathjax.jsx | 4 +- layout/plugin/outdated_browser.jsx | 4 +- layout/plugin/progressbar.jsx | 2 - layout/post.jsx | 9 +- layout/search/baidu.jsx | 14 ++- layout/search/google-cse.jsx | 16 ++-- layout/search/insight.jsx | 16 ++-- layout/share/addthis.jsx | 6 +- layout/share/addtoany.jsx | 16 ++-- layout/share/bdshare.jsx | 16 ++-- layout/share/sharejs.jsx | 4 +- layout/share/sharethis.jsx | 6 +- layout/tag.jsx | 18 ++-- layout/tags.jsx | 8 +- layout/util/cache.jsx | 2 - layout/util/classname.jsx | 12 +++ layout/widget/archives.jsx | 22 ++--- layout/widget/categories.jsx | 24 +++-- layout/widget/links.jsx | 22 ++--- layout/widget/profile.jsx | 52 +++++------ layout/widget/recent_posts.jsx | 30 +++--- layout/widget/subscribe_email.jsx | 28 +++--- layout/widget/tags.jsx | 20 ++-- layout/widget/toc.jsx | 22 ++--- package.json | 2 +- scripts/index.js | 29 +++--- source/js/main.js | 2 + 97 files changed, 583 insertions(+), 729 deletions(-) rename {includes => include}/common/ConfigGenerator.js (100%) rename {includes => include}/common/ConfigValidator.js (100%) rename {includes => include}/common/utils.js (100%) create mode 100644 include/filter/locals.js rename {includes/generators => include/generator}/categories.js (69%) rename {includes/generators => include/generator}/category.js (85%) rename {includes/generators => include/generator}/insight.js (81%) rename {includes/generators => include/generator}/tags.js (68%) rename {includes/helpers => include/helper}/cdn.js (71%) create mode 100644 include/helper/page.js rename {includes => include}/specs/article.spec.js (100%) rename {includes => include}/specs/comment.spec.js (100%) rename {includes => include}/specs/config.spec.js (100%) rename {includes => include}/specs/donate.spec.js (100%) rename {includes => include}/specs/footer.spec.js (100%) rename {includes => include}/specs/icon_link.spec.js (100%) rename {includes => include}/specs/meta.spec.js (100%) rename {includes => include}/specs/navbar.spec.js (100%) rename {includes => include}/specs/plugins.spec.js (100%) rename {includes => include}/specs/providers.spec.js (100%) rename {includes => include}/specs/search.spec.js (100%) rename {includes => include}/specs/share.spec.js (100%) rename {includes => include}/specs/sidebar.spec.js (100%) rename {includes => include}/specs/widgets.spec.js (100%) rename {includes/tasks => include/task}/check_config.js (92%) rename {includes/tasks => include/task}/check_deps.js (79%) rename {includes/tasks => include/task}/welcome.js (94%) delete mode 100644 includes/helpers/config.js delete mode 100644 includes/helpers/page.js delete mode 100644 layout/.eslintrc create mode 100644 layout/util/classname.jsx diff --git a/.eslintrc b/.eslintrc index 2033482..bab92d4 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,13 +1,80 @@ { - "extends": "hexo", - "root": true, + "extends": [ + "hexo", + "plugin:react/recommended" + ], + "settings": { + "node": { + "tryExtensions": [ + ".js", + ".jsx", + ".json" + ] + }, + "react": { + "version": "16.0" + } + }, + "parserOptions": { + "ecmaFeatures": { + "jsx": true + }, + "sourceType": "module" + }, + "plugins": [ + "react" + ], "rules": { + "react/jsx-uses-vars": "error", "indent": [ "error", 4, { "SwitchCase": 1 } + ], + "node/no-extraneous-require": [ + "error", + { + "allowModules": [ + "inferno", + "inferno-create-element", + "hexo", + "hexo-util", + "hexo-log", + "hexo-pagination", + "cheerio", + "moment" + ] + } + ], + "react/no-unknown-property": [ + "error", + { + "ignore": [ + "class", + "onclick", + "onsubmit" + ] + } + ], + "react/react-in-jsx-scope": [ + "off" + ], + "react/prop-types": [ + "off" + ], + "react/display-name": [ + "off" + ], + "react/jsx-key": [ + "off" + ], + "react/jsx-no-target-blank": [ + "error", + { + "allowReferrer": true + } ] } } \ No newline at end of file diff --git a/includes/common/ConfigGenerator.js b/include/common/ConfigGenerator.js similarity index 100% rename from includes/common/ConfigGenerator.js rename to include/common/ConfigGenerator.js diff --git a/includes/common/ConfigValidator.js b/include/common/ConfigValidator.js similarity index 100% rename from includes/common/ConfigValidator.js rename to include/common/ConfigValidator.js diff --git a/includes/common/utils.js b/include/common/utils.js similarity index 100% rename from includes/common/utils.js rename to include/common/utils.js diff --git a/include/filter/locals.js b/include/filter/locals.js new file mode 100644 index 0000000..11f7bb3 --- /dev/null +++ b/include/filter/locals.js @@ -0,0 +1,45 @@ +const createPostSchema = require('hexo/lib/models/post'); +const createPageSchema = require('hexo/lib/models/page'); + +module.exports = hexo => { + const RESERVED_KEYS = { + post: Object.keys(createPostSchema(hexo).paths), + page: Object.keys(createPageSchema(hexo).paths) + }; + + function getExtraConfig(source, reservedKeys) { + const result = {}; + for (const key in source) { + if (!key.startsWith('_') && !reservedKeys.includes(key) && typeof source[key] !== 'function') { + result[key] = source[key]; + } + } + return result; + } + + hexo.extend.filter.register('template_locals', locals => { + // inject helper functions + locals.helper = {}; + const helpers = hexo.extend.helper.list(); + for (const name in helpers) { + locals.helper[name] = helpers[name].bind(locals); + } + if (typeof locals.__ === 'function') { + locals.helper.__ = locals.__; + } + if (typeof locals._p === 'function') { + locals.helper._p = locals._p; + } + + // site config already merged into theme config in hexo/lib/hexo/index.js#Hexo.prototype._generateLocals() + locals.config = Object.assign({}, Object.getPrototypeOf(locals).theme); + // merge page configs + if (locals.page.__post === true) { + Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page)); + } else if (locals.page.__page === true) { + Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page)); + } + + return locals; + }); +}; diff --git a/includes/generators/categories.js b/include/generator/categories.js similarity index 69% rename from includes/generators/categories.js rename to include/generator/categories.js index c8eb846..e208ad4 100644 --- a/includes/generators/categories.js +++ b/include/generator/categories.js @@ -1,8 +1,8 @@ /** * Category list page generator */ -module.exports = function (hexo) { - hexo.extend.generator.register('categories', function (locals) { +module.exports = function(hexo) { + hexo.extend.generator.register('categories', locals => { return { path: 'categories/', layout: ['categories'], @@ -11,4 +11,4 @@ module.exports = function (hexo) { }) }; }); -} \ No newline at end of file +}; diff --git a/includes/generators/category.js b/include/generator/category.js similarity index 85% rename from includes/generators/category.js rename to include/generator/category.js index 0f019e3..a705ef7 100644 --- a/includes/generators/category.js +++ b/include/generator/category.js @@ -1,6 +1,6 @@ const pagination = require('hexo-pagination'); -module.exports = function (hexo) { +module.exports = function(hexo) { // ATTENTION: This will override the default category generator! hexo.extend.generator.register('category', function(locals) { const config = this.config; @@ -9,14 +9,14 @@ module.exports = function (hexo) { function findParent(category) { let parents = []; - if (category && category.hasOwnProperty('parent')) { + if (category && 'parent' in category) { const parent = locals.categories.filter(cat => cat._id === category.parent).first(); parents = [parent].concat(findParent(parent)); } return parents; } - - return locals.categories.reduce(function(result, category){ + + return locals.categories.reduce((result, category) => { const posts = category.posts.sort('-date'); const data = pagination(category.path, posts, { perPage: perPage, @@ -27,8 +27,8 @@ module.exports = function (hexo) { parents: findParent(category) } }); - + return result.concat(data); }, []); }); -} \ No newline at end of file +}; diff --git a/includes/generators/insight.js b/include/generator/insight.js similarity index 81% rename from includes/generators/insight.js rename to include/generator/insight.js index 88f7671..afe0275 100644 --- a/includes/generators/insight.js +++ b/include/generator/insight.js @@ -3,15 +3,15 @@ const util = require('hexo-util'); /** * Insight search content.json generator. */ -module.exports = function (hexo) { - hexo.extend.generator.register('insight', function (locals) { +module.exports = function(hexo) { + hexo.extend.generator.register('insight', function(locals) { const url_for = hexo.extend.helper.get('url_for').bind(this); function minify(str) { return util.stripHTML(str).trim().replace(/\n/g, ' ').replace(/\s+/g, ' ') - .replace(/&#x([\da-fA-F]+);/g, function (match, hex) { + .replace(/&#x([\da-fA-F]+);/g, (match, hex) => { return String.fromCharCode(parseInt(hex, 16)); }) - .replace(/&#([\d]+);/g, function (match, dec) { + .replace(/&#([\d]+);/g, (match, dec) => { return String.fromCharCode(dec); }); } @@ -20,14 +20,14 @@ module.exports = function (hexo) { title: post.title, text: minify(post.content), link: url_for(post.path) - } + }; } function tagMapper(tag) { return { name: tag.name, slug: tag.slug, link: url_for(tag.path) - } + }; } const site = { pages: locals.pages.map(postMapper), @@ -40,4 +40,4 @@ module.exports = function (hexo) { data: JSON.stringify(site) }; }); -} \ No newline at end of file +}; diff --git a/includes/generators/tags.js b/include/generator/tags.js similarity index 68% rename from includes/generators/tags.js rename to include/generator/tags.js index e6a8a92..87b9c7b 100644 --- a/includes/generators/tags.js +++ b/include/generator/tags.js @@ -1,8 +1,8 @@ /** * Tag list page generator */ -module.exports = function (hexo) { - hexo.extend.generator.register('tags', function (locals) { +module.exports = function(hexo) { + hexo.extend.generator.register('tags', locals => { return { path: 'tags/', layout: ['tags'], @@ -11,4 +11,4 @@ module.exports = function (hexo) { }) }; }); -} \ No newline at end of file +}; diff --git a/includes/helpers/cdn.js b/include/helper/cdn.js similarity index 71% rename from includes/helpers/cdn.js rename to include/helper/cdn.js index 8499e5f..242573c 100644 --- a/includes/helpers/cdn.js +++ b/include/helper/cdn.js @@ -20,9 +20,10 @@ const icon_providers = { fontawesome: 'https://use.fontawesome.com/releases/v5.4.1/css/all.css' }; -module.exports = function (hexo) { - hexo.extend.helper.register('cdn', function (_package, version, filename) { - let provider = hexo.extend.helper.get('get_config').bind(this)('providers.cdn'); +module.exports = function(hexo) { + hexo.extend.helper.register('cdn', function(_package, version, filename) { + let provider = this.config.provider && 'cdn' in this.config.provider ? this.config.provider.cdn : 'jsdelivr'; + // cdn.js does not follow a GitHub npm style like jsdeliver and unpkg do. Patch it! if (provider === 'cdnjs' || provider.startsWith('[cdnjs]')) { if (provider.startsWith('[cdnjs]')) { @@ -40,8 +41,8 @@ module.exports = function (hexo) { filename = filename.startsWith('outdatedbrowser/') ? filename.substr(16) : filename; } if (_package === 'highlight.js') { - filename = filename.endsWith('.css') && filename.indexOf('.min.') === -1 ? - filename.substr(0, filename.length - 4) + '.min.css' : filename; + filename = filename.endsWith('.css') && filename.indexOf('.min.') === -1 + ? filename.substr(0, filename.length - 4) + '.min.css' : filename; } if (_package === 'mathjax') { filename = filename.startsWith('unpacked/') ? filename.substr(9) : filename; @@ -53,7 +54,7 @@ module.exports = function (hexo) { _package = 'clipboard.js'; } } - if (provider !== null && cdn_providers.hasOwnProperty(provider)) { + if (provider !== null && provider in cdn_providers) { provider = cdn_providers[provider]; } return provider.replace(/\${\s*package\s*}/gi, _package) @@ -61,24 +62,24 @@ module.exports = function (hexo) { .replace(/\${\s*filename\s*}/gi, filename); }); - hexo.extend.helper.register('fontcdn', function (fontName, type = 'css') { - let provider = hexo.extend.helper.get('get_config').bind(this)('providers.fontcdn'); - if (provider !== null && font_providers.hasOwnProperty(provider)) { + hexo.extend.helper.register('fontcdn', function(fontName, type = 'css') { + let provider = this.config.provider && 'fontcdn' in this.config.provider ? this.config.provider.fontcdn : 'google'; + if (provider !== null && provider in font_providers) { provider = font_providers[provider]; } return provider.replace(/\${\s*fontname\s*}/gi, fontName) .replace(/\${\s*type\s*}/gi, type); }); - hexo.extend.helper.register('iconcdn', function (provider = null) { - if (provider !== null && icon_providers.hasOwnProperty(provider)) { + hexo.extend.helper.register('iconcdn', function(provider = null) { + if (provider !== null && provider in icon_providers) { provider = icon_providers[provider]; } else { - provider = hexo.extend.helper.get('get_config').bind(this)('providers.iconcdn'); - if (provider !== null && icon_providers.hasOwnProperty(provider)) { + provider = this.config.provider && 'iconcdn' in this.config.provider ? this.config.provider.iconcdn : 'fontawesome'; + if (provider !== null && provider in icon_providers) { provider = icon_providers[provider]; } } return provider; }); -} \ No newline at end of file +}; diff --git a/include/helper/page.js b/include/helper/page.js new file mode 100644 index 0000000..b64dc95 --- /dev/null +++ b/include/helper/page.js @@ -0,0 +1,31 @@ +/** +* Helper functions for page/post. +* +* @example +* <%- is_categories(page) %> +* <%- is_tags(page) %> +* <%- has_thumbnail(post) %> +* <%- get_thumbnail(post) %> +*/ +module.exports = function(hexo) { + hexo.extend.helper.register('is_categories', function(page = null) { + return (page === null ? this.page : page).__categories; + }); + + hexo.extend.helper.register('is_tags', function(page = null) { + return (page === null ? this.page : page).__tags; + }); + + hexo.extend.helper.register('has_thumbnail', function(post) { + const { article } = this.config; + if (article && article.thumbnail === false) { + return false; + } + return 'thumbnail' in post && post.thumbnail; + }); + + hexo.extend.helper.register('get_thumbnail', function(post) { + const { url_for, has_thumbnail } = this.helper; + return url_for(has_thumbnail.call(this, post) ? post.thumbnail : 'images/thumbnail.svg'); + }); +}; diff --git a/includes/specs/article.spec.js b/include/specs/article.spec.js similarity index 100% rename from includes/specs/article.spec.js rename to include/specs/article.spec.js diff --git a/includes/specs/comment.spec.js b/include/specs/comment.spec.js similarity index 100% rename from includes/specs/comment.spec.js rename to include/specs/comment.spec.js diff --git a/includes/specs/config.spec.js b/include/specs/config.spec.js similarity index 100% rename from includes/specs/config.spec.js rename to include/specs/config.spec.js diff --git a/includes/specs/donate.spec.js b/include/specs/donate.spec.js similarity index 100% rename from includes/specs/donate.spec.js rename to include/specs/donate.spec.js diff --git a/includes/specs/footer.spec.js b/include/specs/footer.spec.js similarity index 100% rename from includes/specs/footer.spec.js rename to include/specs/footer.spec.js diff --git a/includes/specs/icon_link.spec.js b/include/specs/icon_link.spec.js similarity index 100% rename from includes/specs/icon_link.spec.js rename to include/specs/icon_link.spec.js diff --git a/includes/specs/meta.spec.js b/include/specs/meta.spec.js similarity index 100% rename from includes/specs/meta.spec.js rename to include/specs/meta.spec.js diff --git a/includes/specs/navbar.spec.js b/include/specs/navbar.spec.js similarity index 100% rename from includes/specs/navbar.spec.js rename to include/specs/navbar.spec.js diff --git a/includes/specs/plugins.spec.js b/include/specs/plugins.spec.js similarity index 100% rename from includes/specs/plugins.spec.js rename to include/specs/plugins.spec.js diff --git a/includes/specs/providers.spec.js b/include/specs/providers.spec.js similarity index 100% rename from includes/specs/providers.spec.js rename to include/specs/providers.spec.js diff --git a/includes/specs/search.spec.js b/include/specs/search.spec.js similarity index 100% rename from includes/specs/search.spec.js rename to include/specs/search.spec.js diff --git a/includes/specs/share.spec.js b/include/specs/share.spec.js similarity index 100% rename from includes/specs/share.spec.js rename to include/specs/share.spec.js diff --git a/includes/specs/sidebar.spec.js b/include/specs/sidebar.spec.js similarity index 100% rename from includes/specs/sidebar.spec.js rename to include/specs/sidebar.spec.js diff --git a/includes/specs/widgets.spec.js b/include/specs/widgets.spec.js similarity index 100% rename from includes/specs/widgets.spec.js rename to include/specs/widgets.spec.js diff --git a/includes/tasks/check_config.js b/include/task/check_config.js similarity index 92% rename from includes/tasks/check_config.js rename to include/task/check_config.js index 1632ac1..79d1f78 100644 --- a/includes/tasks/check_config.js +++ b/include/task/check_config.js @@ -38,7 +38,7 @@ try { } } else if (e instanceof errors.VersionError) { logger.error(e.message); - logger.warn(`To let us create a fresh configuration file for you, please rename or delete the following file:`); + logger.warn('To let us create a fresh configuration file for you, please rename or delete the following file:'); logger.warn(CONFIG_PATH); } else { throw e; diff --git a/includes/tasks/check_deps.js b/include/task/check_deps.js similarity index 79% rename from includes/tasks/check_deps.js rename to include/task/check_deps.js index 5115582..b971041 100644 --- a/includes/tasks/check_deps.js +++ b/include/task/check_deps.js @@ -4,8 +4,8 @@ function checkDependency(name) { try { require.resolve(name); return true; - } catch(e) { - logger.error(`Package ${name} is not installed.`) + } catch (e) { + logger.error(`Package ${name} is not installed.`); } return false; } @@ -17,9 +17,9 @@ const missingDeps = [ 'cheerio', 'hexo-util', 'hexo-log', - 'hexo-pagination', + 'hexo-pagination' ].map(checkDependency).some(installed => !installed); if (missingDeps) { logger.error('Please install the missing dependencies from the root directory of your Hexo site.'); - process.exit(-1); + throw new Error(); } diff --git a/includes/tasks/welcome.js b/include/task/welcome.js similarity index 94% rename from includes/tasks/welcome.js rename to include/task/welcome.js index 264f15e..ea4be5b 100644 --- a/includes/tasks/welcome.js +++ b/include/task/welcome.js @@ -7,4 +7,4 @@ logger.info(`======================================= ██║██║ ██╔══██║██╔══██╗██║ ██║╚════██║ ██║╚██████╗██║ ██║██║ ██║╚██████╔╝███████║ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ -=============================================`); \ No newline at end of file +=============================================`); diff --git a/includes/helpers/config.js b/includes/helpers/config.js deleted file mode 100644 index d29bcd7..0000000 --- a/includes/helpers/config.js +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Theme configuration helpers. - * - * @description Test if a configuration is set or fetch its value. If `exclude_page` is set, the helpers will - * not look up configurations in the current page's front matter. - * @example - * <%- has_config(config_name, exclude_page) %> - * <%- get_config(config_name, default_value, exclude_page) %> - */ -const specs = require('../specs/config.spec'); -const descriptors = require('../common/utils').descriptors; - -module.exports = function (hexo) { - function readProperty(object, path) { - const paths = path.split('.'); - for (let path of paths) { - if (typeof (object) === 'undefined' || object === null || !object.hasOwnProperty(path)) { - return null; - } - object = object[path]; - } - return object; - } - - hexo.extend.helper.register('get_config', function (configName, defaultValue = undefined, excludePage = false) { - const value = readProperty(Object.assign({}, this.config, hexo.theme.config, - !excludePage ? this.page : {}), configName); - if (value === null) { - if (typeof(defaultValue) !== 'undefined') { - return defaultValue; - } else { - const property = readProperty(specs, configName); - return property === null ? null : property[descriptors.defaultValue]; - } - } - return value; - }); - - hexo.extend.helper.register('has_config', function (configName, excludePage = false) { - const readProperty = hexo.extend.helper.get('get_config').bind(this); - return readProperty(configName, null, excludePage) != null; - }); - - hexo.extend.helper.register('get_config_from_obj', function (object, configName, defaultValue = null) { - const value = readProperty(object, configName); - return value === null ? defaultValue : value; - }); -} \ No newline at end of file diff --git a/includes/helpers/page.js b/includes/helpers/page.js deleted file mode 100644 index 8dd7445..0000000 --- a/includes/helpers/page.js +++ /dev/null @@ -1,22 +0,0 @@ -/** -* Helper functions for page/post. -* -* @example -* <%- has_thumbnail(post) %> -* <%- get_thumbnail(post) %> -*/ -module.exports = function (hexo) { - hexo.extend.helper.register('has_thumbnail', function (post) { - const getConfig = hexo.extend.helper.get('get_config').bind(this); - const allowThumbnail = getConfig('article.thumbnail', true); - if (!allowThumbnail) { - return false; - } - return post.hasOwnProperty('thumbnail') && post.thumbnail; - }); - - hexo.extend.helper.register('get_thumbnail', function (post) { - const hasThumbnail = hexo.extend.helper.get('has_thumbnail').bind(this)(post); - return this.url_for(hasThumbnail ? post.thumbnail : 'images/thumbnail.svg'); - }); -} diff --git a/layout/.eslintrc b/layout/.eslintrc deleted file mode 100644 index 9605cd0..0000000 --- a/layout/.eslintrc +++ /dev/null @@ -1,38 +0,0 @@ -{ - "extends": [ - "../.eslintrc" - ], - "settings": { - "node": { - "tryExtensions": [ - ".js", - ".jsx", - ".json" - ] - } - }, - "parserOptions": { - "ecmaFeatures": { - "jsx": true - } - }, - "plugins": [ - "react" - ], - "rules": { - "react/jsx-uses-vars": "error", - "node/no-extraneous-require": [ - "error", - { - "allowModules": [ - "inferno", - "inferno-create-element", - "hexo-util", - "hexo-log", - "cheerio", - "moment" - ] - } - ] - } -} \ No newline at end of file diff --git a/layout/archive.jsx b/layout/archive.jsx index c4bce4a..7455adc 100644 --- a/layout/archive.jsx +++ b/layout/archive.jsx @@ -1,15 +1,10 @@ -'use strict'; - const moment = require('moment'); const { Component, Fragment } = require('inferno'); const Paginator = require('./misc/paginator'); module.exports = class extends Component { render() { - const { page } = this.props; - // TODO - const helper = {}; - const config = {}; + const { config, page, helper } = this.props; const { url_for, __, has_thumbnail, get_thumbnail, date_xml, date } = helper; const language = page.lang || page.language || config.language; @@ -17,40 +12,40 @@ module.exports = class extends Component { function renderArticleList(posts, year, month = null) { const time = moment([page.year, page.month ? page.month - 1 : null].filter(i => i !== null)); - return
-
-

{month === null ? year : time.locale(language).format('MMMM YYYY')}

-
+ return
+
+ +
{posts.map(post => { const categories = []; post.categories.forEach((category, i) => { - categories.push({category.name}); + categories.push({category.name}); if (i < post.categories.length - 1) { categories.push('/'); } }); - return ; })}
@@ -60,9 +55,9 @@ module.exports = class extends Component { let articleList; if (!page.year) { const years = {}; - page.posts.each(p => years[p.date.year()] = null); + page.posts.each(p => { years[p.date.year()] = null; }); articleList = Object.keys(years).sort((a, b) => b - a).map(year => { - let posts = page.posts.filter(p => p.date.year() == year); + const posts = page.posts.filter(p => p.date.year() === parseInt(year, 10)); return renderArticleList(posts, year, null); }); } else { @@ -81,4 +76,4 @@ module.exports = class extends Component { nextTitle={__('common.next')} /> : null} ; } -} \ No newline at end of file +}; diff --git a/layout/categories.jsx b/layout/categories.jsx index 04ce414..acb8922 100644 --- a/layout/categories.jsx +++ b/layout/categories.jsx @@ -1,14 +1,10 @@ -'use strict'; - const { Component } = require('inferno'); const Categories = require('./widget/categories'); module.exports = class extends Component { render() { - const { site, page } = this.props; - // TODO - const helper = {}; + const { site, page, helper } = this.props; return ; } -} \ No newline at end of file +}; diff --git a/layout/category.jsx b/layout/category.jsx index fa23339..177041a 100644 --- a/layout/category.jsx +++ b/layout/category.jsx @@ -1,30 +1,26 @@ -'use strict'; - const { Component, Fragment } = require('inferno'); const Index = require('./index'); module.exports = class extends Component { render() { - const { page } = this.props; - // TODO - const helper = {}; + const { config, page, helper } = this.props; const { url_for, _p } = helper; return -
-
-