Module: MetaHelper
- Defined in:
- app/helpers/meta_helper.rb
Overview
View helper: meta.
Instance Method Summary collapse
-
#bootstrap_bare_tables(html) ⇒ Object
Rich-text tables we generate arrive as bare — Kramdown output from Sunny's Markdown conversion — and Bootstrap 5 styles nothing without the opt-in class, so they render as unstyled text ("this does not appear to be a table", BC 10131899786).
- #build_title(*args) ⇒ Object
-
#downgrade_h1_tags(html) ⇒ Object
Converts any tags in rich-text HTML to to prevent multiple H1s when the template already defines one.
-
#effective_page_tag ⇒ Object
Returns the effective page tag: override if set, otherwise @main_tag from controller.
-
#og_image_asset_url(image_id, options = {}) ⇒ String?
Build the social (Open Graph) image URL for a digital asset, with social-card-friendly defaults that any key in
optionscan override. - #page_author(author) ⇒ Object
- #page_description(description) ⇒ Object
- #page_keywords(keywords) ⇒ Object
- #page_og_image(image_id_or_image) ⇒ Object
- #page_robot_directive(directive) ⇒ Object
-
#page_tag(page_path) ⇒ Object
Overrides the automatic page tag (set by PagesController) with a custom value.
- #page_title(*args) ⇒ Object
- #set_metas(title: DEFAULT_PAGE_TITLE, description: DEFAULT_PAGE_TITLE, keywords: DEFAULT_PAGE_KEYWORDS) ⇒ Object
- #swiftype_robot_exclude ⇒ Object
- #swiftype_type(type) ⇒ Object
Instance Method Details
#bootstrap_bare_tables(html) ⇒ Object
Rich-text tables we generate arrive as bare — Kramdown output from
Sunny's Markdown conversion — and Bootstrap 5 styles nothing without the
opt-in class, so they render as unstyled text ("this does not appear to be
a table", BC 10131899786). Applied at WRITE time (ShowcaseToolBuilder
.normalize_description), not render time, so the stored HTML is exactly
what the page serves; existing rows were backfilled once with the same
transform. A table already carrying a class keeps it.
The class pair matches the 204 legacy showcase tables already stored as
table table-bordered. Parsed, not regexed: a > inside a quoted
attribute value defeats any tag-scoped pattern (the class lookahead stops
early and a duplicate class attribute is emitted, clobbering the real
one), while table:not([class]) is exact and leaves custom elements like
redactor's <table-block> alone for free. The original string comes back
untouched when there is nothing to do, so running this over stored content
never rewrites markup gratuitously.
102 103 104 105 106 107 108 109 110 111 |
# File 'app/helpers/meta_helper.rb', line 102 def (html) return html if html.blank? fragment = Nokogiri::HTML5.fragment(html) = fragment.css('table:not([class])') return html if .empty? .each { |table| table['class'] = 'table table-bordered' } fragment.to_html end |
#build_title(*args) ⇒ Object
81 82 83 84 |
# File 'app/helpers/meta_helper.rb', line 81 def build_title(*args) links = args.compact.uniq page_title links.join(' > ') end |
#downgrade_h1_tags(html) ⇒ Object
Converts any tags in rich-text HTML to to prevent
multiple H1s when the template already defines one.
115 116 117 118 119 |
# File 'app/helpers/meta_helper.rb', line 115 def (html) return html if html.blank? html.gsub(%r{<(/?)h1(\s|>)}i) { "<#{::Regexp.last_match(1)}h2#{::Regexp.last_match(2)}" } end |
#effective_page_tag ⇒ Object
Returns the effective page tag: override if set, otherwise @main_tag from controller
63 64 65 |
# File 'app/helpers/meta_helper.rb', line 63 def effective_page_tag @page_tag_override || @main_tag end |
#og_image_asset_url(image_id, options = {}) ⇒ String?
Build the social (Open Graph) image URL for a digital asset, with
social-card-friendly defaults that any key in options can override.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/meta_helper.rb', line 132 def og_image_asset_url(image_id, = {}) = { format: :jpeg, background: 'ffffff', width: SOCIAL_IMAGE_WIDTH, height: SOCIAL_IMAGE_HEIGHT, crop_mode: 'cm-extract', thumbnail: true }.merge() image_asset_url(image_id, **) end |
#page_author(author) ⇒ Object
14 15 16 |
# File 'app/helpers/meta_helper.rb', line 14 def () content_for(:author) { } end |
#page_description(description) ⇒ Object
8 9 10 11 12 |
# File 'app/helpers/meta_helper.rb', line 8 def page_description(description) return if description.blank? content_for(:description) { description.truncate(375, separator: /[\s|]/, omission: '') } end |
#page_keywords(keywords) ⇒ Object
4 5 6 |
# File 'app/helpers/meta_helper.rb', line 4 def page_keywords(keywords) content_for(:keywords) { keywords } end |
#page_og_image(image_id_or_image) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'app/helpers/meta_helper.rb', line 39 def page_og_image(image_id_or_image) return if @og_image_set return if image_id_or_image.blank? image_asset_url = og_image_asset_url(image_id_or_image) || DEFAULT_SOCIAL_ICON content_for(:og_image) { image_asset_url } @og_image_set = true nil end |
#page_robot_directive(directive) ⇒ Object
67 68 69 70 71 |
# File 'app/helpers/meta_helper.rb', line 67 def page_robot_directive(directive) return if directive.blank? content_for(:robots_directive) { directive } end |
#page_tag(page_path) ⇒ Object
Overrides the automatic page tag (set by PagesController) with a custom value.
Only needed when the page tag should differ from the URL-based default.
Usage: page_tag('custom/path') outputs: for-custom-path-page
53 54 55 56 57 58 59 60 |
# File 'app/helpers/meta_helper.rb', line 53 def page_tag(page_path) return if @page_tag_override_set return if page_path.blank? @page_tag_override = DigitalAsset.page_tag_for(page_path) @page_tag_override_set = true nil end |
#page_title(*args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'app/helpers/meta_helper.rb', line 28 def page_title(*args) page_title = [args].flatten.map(&:to_s).filter_map(&:presence).uniq.join(' | ') return if page_title.blank? # Check the length of our page_title, if not too long and no WarmlyYours present in the name, add the company locale add_title_info = " | #{company_locale}" page_title << add_title_info if (page_title.size + add_title_info.size) <= 60 && !page_title.match?(/WarmlyYours/i) @page_title = page_title content_for(:title) { page_title } end |
#set_metas(title: DEFAULT_PAGE_TITLE, description: DEFAULT_PAGE_TITLE, keywords: DEFAULT_PAGE_KEYWORDS) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'app/helpers/meta_helper.rb', line 18 def ( title: DEFAULT_PAGE_TITLE, description: DEFAULT_PAGE_TITLE, keywords: DEFAULT_PAGE_KEYWORDS ) page_title title page_description description page_keywords keywords end |
#swiftype_robot_exclude ⇒ Object
77 78 79 |
# File 'app/helpers/meta_helper.rb', line 77 def swiftype_robot_exclude content_for(:swiftype_robot) { 'noindex, nofollow' } end |
#swiftype_type(type) ⇒ Object
73 74 75 |
# File 'app/helpers/meta_helper.rb', line 73 def swiftype_type(type) content_for(:swiftype_type) { type } end |