badge.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const pug = require('pug')
  2. const title = 'slack';
  3. const color = '#39AE85';
  4. function width(str) {
  5. return 6 * str.length;
  6. }
  7. const svgTmpl =
  8. `svg(xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width=wd height="20")\n` +
  9. ` linearGradient(x2="0" y2="100%")#b\n` +
  10. ` stop(offset="0" stop-color="#bbb" stop-opacity=".1")\n` +
  11. ` stop(offset="1" stop-opacity=".1")\n` +
  12. ` clipPath#a\n` +
  13. ` rect(width=wd height="20" rx="3" fill="#fff")\n` +
  14. ` g(clip-path="url(#a)")\n` +
  15. ` path(fill="#555" d='M0 0h' + left + 'v20H0z')\n` +
  16. ` path(fill="${color}" d='M'+left + ' 0h' + (wd-left) + 'v20H' + left +'z')\n` +
  17. ` path(fill="url(#b)" d='M0 0h' + wd + 'v20H0z')\n` +
  18. ` g(fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110")\n` +
  19. ` text(x="195" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="${title.length*54}") ${title}\n` +
  20. ` text(x="195" y="140" transform="scale(.1)" textLength="${title.length*54}") ${title}\n` +
  21. ` text(x=(wd * 5 + 195) y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength=(value.length*60)) #{value}\n` +
  22. ` text(x=(wd * 5 + 195) y="140" transform="scale(.1)" textLength=(value.length*60)) #{value}\n`;
  23. module.exports = {
  24. badge: function(presence, total) {
  25. const fn = pug.compile(svgTmpl);
  26. const value = `${presence}/${total}`;
  27. const left = width(title) + 8;
  28. const wd = left + width(value) + 22;
  29. return fn({ value, wd, left });
  30. },
  31. };