Use first paragraph as meta description fallback
- For https://gitlab.com/gitlab-com/gitlab-docs/issues/194, https://gitlab.com/gitlab-com/gitlab-docs/issues/195
The problem
We use text as fallback description, causing most of the pages to having the same description, which is bad for SEO:
<% if @item[:description] %>
<meta name="description" content="<%= @item[:description] %>">
<% else %>
<meta name="description" content="Documentation for GitLab Community Edition, GitLab Enterprise Edition, Omnibus GitLab, and GitLab Runner.">
The solution
Create a logic to use the fallback description as the first 150 chars after the h1
.
Something like:
def meta_description
# get the page content from the main `.article-content` div:
# truncate to 150 chars
# ommision: `...`
# something like:
# truncate(page.meta_description, :ommision => "...", :length => 150)`
end
Then call it in the head.html
layout with something like:
<% if @item[:description] %>
<meta name="description" content="<%= @item[:description] %>">
<% else %>
<meta name="description" content="<%= @item[:meta_description] %>">
References
- Helper "truncate": https://apidock.com/rails/ActionView/Helpers/TextHelper/truncate
- Nanoc
:summary
: https://nanoc.ws/doc/reference/helpers/#capturing - Nanoc
Text
helper: https://www.rubydoc.info/gems/nanoc/4.2.4/Nanoc/Helpers/Text
@axil ideas?
cc/ @mikelewis
Edited by Marcia Ramos