Skip to content

Commit

Permalink
Merge pull request #4370 from hexojs/tagcloud
Browse files Browse the repository at this point in the history
feat(tagcloud): new option class & level
  • Loading branch information
curbengh authored Jun 25, 2020
2 parents 0dad211 + e6e587e commit 7ff1a70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/plugins/helper/tagcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function tagcloudHelper(tags, options) {
const order = options.order || 1;
const unit = options.unit || 'px';
const color = options.color;
const className = options.class;
const level = options.level || 10;
const { transform } = options;
const separator = options.separator || ' ';
const result = [];
Expand Down Expand Up @@ -57,14 +59,15 @@ function tagcloudHelper(tags, options) {
const ratio = length ? sizes.indexOf(tag.length) / length : 0;
const size = min + ((max - min) * ratio);
let style = `font-size: ${parseFloat(size.toFixed(2))}${unit};`;
const attr = className ? ` class="${className}-${Math.round(ratio * level)}"` : '';

if (color) {
const midColor = startColor.mix(endColor, ratio);
style += ` color: ${midColor.toString()}`;
}

result.push(
`<a href="${this.url_for(tag.path)}" style="${style}">${transform ? transform(tag.name) : tag.name}</a>`
`<a href="${this.url_for(tag.path)}" style="${style}"${attr}>${transform ? transform(tag.name) : tag.name}</a>`
);
});

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

it('class name', () => {
const result = tagcloud({
class: 'tag-cloud'
});

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

0 comments on commit 7ff1a70

Please sign in to comment.