Просмотр исходного кода

Utilize canonical URLs if specified in front matter (#827)

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>

### Prerequisites

Put an `x` into the box(es) that apply:

- [ ] This pull request fixes a bug.
- [x] This pull request adds a feature.
- [ ] This pull request introduces breaking change.

### Description

To prevent hurting the SEO when a blog post re-posted, it's recommended
to use canonical URL tags.
This PR updates the concerning metatag if `canonicalUrl` specified in
front matter.

See working example:
- https://kakkoyun.me/posts/fantastic-symbols-and-where-to-find-them
-
https://github.com/kakkoyun/me/blob/master/content/posts/fantastic-symbols-and-where-to-find-them.md

### Checklist

Put an `x` into the box(es) that apply:

#### General

- [x] Describe what changes are being made
- [x] Explain why and how the changes were necessary and implemented
respectively
- [ ] Reference issue with `#<ISSUE_NO>` if applicable

#### Resources

- [ ] If you have changed any SCSS code, run `make release` to
regenerate all CSS files

#### Contributors

- [x] Add yourself to `CONTRIBUTORS.md` if you aren't on it already

---------

Signed-off-by: Kemal Akkoyun <kakkoyun@gmail.com>
Kemal Akkoyun 2 лет назад
Родитель
Сommit
44fdec2a22
3 измененных файлов с 17 добавлено и 13 удалено
  1. 2 1
      CONTRIBUTORS.md
  2. 12 11
      docs/configurations.md
  3. 3 1
      layouts/partials/head.html

+ 2 - 1
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)
+- [Todor Bogosavljević](https://github.com/tbx1b)
+- [Kemal Akkoyun](https://github.com/kakkoyun)

+ 12 - 11
docs/configurations.md

@@ -318,16 +318,17 @@ This theme includes one content type:
 
 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.

+ 3 - 1
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 }}