Module: Postnhost::SeoHelper

Includes:
ApplicationHelper, SchemaHelper, SettingsHelper
Defined in:
app/helpers/postnhost/seo_helper.rb

Constant Summary collapse

SCHEMA_CONTEXT =
"https://schema.org".freeze
DEFAULT_LANGUAGE =
"en".freeze

Constants included from SchemaHelper

Postnhost::SchemaHelper::ARTICLE_TYPES, Postnhost::SchemaHelper::AUTHOR_ALUMNI_TYPES, Postnhost::SchemaHelper::CONTACT_TYPES, Postnhost::SchemaHelper::ORGANIZATION_TYPES, Postnhost::SchemaHelper::POLICY_FIELDS, Postnhost::SchemaHelper::SETTINGS_TEXT_OVERRIDE_FIELDS

Constants included from SettingsHelper

Postnhost::SettingsHelper::DEFAULT_OG_IMAGE_PATH, Postnhost::SettingsHelper::DEFAULT_SITE_LOGO_PATH, Postnhost::SettingsHelper::SITE_COPY_LABELS

Constants included from ApplicationHelper

ApplicationHelper::AUTHOR_SOCIAL_FIELDS

Instance Method Summary collapse

Methods included from SchemaHelper

#schema_array_to_multiline, #schema_article_type_options, #schema_author_alumni_type_options, #schema_contact_type_options, #schema_multiline_to_array, #schema_organization_type_options, #schema_policy_fields

Methods included from SettingsHelper

#cms_template_options, #postnhost_timezone_options, #render_site_scripts, #setting_og_image_absolute_url, #setting_og_image_src, #setting_site_logo_absolute_url, #setting_site_logo_src, #site_copy_label, #translation_entries_advanced, #translation_entries_primary

Methods included from ApplicationHelper

#admin_datetime, #admin_datetime_local_value, #article_author_label, #article_author_records, #article_authors, #article_authors_cache_key, #article_record_for_author_data, #author_avatar, #author_initials, #author_name, #author_pages_enabled?, #author_social_links, #current_path_without_locale, #flash_class, #language_author_path, #language_author_url, #language_category_path, #language_root_path, #localized_path, #normalize_external_url, #postnhost_openai_api_key_configured?, #postnhost_public_locale_file_present_for?

Instance Method Details

#article_cover_for_seo(article) ⇒ Object



67
68
69
70
71
# File 'app/helpers/postnhost/seo_helper.rb', line 67

def article_cover_for_seo(article)
  return unless article

  article if article.cover_image?
end

#article_schema(article) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/postnhost/seo_helper.rb', line 73

def article_schema(article)
  return unless article.respond_to?(:published_at) && article.published_at.present?

  schema = {
    "@context" => SCHEMA_CONTEXT,
    "@type" => article_schema_type(article),
    "headline" => schema_headline,
    "description" => page_description,
    "url" => page_url,
    "datePublished" => article.published_at.iso8601,
    "dateModified" => article.updated_at.iso8601,
    "author" => (article),
    "publisher" => {
      "@id" => schema_organization_id
    },
    "mainEntityOfPage" => {
      "@type" => "WebPage",
      "@id" => page_url
    },
    "articleSection" => article.categories.first&.name,
    "inLanguage" => schema_in_language
  }

  cover = article_cover_for_seo(article)
  if cover
    schema["image"] = {
      "@type" => "ImageObject",
      "url" => cover.cover_image.url,
      "width" => 1200,
      "height" => 630
    }
  end

  schema.compact
end

#author_schema(author) ⇒ Object



109
110
111
112
113
# File 'app/helpers/postnhost/seo_helper.rb', line 109

def author_schema(author)
  return unless author

  author_schema_person(author, include_works_for: true).merge("@context" => SCHEMA_CONTEXT)
end


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/postnhost/seo_helper.rb', line 143

def breadcrumbs_schema(article, category)
  items = [{ name: I18n.t("postnhost.public.breadcrumbs.home"), url: schema_site_root_url }]

  if article&.categories&.any?
    items << { name: article.categories.first.name, url: postnhost.public_category_url(article.categories.first.slug) }
    items << { name: page_title, url: page_url }
  elsif category
    items << { name: category.name, url: page_url }
  elsif article
    items << { name: page_title, url: page_url }
  end

  {
    "@context" => SCHEMA_CONTEXT,
    "@type" => "BreadcrumbList",
    "itemListElement" => items.map.with_index do |item, index|
      {
        "@type" => "ListItem",
        "position" => index + 1,
        "name" => item[:name],
        "item" => item[:url]
      }
    end
  }
end

#canonical_urlObject



56
57
58
# File 'app/helpers/postnhost/seo_helper.rb', line 56

def canonical_url
  page_url.split("?").first.chomp("/")
end

#meta_description_contentObject



41
42
43
# File 'app/helpers/postnhost/seo_helper.rb', line 41

def meta_description_content
  page_description
end

#og_titleObject



31
32
33
34
# File 'app/helpers/postnhost/seo_helper.rb', line 31

def og_title
  candidate = content_for?(:og_title) ? content_for(:og_title) : page_title
  normalize_meta_text(candidate).presence || page_title
end

#organization_schemaObject



127
128
129
# File 'app/helpers/postnhost/seo_helper.rb', line 127

def organization_schema
  schema_publisher
end

#page_descriptionObject



24
25
26
27
28
29
# File 'app/helpers/postnhost/seo_helper.rb', line 24

def page_description
  return nil if omit_description_for_paginated_public_index?

  candidate = content_for?(:meta_description) ? content_for(:meta_description) : site_description
  normalize_meta_text(candidate).presence || site_description
end

#page_titleObject



18
19
20
21
22
# File 'app/helpers/postnhost/seo_helper.rb', line 18

def page_title
  fallback_title = I18n.t("postnhost.public.site.blog_meta_title")
  candidate = content_for?(:title) ? content_for(:title) : fallback_title
  normalize_meta_text(candidate).presence || fallback_title
end

#page_urlObject



49
50
51
52
53
54
# File 'app/helpers/postnhost/seo_helper.rb', line 49

def page_url
  @page_url ||= begin
    raw_url = content_for?(:url) ? content_for(:url) : request.original_url
    setting_current.canonical_url_for(raw_url)
  end
end

#render_description_meta_tags?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/helpers/postnhost/seo_helper.rb', line 45

def render_description_meta_tags?
  meta_description_content.present?
end

#robots_contentObject



60
61
62
63
64
65
# File 'app/helpers/postnhost/seo_helper.rb', line 60

def robots_content
  return "noindex, nofollow" if setting_current.site_indexing == "noindex"

  candidate = content_for?(:robots) ? content_for(:robots) : "index, follow"
  normalize_meta_text(candidate).presence || "index, follow"
end

#schema_headlineObject



36
37
38
39
# File 'app/helpers/postnhost/seo_helper.rb', line 36

def schema_headline
  candidate = content_for?(:schema_headline) ? content_for(:schema_headline) : page_title
  normalize_meta_text(candidate).presence || page_title
end

#schema_site_nameObject



14
15
16
# File 'app/helpers/postnhost/seo_helper.rb', line 14

def schema_site_name
  schema_website_name
end

#site_descriptionObject



10
11
12
# File 'app/helpers/postnhost/seo_helper.rb', line 10

def site_description
  schema_website_description
end

#structured_data_graph(article: nil, author: nil, category: nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/postnhost/seo_helper.rb', line 131

def structured_data_graph(article: nil, author: nil, category: nil)
  graph = [website_schema, organization_schema]
  graph << article_schema(article) if article
  graph << author_schema(author) if author && !article
  graph << breadcrumbs_schema(article, category)

  {
    "@context" => SCHEMA_CONTEXT,
    "@graph" => graph.compact
  }
end

#structured_data_script(data, id: nil) ⇒ Object



169
170
171
172
173
# File 'app/helpers/postnhost/seo_helper.rb', line 169

def structured_data_script(data, id: nil)
   :script, type: "application/ld+json", "data-turbo-track": "reload", id: id do
    data.to_json.html_safe
  end
end

#website_schemaObject



115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/postnhost/seo_helper.rb', line 115

def website_schema
  {
    "@context" => SCHEMA_CONTEXT,
    "@type" => "WebSite",
    "name" => schema_website_name,
    "url" => schema_site_root_url,
    "description" => schema_website_description,
    "inLanguage" => schema_in_language,
    "publisher" => { "@id" => schema_organization_id }
  }.compact
end