Skip to content

Commit

Permalink
feat(helper/tagcloud): show_count option (#5047) (#5048)
Browse files Browse the repository at this point in the history
  • Loading branch information
renbaoshuo authored Sep 4, 2022
1 parent d95d297 commit d9e5f2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/plugins/helper/tagcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function tagcloudHelper(tags, options) {
const unit = options.unit || 'px';
const color = options.color;
const className = options.class;
const showCount = options.show_count;
const countClassName = options.count_class || 'count';
const level = options.level || 10;
const { transform } = options;
const separator = options.separator || ' ';
Expand Down Expand Up @@ -68,7 +70,7 @@ function tagcloudHelper(tags, options) {
}

result.push(
`<a href="${url_for.call(this, tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}</a>`
`<a href="${url_for.call(this, tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}${showCount ? `<span class="${countClassName}">${tag.length}</span>` : ''}</a>`
);
});

Expand Down
22 changes: 22 additions & 0 deletions test/scripts/helpers/tagcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,26 @@ describe('tagcloud', () => {
'<a href="/tags/def/" style="font-size: 10px;" class="tag-cloud-0">def</a>'
].join(' '));
});

it('show_count', () => {
const result = tagcloud({ show_count: true });

result.should.eql([
'<a href="/tags/abc/" style="font-size: 13.33px;">abc<span class="count">2</span></a>',
'<a href="/tags/bcd/" style="font-size: 20px;">bcd<span class="count">4</span></a>',
'<a href="/tags/cde/" style="font-size: 16.67px;">cde<span class="count">3</span></a>',
'<a href="/tags/def/" style="font-size: 10px;">def<span class="count">1</span></a>'
].join(' '));
});

it('show_count with custom class', () => {
const result = tagcloud({ show_count: true, count_class: 'tag-count' });

result.should.eql([
'<a href="/tags/abc/" style="font-size: 13.33px;">abc<span class="tag-count">2</span></a>',
'<a href="/tags/bcd/" style="font-size: 20px;">bcd<span class="tag-count">4</span></a>',
'<a href="/tags/cde/" style="font-size: 16.67px;">cde<span class="tag-count">3</span></a>',
'<a href="/tags/def/" style="font-size: 10px;">def<span class="tag-count">1</span></a>'
].join(' '));
});
});

0 comments on commit d9e5f2e

Please sign in to comment.