Skip to content

Commit

Permalink
[zh] Ensure zh fallback pages specify en page as canonical source
Browse files Browse the repository at this point in the history
  • Loading branch information
chalin committed May 15, 2024
1 parent fdd8422 commit 475009f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions layouts/partials/hooks/head-end.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@

{{ $canonicalURL := .Permalink -}}

{{ $defaultLang := "en" -}}
{{ if and (ne .Language.Lang $defaultLang) .File -}}
{{/* This page is in a non-default-language section */ -}}
{{ $pagePath := strings.TrimPrefix (add hugo.WorkingDir "/content/") .File.Filename -}}
{{ if hasPrefix $pagePath $defaultLang -}}

{{/* This page is actually a default-language fallback page. Use the link
to the origin of the fallback page as canonical reference. */ -}}

{{ $translationPages := where .Translations "Lang" $defaultLang -}}
{{ $translation := index $translationPages 0 -}}
{{ with $translation -}}
{{ $canonicalURL = .Permalink -}}
{{ end -}}
{{ end -}}
{{ end -}}
<link rel="canonical" href="{{ $canonicalURL }}">

{{- end -}}

0 comments on commit 475009f

Please sign in to comment.