class GovukTechDocs::MetaTags

Attributes

config[R]
current_page[R]

Public Class Methods

new(config, current_page) click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 3
def initialize(config, current_page)
  @config = config
  @current_page = current_page
end

Public Instance Methods

browser_title() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 38
def browser_title
  [page_title, site_name].select(&:present?).uniq.join(" - ")
end
canonical_url() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 42
def canonical_url
  "#{host}#{current_page.url}"
end
opengraph_tags() click to toggle source

OpenGraph uses the non-standard property attribute instead of name, so we return these separately so we can output them correctly.

# File lib/govuk_tech_docs/meta_tags.rb, line 25
def opengraph_tags
  all_opengraph_tags = {
    "og:description" => page_description,
    "og:image" => page_image,
    "og:site_name" => site_name,
    "og:title" => page_title,
    "og:type" => "object",
    "og:url" => canonical_url,
  }

  Hash[all_opengraph_tags.select { |_k, v| v }]
end
tags() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 8
def tags
  all_tags = {
    "description" => page_description,
    "google-site-verification" => google_site_verification,
    "robots" => robots,
    "twitter:card" => "summary",
    "twitter:domain" => URI.parse(host).host,
    "twitter:image" => page_image,
    "twitter:title" => browser_title,
    "twitter:url" => canonical_url,
  }

  Hash[all_tags.select { |_k, v| v }]
end

Private Instance Methods

frontmatter() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 82
def frontmatter
  current_page.data
end
google_site_verification() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 70
def google_site_verification
  config[:tech_docs][:google_site_verification]
end
host() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 74
def host
  config[:tech_docs][:host].to_s
end
locals() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 78
def locals
  current_page.metadata[:locals]
end
page_description() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 58
def page_description
  locals[:description] || frontmatter[:description]
end
page_image() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 50
def page_image
  "#{host}/images/govuk-large.png"
end
page_title() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 62
def page_title
  locals[:title] || frontmatter[:title]
end
robots() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 66
def robots
  "noindex" if config[:tech_docs][:prevent_indexing] || frontmatter[:prevent_indexing]
end
site_name() click to toggle source
# File lib/govuk_tech_docs/meta_tags.rb, line 54
def site_name
  config[:tech_docs][:full_service_name] || config[:tech_docs][:service_name]
end