Module: Middleman::Metaman::Helpers

Defined in:
lib/middleman-metaman/helpers.rb

Instance Method Summary collapse

Instance Method Details

#display_meta_tagsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/middleman-metaman/helpers.rb', line 10

def display_meta_tags
  @meta_tags ||= ActiveSupport::HashWithIndifferentAccess.new
  @meta_tags[:og] ||= ActiveSupport::HashWithIndifferentAccess.new
  @meta_tags[:twitter] ||= ActiveSupport::HashWithIndifferentAccess.new
  @meta_tags[:canonical] = true

  if app.data[:meta_tags]
    site_meta_tags = app.data[:meta_tags].with_indifferent_access
    @meta_tags.merge!(site_meta_tags)
  end

  # get meta from translation
  page_array = current_page.page_id.gsub('-', '_').split('/')
  lang = page_array.first&.to_s&.gsub('_', '-')&.to_sym
  page_array.shift if I18n.available_locales.include?(lang)
  t_key = "#{page_array.join('.')}.meta"
  @meta_tags.merge!(I18n.t(t_key)) if I18n.exists?(t_key)

  # get meta from frontmatter
  @meta_tags.merge!(
    {
      title: current_page.data.title,
      description: current_page.data.description
    }.compact
  )

  @meta_tags.merge!(@set_meta_tags) if @set_meta_tags

  html = []
  @meta_tags[:title] = full_title(@meta_tags[:title])
  html.push (:title, @meta_tags[:title])
  if @meta_tags[:canonical]
    html.push(
      tag(:link, rel: 'canonical', href: "#{host}#{current_page.url}")
    )
  end


  excluded_keys = %w(site_name separator title image canonical)
  meta_hash = @meta_tags.reject { |k| excluded_keys.include?(k) }.merge(
    og: meta_open_graph,
    twitter: meta_twitter
  )

  meta_hash.each do |key, value|
    next unless value

    html.push generate_meta_html(key, value)
  end
  html.flatten.join
end

#set_meta_tags(tags) ⇒ Object



5
6
7
8
# File 'lib/middleman-metaman/helpers.rb', line 5

def set_meta_tags(tags)
  @set_meta_tags ||= ActiveSupport::HashWithIndifferentAccess.new
  @set_meta_tags.merge!(tags)
end