Inject HTML snippets

to run in your site's root directory

View template source
create_file "app/processors/inject_processor.rb", <<~RUBY
class InjectProcessor < Perron::HtmlProcessor::Base
  def process
    return if content_too_short?
    return if injection_point.blank?

    injection_point.add_previous_sibling(Nokogiri::HTML.fragment(snippet))
  end

  private

  MINIMUM_CHARACTER_COUNT = 2_000

  def content_too_short?
    @html.text.size < MINIMUM_CHARACTER_COUNT
  end

  def injection_point
    return if h2_elements.empty?

    h2_elements.find { text_position_of(it) >= content_midpoint } || h2_elements.last
  end

  def snippet
    # Add whatever you want to inject,
    # e.g. a email form, an advertisement script, etc.
    "💉"
  end

  def h2_elements = @html.css("h2")

  def text_position_of(element) = @html.text.index(element.text).to_i

  def content_midpoint = @html.text.size / 2
end
RUBY

This snippet adds a processor to inject custom HTML snippets into long-form content at a natural breaking point in the reading flow.

Use cases

  • Newsletter signups: Capture emails from engaged readers
  • Advertisements: Place ads in long articles without disrupting short posts
  • Related content: Suggest related articles or products (use related resources

Configuration

Customize the processor by overriding these methods:

  • MINIMUM_CHARACTER_COUNT
  • snippet

The processor only runs on content that exceeds the minimum length, keeping short posts clean.