fix(perf): process external links in frontend

This commit is contained in:
ppoffice 2019-12-20 18:05:36 -05:00
parent 3c3fefda71
commit f5185d52d0
3 changed files with 69 additions and 6 deletions

View File

@ -2,8 +2,23 @@
<%- _js(cdn('moment', '2.22.2', 'min/moment-with-locales.min.js')) %>
<script>moment.locale("<%= get_config('language', 'en') %>");</script>
<%
let externalLink = get_config('external_link');
if (typeof externalLink === 'boolean') {
externalLink = { enable: externalLink, exclude: [] };
} else {
externalLink = {
enable: typeof externalLink.enable === 'boolean' ? externalLink.enable : true,
exclude: externalLink.exclude || []
};
}
%>
<script>
var IcarusThemeSettings = {
site: {
url: '<%= config.url %>',
external_link: <%- JSON.stringify(externalLink) %>
},
article: {
highlight: {
clipboard: <%= get_config('article.highlight.clipboard', true) %>,
@ -14,13 +29,13 @@ var IcarusThemeSettings = {
</script>
<% if (get_config('article.highlight.clipboard')) { %>
<%- _js(cdn('clipboard', '2.0.4', 'dist/clipboard.min.js'), true) %>
<%- _js(cdn('clipboard', '2.0.4', 'dist/clipboard.min.js'), true) %>
<% } %>
<% if (has_config('plugins')) { %>
<% for (let plugin in get_config('plugins')) { %>
<%- _partial('plugin/' + plugin, { head: false, plugin: get_config('plugins')[plugin] }) %>
<% } %>
<% for (let plugin in get_config('plugins')) { %>
<%- _partial('plugin/' + plugin, { head: false, plugin: get_config('plugins')[plugin] }) %>
<% } %>
<% } %>
<%- _js('js/main', true) %>

View File

@ -1,5 +1,8 @@
{
"name": "hexo-theme-icarus",
"version": "2.6.0",
"private": true
}
"private": true,
"engines": {
"node": ">=8.3.0"
}
}

View File

@ -103,4 +103,49 @@
$mask.on('click', toggleToc);
$('.navbar-main .catalogue').on('click', toggleToc);
}
// hexo-util/lib/is_external_link.js
function isExternalLink(input, sitehost, exclude) {
try {
sitehost = new URL(sitehost).hostname;
} catch (e) { }
if (!sitehost) return false;
// handle relative url
const data = new URL(input, 'http://' + sitehost);
// handle mailto: javascript: vbscript: and so on
if (data.origin === 'null') return false;
const host = data.hostname;
if (exclude) {
exclude = Array.isArray(exclude) ? exclude : [exclude];
if (exclude && exclude.length) {
for (const i of exclude) {
if (host === i) return false;
}
}
}
if (host !== sitehost) return true;
return false;
}
if (typeof (IcarusThemeSettings) !== 'undefined' &&
typeof (IcarusThemeSettings.site.url) !== 'undefined' &&
typeof (IcarusThemeSettings.site.external_link) !== 'undefined' &&
IcarusThemeSettings.site.external_link.enable) {
$('.article .content a').filter(function (i, link) {
return link.href && link.classList.length === 0 && isExternalLink(link.href,
IcarusThemeSettings.site.url,
IcarusThemeSettings.site.external_link.exclude);
}).each(function (i, link) {
link.relList.add('noopener');
link.target = '_blank';
});
}
})(jQuery);