mail_to.js 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const htmlTag = require('hexo-util').htmlTag;
  3. const qs = require('querystring');
  4. function mailToHelper(path, text, options = {}) {
  5. if (Array.isArray(path)) path = path.join(',');
  6. if (!text) text = path;
  7. const attrs = {
  8. href: `mailto:${path}`,
  9. title: text
  10. };
  11. const keys = Object.keys(options);
  12. let key = '';
  13. for (let i = 0, len = keys.length; i < len; i++) {
  14. key = keys[i];
  15. attrs[key] = options[key];
  16. }
  17. if (attrs.class && Array.isArray(attrs.class)) {
  18. attrs.class = attrs.class.join(' ');
  19. }
  20. const data = {};
  21. ['subject', 'cc', 'bcc', 'body'].forEach(i => {
  22. const item = attrs[i];
  23. if (item) {
  24. data[i] = Array.isArray(item) ? item.join(',') : item;
  25. attrs[i] = null;
  26. }
  27. });
  28. const querystring = qs.stringify(data);
  29. if (querystring) attrs.href += `?${querystring}`;
  30. return htmlTag('a', attrs, text);
  31. }
  32. module.exports = mailToHelper;