Sha256: 4d44fabbc12787373bb7fe2b6fa0e1a81b2c306eacf1c086790b8dcb345e765a

Contents?: true

Size: 1.96 KB

Versions: 20

Compression:

Stored size: 1.96 KB

Contents

module GovukTechDocs
  class MetaTags
    def initialize(config, current_page)
      @config = config
      @current_page = current_page
    end

    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

    # OpenGraph uses the non-standard property attribute instead of name, so we
    # return these separately so we can output them correctly.
    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

    def browser_title
      [page_title, site_name].select(&:present?).uniq.join(" - ")
    end

    def canonical_url
      "#{host}#{current_page.url}"
    end

  private

    attr_reader :config, :current_page

    def page_image
      "#{host}/images/govuk-large.png"
    end

    def site_name
      config[:tech_docs][:full_service_name] || config[:tech_docs][:service_name]
    end

    def page_description
      locals[:description] || frontmatter[:description]
    end

    def page_title
      locals[:title] || frontmatter[:title]
    end

    def robots
      "noindex" if config[:tech_docs][:prevent_indexing] || frontmatter[:prevent_indexing]
    end

    def google_site_verification
      config[:tech_docs][:google_site_verification]
    end

    def host
      config[:tech_docs][:host].to_s
    end

    def locals
      current_page.metadata[:locals]
    end

    def frontmatter
      current_page.data
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
govuk_tech_docs-3.5.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.4.5 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.4.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.3.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.3.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.2.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.2.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.1.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.0.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-3.0.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.4.3 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.4.2 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.4.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.4.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.3.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.2.2 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.2.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.2.0 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.1.1 lib/govuk_tech_docs/meta_tags.rb
govuk_tech_docs-2.1.0 lib/govuk_tech_docs/meta_tags.rb