From 44fdec2a2291128162f1bfcc6d4ed47ba97f633f Mon Sep 17 00:00:00 2001
From: Kemal Akkoyun <kakkoyun@users.noreply.github.com>
Date: Thu, 13 Jul 2023 12:14:56 +0000
Subject: [PATCH] Utilize canonical URLs if specified in front matter (#827)

---
 docs/configurations.md     |   23 ++++++++++++-----------
 layouts/partials/head.html |    4 +++-
 CONTRIBUTORS.md            |    3 ++-
 3 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index de79113..c245b06 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -131,4 +131,5 @@
 - [Yiğit Altınay](https://altinay.xyz)
 - [Fei Kong](https://github.com/alpha0422)
 - [Ahmet Enes Bayraktar](https://github.com/aeb-dev)
-- [Todor Bogosavljević](https://github.com/tbx1b)
\ No newline at end of file
+- [Todor Bogosavljević](https://github.com/tbx1b)
+- [Kemal Akkoyun](https://github.com/kakkoyun)
diff --git a/docs/configurations.md b/docs/configurations.md
index e86fc7b..58f0a5a 100644
--- a/docs/configurations.md
+++ b/docs/configurations.md
@@ -318,16 +318,17 @@
 
 These are the front matter variables used by `hugo-coder` theme.
 
-| Name             | Type   | Required | Description                                        | Default | Example                                                                       |
-| ---------------- | ------ | -------- | -------------------------------------------------- | ------- | ----------------------------------------------------------------------------- |
-| tags             | list   | No       | Add tag(s) to this post.                           |         | `["Hugo", "Go"]`                                                              |
-| categories       | list   | No       | Add categorie(s) to this post.                     |         | `["Hugo", "Go"]`                                                              |
-| series           | list   | No       | Add series to this post (used by OpenGraph).       |         | `["Theme Demo"]`                                                              |
-| author           | list   | No       | Add author to this post.                           |         | `["John Doe"]`                                                                |
-| externalLink     | string | No       | Link to an external post.                          |         | `"https://github.com/luizdepra/hugo-coder/wiki"`                              |
-| featuredImage    | string | No       | Link/path to add an image below post metadata.     |         | `"https://github.com/luizdepra/hugo-coder/blob/master/images/screenshot.png"` |
-| math             | bool   | No       | If true, MathJax is enabled only for this post.    | `false` | `true` or `false`                                                             |
-| katex            | bool   | No       | If true, katex is enabled only for this post.      | `false` | `true` or `false`                                                             |
-| disableComments  | bool   | No       | If true, comments are disabled.                    | `false` | `true` or `false`                                                             |
+| Name             | Type   | Required | Description                                        | Default | Example                                                                         |
+| ---------------- | ------ | -------- | -------------------------------------------------- | ------- | ------------------------------------------------------------------------------- |
+| tags             | list   | No       | Add tag(s) to this post.                           |         | `["Hugo", "Go"]`                                                                |
+| categories       | list   | No       | Add categorie(s) to this post.                     |         | `["Hugo", "Go"]`                                                                |
+| series           | list   | No       | Add series to this post (used by OpenGraph).       |         | `["Theme Demo"]`                                                                |
+| author           | list   | No       | Add author to this post.                           |         | `["John Doe"]`                                                                  |
+| externalLink     | string | No       | Link to an external post.                          |         | `"https://github.com/luizdepra/hugo-coder/wiki"`                                |
+| featuredImage    | string | No       | Link/path to add an image below post metadata.     |         | `"https://github.com/luizdepra/hugo-coder/blob/master/images/screenshot.png"`   |
+| math             | bool   | No       | If true, MathJax is enabled only for this post.    | `false` | `true` or `false`                                                               |
+| katex            | bool   | No       | If true, katex is enabled only for this post.      | `false` | `true` or `false`                                                               |
+| disableComments  | bool   | No       | If true, comments are disabled.                    | `false` | `true` or `false`                                                               |
+| canonicalUrl     | string | No       | Link to override <link rel="canonical"/> in <head> | `false` | `"https://my-company.com/blog/my-blog-post-that-I-repost-without-hurtiong-seo"` |
 
 > "tags", "categories", "series" and "authors" are taxonomies defined in the `config.toml` file.
diff --git a/layouts/partials/head.html b/layouts/partials/head.html
index 6637bd6..e9cad8b 100644
--- a/layouts/partials/head.html
+++ b/layouts/partials/head.html
@@ -1,6 +1,8 @@
 {{ partial "head/meta-tags.html" . }}
 
-{{ if .Permalink }}
+{{ if .Params.canonicalUrl }}
+<link rel="canonical" href="{{ .Params.canonicalUrl }}">
+{{ else }}
 <link rel="canonical" href="{{ .Permalink }}">
 {{ end }}
 

--
Gitblit v1.10.0