Skip to content
Draft
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
26 changes: 26 additions & 0 deletions layouts/partials/hooks/head-end.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{ with .Params.canonical_url -}}

<link rel="canonical" href="{{ . }}">

{{- else -}}

{{ $canonicalURL := .Permalink -}}

{{ $defaultLang := "en" -}}
{{ if and (ne .Language.Lang $defaultLang) .File -}}
<!-- If the page is not in the default language section, Extract the path of the current page relative to the content directory -->
{{ $pagePath := strings.TrimPrefix (add hugo.WorkingDir "/content/") .File.Filename -}}
{{ if hasPrefix $pagePath $defaultLang -}}

<!-- If it's a default-language fallback page, use the permalink of the origin page as canonical -->

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

{{- end -}}