From 7b95c57b41f4827948bea55f1eed5b86f291b015 Mon Sep 17 00:00:00 2001 From: Wolfgang Kluge Date: Fri, 1 May 2015 16:53:58 +0200 Subject: [PATCH] Add option to indicate current category with class name `current` Currently, this is handled by client-script, if needed. --- lib/plugins/helper/list_categories.js | 21 ++++++++++++++++++++- test/scripts/helpers/list_categories.js | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/plugins/helper/list_categories.js b/lib/plugins/helper/list_categories.js index eac2476661..b6bbfa6670 100644 --- a/lib/plugins/helper/list_categories.js +++ b/lib/plugins/helper/list_categories.js @@ -18,6 +18,7 @@ function listCategoriesHelper(categories, options){ var order = options.order || 1; var transform = options.transform; var separator = options.hasOwnProperty('separator') ? options.separator : ', '; + var showCurrent = options.show_current || false; var childrenIndicator = options.hasOwnProperty('children_indicator') ? options.children_indicator : false; var result = ''; var self = this; @@ -45,6 +46,24 @@ function listCategoriesHelper(categories, options){ child = hierarchicalList(level + 1, cat._id); } + var isCurrent = false; + if (showCurrent && self.page) { + for (var j = 0; j < cat.length; j++) { + var post = cat.posts.data[j]; + if (post && post._id === self.page._id) { + isCurrent = true; + break; + } + } + + // special case: category page + if (!isCurrent && self.page.base) { + if (self.page.base.indexOf(cat.path) === 0) { + isCurrent = true; + } + } + } + var additionalClassName = ''; if (child && childrenIndicator){ additionalClassName = ' ' + childrenIndicator; @@ -52,7 +71,7 @@ function listCategoriesHelper(categories, options){ result += '
  • '; - result += ''; + result += ''; result += transform ? transform(cat.name) : cat.name; result += ''; diff --git a/test/scripts/helpers/list_categories.js b/test/scripts/helpers/list_categories.js index 2a2dcb0820..e369ede862 100644 --- a/test/scripts/helpers/list_categories.js +++ b/test/scripts/helpers/list_categories.js @@ -35,6 +35,7 @@ describe('list_categories', function(){ }).then(function(){ hexo.locals.invalidate(); ctx.site = hexo.locals.toObject(); + ctx.page = ctx.site.posts.data[1]; }); }); @@ -250,4 +251,26 @@ describe('list_categories', function(){ '' ].join('')); }); + + it('show-current', function(){ + var result = listCategories({ + show_current: true + }); + + result.should.eql([ + '' + ].join('')); + }); });