Class: Www::FaqItemComponent

Inherits:
ApplicationComponent show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::UrlHelper
Defined in:
app/components/www/faq_item_component.rb

Overview

ViewComponent: renders the faq item block.

Instance Method Summary collapse

Methods inherited from ApplicationComponent

#cms_link, #fetch_or_fallback, #image_asset_tag, #image_tag, #number_to_currency, #number_with_delimiter, #post_path, #post_url, #strip_tags

Constructor Details

#initialize(article_faq:) ⇒ FaqItemComponent

Returns a new instance of FaqItemComponent.

Parameters:

  • article_faq (Object)

    the FAQ record to render (an Article::Faq or a
    hash-like object exposing question/answer)



10
11
12
# File 'app/components/www/faq_item_component.rb', line 10

def initialize(article_faq:)
  @article_faq = article_faq
end

Instance Method Details

#plain_text(html) ⇒ String

Strips HTML tags from a FAQ solution for use in plain-text contexts such as
the clipboard copy button. Uses regex rather than an HTML5 parser — the full
Nokogiri parse is unnecessary overhead for producing readable clipboard text
from the simple HTML that FAQ solutions contain.

Parameters:

  • html (String, nil)

    the HTML markup to strip

Returns:

  • (String)

    plain text with tags removed and entities unescaped



20
21
22
23
24
25
26
# File 'app/components/www/faq_item_component.rb', line 20

def plain_text(html)
  html.to_s
      .gsub(/<[^>]*>/, ' ')
      .gsub('&amp;', '&').gsub('&lt;', '<').gsub('&gt;', '>').gsub('&nbsp;', ' ')
      .gsub(/\s+/, ' ')
      .strip
end