Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: incorrect canonical and alternative URL annotations #2159

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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