Skip to content

Commit

Permalink
Optimize canonical URL
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Aug 7, 2020
1 parent f76c0d8 commit 219adff
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
7 changes: 6 additions & 1 deletion layout/_partials/head/head-unique.njk
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
8 changes: 3 additions & 5 deletions layout/_partials/page/breadcrumb.njk
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
{%- 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 == '' %}
{%- set link = '/' + path %}
{% 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 %}
Expand Down
2 changes: 1 addition & 1 deletion layout/_third-party/quicklink.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
});
Expand Down
2 changes: 1 addition & 1 deletion scripts/events/lib/vendors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
26 changes: 7 additions & 19 deletions scripts/helpers/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>`;
});

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 219adff

Please sign in to comment.