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

Added Support for Canonical page link in <head> #2032

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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 -}}