Skip to content

Commit

Permalink
fix: incorrect canonical and alternative URL annotations (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
KentarouTakeda committed Apr 9, 2024
1 parent 3a93850 commit d2192c7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
12 changes: 10 additions & 2 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ hexo.extend.helper.register('header_menu', function(className) {
});

hexo.extend.helper.register('canonical_url', function(lang) {
let path = this.page.path;
if (lang && lang !== 'en') path = lang + '/' + path;
const slugs = this.page.path.split('/').filter(v => v !== '');

if (Object.keys(this.site.data.languages).includes(slugs[0])) {
slugs.shift();
}
if (lang !== 'en') {
slugs.unshift(lang);
}

const path = slugs.join('/');

return full_url_for(path);
});
Expand Down
1 change: 1 addition & 0 deletions source/about/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: About
layout: about
---

[![github stars](https://img.shields.io/github/stars/hexojs/hexo?style=for-the-badge&color=0e83cd&logo=github)](https://github.com/hexojs/hexo) [![github forks](https://img.shields.io/github/forks/hexojs/hexo?style=for-the-badge&color=0e83cd&logo=github)](https://github.com/hexojs/hexo) [![npm stats](https://img.shields.io/npm/dm/hexo?style=for-the-badge&color=0e83cd&label=npm%20downloads&logo=npm)](https://www.npmjs.com/package/hexo)
Expand Down
1 change: 1 addition & 0 deletions themes/navy/layout/about.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ partial('page') }}
9 changes: 6 additions & 3 deletions themes/navy/layout/partial/head.njk
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Canonical links -->
<link rel="canonical" href="{{ url }}">
<!-- Alternative links -->
<link rel="canonical" href="{{ page.permalink | default(canonical_url('en'), true) }}">
<!-- Alternate links -->
{% if page.layout == 'page' or page.layout == 'index' %}
{% for lang, value in site.data.languages %}
<link rel="alternative" hreflang="{{ lang }}" href="{{ canonical_url(lang) }}">
{% if page.permalink !== canonical_url(lang) %}
<link rel="alternate" hreflang="{{ lang }}" href="{{ canonical_url(lang) }}">
{% endif %}
{% endfor %}
<link rel="alternate" hreflang="x-default" href="{{ canonical_url('en') }}" />
{% endif %}
<!-- Icon -->
<link rel="apple-touch-icon" sizes="57x57" href="{{ url_for('icon/apple-touch-icon-57x57.png') }}">
Expand Down

0 comments on commit d2192c7

Please sign in to comment.