Optimize canonical URL

This commit is contained in:
Mimi 2020-08-07 10:38:32 +08:00
parent f76c0d8a97
commit 219adffe10
5 changed files with 18 additions and 27 deletions

View File

@ -1,6 +1,11 @@
{{ open_graph() }}
{{ canonical() }}
{# https://github.com/theme-next/hexo-theme-next/issues/866 #}
{%- set canonical = url | replace(r/index\.html$/, '') %}
{%- if not config.permalink.endsWith('.html') %}
{%- set canonical = canonical | replace(r/\.html$/, '') %}
{%- endif %}
<link rel="canonical" href="{{ canonical }}">
{# Exports some front-matter variables to Front-End #}
<script{{ pjax }} class="page-configurations">

View File

@ -1,13 +1,11 @@
{%- set paths = page.path.split('/') %}
{%- set count = paths.length %}
{%- if count > 2 %}
{%- set current = 0 %}
{%- set link = '' %}
<ul class="breadcrumb">
{%- for path in paths %}
{%- set current = current + 1 %}
{%- if path != 'index.html' %}
{%- if current == count - 1 and paths[count - 1] == 'index.html' %}
{%- if loop.index == count - 1 and paths[loop.index] == 'index.html' %}
<li>{{ path | upper }}</li>
{% else %}
{%- if link == '' %}
@ -15,8 +13,8 @@
{% else %}
{%- set link = link + '/' + path %}
{%- endif %}
{%- if path.includes('.html') %}
<li>{{ path | replace('.html', '') | upper }}</li>
{%- if path.endsWith('.html') %}
<li>{{ path | replace(r/\.html$/, '') | upper }}</li>
{% else %}
<li><a href="{{ url_for(link) }}/">{{ path | upper }}</a></li>
{%- endif %}

View File

@ -7,7 +7,7 @@
quicklink.listen({
timeout : {{ page.quicklink.timeout }},
priority: {{ page.quicklink.priority }},
ignores : [uri => uri.includes('#'),uri => uri === '{{ url | replace('index.html', '') }}',{{ page.quicklink.ignores }}]
ignores : [uri => uri.includes('#'),uri => uri === '{{ url | replace(r/index\.html$/, '') }}',{{ page.quicklink.ignores }}]
});
{%- if page.quicklink.delay %}
});

View File

@ -32,6 +32,6 @@ module.exports = hexo => {
let { plugins = 'jsdelivr' } = vendors;
if (plugins === 'cdnjs' && unavailable && unavailable.includes('cdnjs')) plugins = 'jsdelivr';
if (plugins === 'local' && typeof internal === 'undefined') plugins = 'jsdelivr';
vendors[key] = links[plugins];
vendors[key] = links[plugins] || links.jsdelivr;
}
};

View File

@ -15,16 +15,15 @@ hexo.extend.helper.register('next_inject', function(point) {
.join('');
});
hexo.extend.helper.register('next_js', function(url, pjax = false) {
hexo.extend.helper.register('next_js', function(file, pjax = false) {
const { next_version } = this;
const { js } = this.theme;
const { internal } = this.theme.vendors;
let src = this.url_for(`${js}/${url}`);
if (internal === 'jsdelivr') {
src = `//cdn.jsdelivr.net/npm/hexo-theme-next@${next_version}/source/js/${url}`;
} else if (internal === 'unpkg') {
src = `//unpkg.com/hexo-theme-next@${next_version}/source/js/${url}`;
}
const links = {
local : this.url_for(`${this.theme.js}/${file}`),
jsdelivr: `//cdn.jsdelivr.net/npm/hexo-theme-next@${next_version}/source/js/${file}`,
unpkg : `//unpkg.com/hexo-theme-next@${next_version}/source/js/${file}`
};
const src = links[internal] || links.local;
return `<script ${pjax ? 'data-pjax ' : ''}src="${src}"></script>`;
});
@ -59,20 +58,9 @@ hexo.extend.helper.register('post_nav', function(post) {
hexo.extend.helper.register('gitalk_md5', function(path) {
const str = this.url_for(path);
str.replace('index.html', '');
return crypto.createHash('md5').update(str).digest('hex');
});
hexo.extend.helper.register('canonical', function() {
// https://support.google.com/webmasters/answer/139066
const { permalink } = hexo.config;
let url = this.url.replace(/index\.html$/, '');
if (!permalink.endsWith('.html')) {
url = url.replace(/\.html$/, '');
}
return `<link rel="canonical" href="${url}">`;
});
/**
* Get page path given a certain language tag
*/