-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Support for Canonical page link in <head>
Signed-off-by: Archit Sharma <74408634+iArchitSharma@users.noreply.github.com>
- Loading branch information
1 parent
cd5e4d0
commit 16543fd
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -}} |