From 409d95d72364a692b4d622b3d3749d73df20c167 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 23 Jun 2022 21:26:44 +0800 Subject: [PATCH 1/2] feat(helper/is): add `is_index()` helper --- lib/plugins/helper/index.js | 1 + lib/plugins/helper/is.js | 5 +++++ test/scripts/helpers/is.js | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/lib/plugins/helper/index.js b/lib/plugins/helper/index.js index c7dc22b531..4bf9477ea7 100644 --- a/lib/plugins/helper/index.js +++ b/lib/plugins/helper/index.js @@ -31,6 +31,7 @@ module.exports = ctx => { const is = require('./is'); helper.register('is_current', is.current); helper.register('is_home', is.home); + helper.register('is_index', is.index); helper.register('is_post', is.post); helper.register('is_page', is.page); helper.register('is_archive', is.archive); diff --git a/lib/plugins/helper/is.js b/lib/plugins/helper/is.js index 2724428aef..b8302c4b15 100644 --- a/lib/plugins/helper/is.js +++ b/lib/plugins/helper/is.js @@ -23,6 +23,10 @@ function isHomeHelper() { return Boolean(this.page.__index); } +function isIndexHelper() { + return Boolean(this.page.__index) && this.page.current === 1; +} + function isPostHelper() { return Boolean(this.page.__post); } @@ -79,6 +83,7 @@ function isTagHelper(tag) { exports.current = isCurrentHelper; exports.home = isHomeHelper; +exports.index = isIndexHelper; exports.post = isPostHelper; exports.page = isPageHelper; exports.archive = isArchiveHelper; diff --git a/test/scripts/helpers/is.js b/test/scripts/helpers/is.js index 02a43808f9..53663e9256 100644 --- a/test/scripts/helpers/is.js +++ b/test/scripts/helpers/is.js @@ -25,6 +25,13 @@ describe('is', () => { await is.home.call({page: {}}).should.be.false; }); + it('is_index', async () => { + await is.index.call({page: {__index: true, current: 1}}).should.be.true; + await is.index.call({page: {__index: true, current: 2}}).should.be.false; + await is.index.call({page: {__index: true}}).should.be.false; + await is.index.call({page: {}}).should.be.false; + }); + it('is_post', async () => { await is.post.call({page: {__post: true}}).should.be.true; await is.post.call({page: {}}).should.be.false; From 9d5d679e0dffbfe641974dbd53ce3e578d01d242 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 23 Jun 2022 21:37:04 +0800 Subject: [PATCH 2/2] fix issue comment --- lib/plugins/helper/index.js | 2 +- lib/plugins/helper/is.js | 4 ++-- test/scripts/helpers/is.js | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/plugins/helper/index.js b/lib/plugins/helper/index.js index 4bf9477ea7..fc86aa6074 100644 --- a/lib/plugins/helper/index.js +++ b/lib/plugins/helper/index.js @@ -31,7 +31,7 @@ module.exports = ctx => { const is = require('./is'); helper.register('is_current', is.current); helper.register('is_home', is.home); - helper.register('is_index', is.index); + helper.register('is_home_first_page', is.home_first_page); helper.register('is_post', is.post); helper.register('is_page', is.page); helper.register('is_archive', is.archive); diff --git a/lib/plugins/helper/is.js b/lib/plugins/helper/is.js index b8302c4b15..9f74f3b6d6 100644 --- a/lib/plugins/helper/is.js +++ b/lib/plugins/helper/is.js @@ -23,7 +23,7 @@ function isHomeHelper() { return Boolean(this.page.__index); } -function isIndexHelper() { +function isHomeFirstPageHelper() { return Boolean(this.page.__index) && this.page.current === 1; } @@ -83,7 +83,7 @@ function isTagHelper(tag) { exports.current = isCurrentHelper; exports.home = isHomeHelper; -exports.index = isIndexHelper; +exports.home_first_page = isHomeFirstPageHelper; exports.post = isPostHelper; exports.page = isPageHelper; exports.archive = isArchiveHelper; diff --git a/test/scripts/helpers/is.js b/test/scripts/helpers/is.js index 53663e9256..fa8acd9551 100644 --- a/test/scripts/helpers/is.js +++ b/test/scripts/helpers/is.js @@ -25,11 +25,11 @@ describe('is', () => { await is.home.call({page: {}}).should.be.false; }); - it('is_index', async () => { - await is.index.call({page: {__index: true, current: 1}}).should.be.true; - await is.index.call({page: {__index: true, current: 2}}).should.be.false; - await is.index.call({page: {__index: true}}).should.be.false; - await is.index.call({page: {}}).should.be.false; + it('is_home_first_page', async () => { + await is.home_first_page.call({page: {__index: true, current: 1}}).should.be.true; + await is.home_first_page.call({page: {__index: true, current: 2}}).should.be.false; + await is.home_first_page.call({page: {__index: true}}).should.be.false; + await is.home_first_page.call({page: {}}).should.be.false; }); it('is_post', async () => {