link_to.js 774 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const htmlTag = require('hexo-util').htmlTag;
  3. function linkToHelper(path, text, options) {
  4. if (typeof options === 'boolean') options = {external: options};
  5. options = options || {};
  6. if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');
  7. const attrs = {
  8. href: this.url_for(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.external) {
  18. attrs.target = '_blank';
  19. attrs.rel = 'noopener';
  20. attrs.external = null;
  21. }
  22. if (attrs.class && Array.isArray(attrs.class)) {
  23. attrs.class = attrs.class.join(' ');
  24. }
  25. return htmlTag('a', attrs, text);
  26. }
  27. module.exports = linkToHelper;