55 lines
1.4 KiB
JavaScript
55 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const { htmlTag } = require('hexo-util');
|
|
const { parse } = require('url');
|
|
|
|
module.exports = function(path, text, options = {}, decode = false) {
|
|
const { config, theme } = this;
|
|
const data = parse(path);
|
|
const siteHost = parse(config.url).hostname || config.url;
|
|
|
|
let exturl = '';
|
|
let tag = 'a';
|
|
let attrs = { href: this.url_for(path) };
|
|
|
|
// If `exturl` enabled, set spanned links only on external links.
|
|
if (theme.exturl && data.protocol && data.hostname !== siteHost) {
|
|
tag = 'span';
|
|
exturl = 'exturl';
|
|
const encoded = Buffer.from(path).toString('base64');
|
|
attrs = {
|
|
class : exturl,
|
|
'data-url': encoded
|
|
};
|
|
}
|
|
|
|
for (const key in options) {
|
|
|
|
/**
|
|
* If option have `class` attribute, add it to
|
|
* 'exturl' class if `exturl` option enabled.
|
|
*/
|
|
if (exturl !== '' && key === 'class') {
|
|
attrs[key] += ' ' + options[key];
|
|
} else {
|
|
attrs[key] = options[key];
|
|
}
|
|
}
|
|
|
|
// If it's external link, rewrite attributes.
|
|
if (data.protocol && data.hostname !== siteHost) {
|
|
attrs.external = null;
|
|
|
|
if (!theme.exturl) {
|
|
// Only for simple link need to rewrite/add attributes.
|
|
attrs.rel = attrs.rel || 'noopener';
|
|
attrs.target = '_blank';
|
|
} else {
|
|
// Remove rel attributes for `exturl` in main menu.
|
|
attrs.rel = null;
|
|
}
|
|
}
|
|
|
|
return htmlTag(tag, attrs, decode ? decodeURI(text) : text, false);
|
|
};
|