link_to.js 625 B

12345678910111213141516171819202122232425262728
  1. 'use strict';
  2. const { htmlTag, url_for } = require('hexo-util');
  3. function linkToHelper(path, text, options = {}) {
  4. if (typeof options === 'boolean') options = {external: options};
  5. if (!text) text = path.replace(/^https?:\/\/|\/$/g, '');
  6. const attrs = Object.assign({
  7. href: url_for.call(this, path),
  8. title: text
  9. }, options);
  10. if (attrs.external) {
  11. attrs.target = '_blank';
  12. attrs.rel = 'noopener';
  13. attrs.external = null;
  14. }
  15. if (attrs.class && Array.isArray(attrs.class)) {
  16. attrs.class = attrs.class.join(' ');
  17. }
  18. return htmlTag('a', attrs, text);
  19. }
  20. module.exports = linkToHelper;