Class: Seo::HtmlHeadingSanitizer
- Inherits:
-
BaseService
- Object
- BaseService
- Seo::HtmlHeadingSanitizer
- Defined in:
- app/services/seo/html_heading_sanitizer.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Instance Method Details
#process(html_fragment) ⇒ Object
6 7 8 9 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 |
# File 'app/services/seo/html_heading_sanitizer.rb', line 6 def process(html_fragment) logger.tagged('Seo::HtmlHeadingSanitizer') do errors = [] headings_modified = [] # Create a Loofah document doc = Loofah.fragment(html_fragment) # Process all heading tags (h1 through h6) (1..6).each do |level| doc.css("h#{level}").each do |heading| original_content = heading.to_html # Use Loofah's text method to get clean text content text_content = heading.text.strip next unless text_content != original_content headings_modified << { level: level, original: original_content, cleaned: text_content } # Replace the heading's content with clean text heading.inner_html = text_content logger.info "Cleaned h#{level} heading: #{text_content}" end end headings_modified.uniq!(&:inspect) headings_modified.sort_by!(&:inspect) Result.new( html_out: doc.to_html, headings_modified: headings_modified, errors: errors ) end end |