Skip to content

Commit

Permalink
Added Support for Canonical page link in <head>
Browse files Browse the repository at this point in the history
Signed-off-by: Archit Sharma <74408634+iArchitSharma@users.noreply.github.com>
  • Loading branch information
iArchitSharma committed Jun 3, 2024
1 parent cd5e4d0 commit 16543fd
Showing 1 changed file with 26 additions and 0 deletions.
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 -}}

0 comments on commit 16543fd

Please sign in to comment.