Skip to content

Commit d9e5f2e

Browse files
authored
feat(helper/tagcloud): show_count option (#5047) (#5048)
1 parent d95d297 commit d9e5f2e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/plugins/helper/tagcloud.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ function tagcloudHelper(tags, options) {
1919
const unit = options.unit || 'px';
2020
const color = options.color;
2121
const className = options.class;
22+
const showCount = options.show_count;
23+
const countClassName = options.count_class || 'count';
2224
const level = options.level || 10;
2325
const { transform } = options;
2426
const separator = options.separator || ' ';
@@ -68,7 +70,7 @@ function tagcloudHelper(tags, options) {
6870
}
6971

7072
result.push(
71-
`<a href="${url_for.call(this, tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}</a>`
73+
`<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>`
7274
);
7375
});
7476

test/scripts/helpers/tagcloud.js

+22
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,26 @@ describe('tagcloud', () => {
290290
'<a href="/tags/def/" style="font-size: 10px;" class="tag-cloud-0">def</a>'
291291
].join(' '));
292292
});
293+
294+
it('show_count', () => {
295+
const result = tagcloud({ show_count: true });
296+
297+
result.should.eql([
298+
'<a href="/tags/abc/" style="font-size: 13.33px;">abc<span class="count">2</span></a>',
299+
'<a href="/tags/bcd/" style="font-size: 20px;">bcd<span class="count">4</span></a>',
300+
'<a href="/tags/cde/" style="font-size: 16.67px;">cde<span class="count">3</span></a>',
301+
'<a href="/tags/def/" style="font-size: 10px;">def<span class="count">1</span></a>'
302+
].join(' '));
303+
});
304+
305+
it('show_count with custom class', () => {
306+
const result = tagcloud({ show_count: true, count_class: 'tag-count' });
307+
308+
result.should.eql([
309+
'<a href="/tags/abc/" style="font-size: 13.33px;">abc<span class="tag-count">2</span></a>',
310+
'<a href="/tags/bcd/" style="font-size: 20px;">bcd<span class="tag-count">4</span></a>',
311+
'<a href="/tags/cde/" style="font-size: 16.67px;">cde<span class="tag-count">3</span></a>',
312+
'<a href="/tags/def/" style="font-size: 10px;">def<span class="tag-count">1</span></a>'
313+
].join(' '));
314+
});
293315
});

0 commit comments

Comments
 (0)