From 219adffe10b40b4f652de1535df4037d0ab2f869 Mon Sep 17 00:00:00 2001
From: Mimi <1119186082@qq.com>
Date: Fri, 7 Aug 2020 10:38:32 +0800
Subject: [PATCH] Optimize canonical URL
---
layout/_partials/head/head-unique.njk | 7 ++++++-
layout/_partials/page/breadcrumb.njk | 8 +++-----
layout/_third-party/quicklink.njk | 2 +-
scripts/events/lib/vendors.js | 2 +-
scripts/helpers/engine.js | 26 +++++++-------------------
5 files changed, 18 insertions(+), 27 deletions(-)
diff --git a/layout/_partials/head/head-unique.njk b/layout/_partials/head/head-unique.njk
index 7aa830868..54249091b 100644
--- a/layout/_partials/head/head-unique.njk
+++ b/layout/_partials/head/head-unique.njk
@@ -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 %}
+
{# Exports some front-matter variables to Front-End #}
`;
});
@@ -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 ``;
-});
-
/**
* Get page path given a certain language tag
*/