Skip to content

Conversation

@fantasticmao
Copy link
Contributor

Prerequisites

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

  • This pull request fixes a bug.
  • This pull request adds a feature.
  • This pull request introduces breaking change.

Description

Support two SEO friendly tags: <meta name="description"> and <meta name="keywords">.

Because of the following code snippet, the meta tag <meta name="description"> and <meta name="keywords"> on the website will use the Hugo site variables on every page, and that is actually not SEO friendly.

{{ with .Site.Params.description }}<meta name="description" content="{{ . }}">{{ end }}
{{ with .Site.Params.keywords }}<meta name="keywords" content="{{ . }}">{{ end }}

I have rewritten this code snippet, and hope to use such a strategy:

{{ with .Description }}
  <meta name="description" content="{{ . }}">
{{ else }}
  {{ if .IsPage }}
    {{ with .Params.description }}
      <meta name="description" content="{{ . }}">
    {{ else }}
      <meta name="description" content="{{ .Summary }}">
    {{ end }}
  {{ else }}
    <meta name="description" content="{{ .Site.Params.description }}">
  {{ end }}
{{ end }}

{{ with .Keywords }}
  <meta name="keywords" content="{{ delimit . "," }}">
{{ else }}
  {{ if .IsPage }}
    {{ with .Params.keywords }}
      <meta name="keywords" content="{{ delimit . "," }}">
    {{ else }}
      <meta name="keywords" content="{{ .Site.Params.keywords }}">
    {{ end }}
  {{ else }}
    <meta name="keywords" content="{{ .Site.Params.keywords }}">
  {{ end }}
{{ end }}

Issues Resolved

#299

Checklist

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

General

  • Describe what changes are being made
  • 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

  • Add yourself to CONTRIBUTORS.md if you aren't on it already

@Tectu
Copy link

Tectu commented Apr 24, 2020

I'm using this patch and it works wonderfully!

{{ with .Site.Params.description }}<meta name="description" content="{{ . }}">{{ end }}
{{ with .Site.Params.keywords }}<meta name="keywords" content="{{ . }}">{{ end }}
<meta name="description" content="{{ with .Description }}{{ . }}{{ else }}{{ if .IsPage }}{{ with .Params.description }}{{ . }}{{ else }}{{ .Summary }}{{ end }}{{ else }}{{ .Site.Params.description }}{{ end }}{{ end }}">
<meta name="keywords" content="{{ with .Keywords }}{{ delimit . "," }}{{ else }}{{ if .IsPage }}{{ with .Params.keywords }}{{ delimit . "," }}{{ else }}{{ .Site.Params.keywords }}{{ end }}{{ else }}{{ .Site.Params.keywords }}{{ end }}{{ end }}">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this part using chained defaults. Something like this:

<meta name="description" content="{{ .Description | default (.Summary | default .Site.Params.description ) }}">
<meta name="keywords" content="{{ (delimit .Keywords ",") | default .Site.Params.keywords }}">

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right 👍, and I have simplified it now

@luizdepra
Copy link
Owner

Thanks. :)

@luizdepra luizdepra merged commit 7724fa7 into luizdepra:master Apr 26, 2020
@fantasticmao fantasticmao deleted the support-seo-friendly-meta-tags branch April 27, 2020 03:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants