-
Notifications
You must be signed in to change notification settings - Fork 2k
/
next-url.js
67 lines (54 loc) · 1.62 KB
/
next-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* global hexo */
'use strict';
const { htmlTag } = require('hexo-util');
const url = require('url');
hexo.extend.helper.register('next_url', function(path, text, options) {
const { config } = this;
var data = url.parse(path);
var siteHost = url.parse(config.url).hostname || config.url;
var theme = hexo.theme.config;
var exturl = '';
var tag = 'a';
var 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';
var encoded = Buffer.from(path).toString('base64');
attrs = {
class : exturl,
'data-url': encoded
};
}
options = options || {};
var keys = Object.keys(options);
var key = '';
for (var i = 0, len = keys.length; i < len; i++) {
key = keys[i];
/**
* 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 (attrs.class && Array.isArray(attrs.class)) {
attrs.class = attrs.class.join(' ');
}
// 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 = 'noopener';
attrs.target = '_blank';
} else {
// Remove rel attributes for `exturl` in main menu.
attrs.rel = null;
}
}
return htmlTag(tag, attrs, text, false);
});