Skip to content

Commit

Permalink
Merge pull request #1248 from WolfgangKluge/category-show-current
Browse files Browse the repository at this point in the history
Add option to indicate current category with class name `current`
  • Loading branch information
tommy351 committed May 5, 2015
2 parents f4270c4 + 7b95c57 commit eff1429
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/plugins/helper/list_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,14 +46,32 @@ 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;
}

result += '<li class="' + className + '-list-item' + additionalClassName + '">';

result += '<a class="' + className + '-list-link" href="' + self.url_for(cat.path) + '">';
result += '<a class="' + className + '-list-link' + (isCurrent ? ' current' : '') + '" href="' + self.url_for(cat.path) + '">';
result += transform ? transform(cat.name) : cat.name;
result += '</a>';

Expand Down
23 changes: 23 additions & 0 deletions test/scripts/helpers/list_categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
});

Expand Down Expand Up @@ -250,4 +251,26 @@ describe('list_categories', function(){
'</ul>'
].join(''));
});

it('show-current', function(){
var result = listCategories({
show_current: true
});

result.should.eql([
'<ul class="category-list">',
'<li class="category-list-item">',
'<a class="category-list-link current" href="/categories/baz/">baz</a><span class="category-list-count">3</span>',
'<ul class="category-list-child">',
'<li class="category-list-item">',
'<a class="category-list-link current" href="/categories/baz/bar/">bar</a><span class="category-list-count">1</span>',
'</li>',
'</ul>',
'</li>',
'<li class="category-list-item">',
'<a class="category-list-link" href="/categories/foo/">foo</a><span class="category-list-count">1</span>',
'</li>',
'</ul>'
].join(''));
});
});

0 comments on commit eff1429

Please sign in to comment.