Skip to content

Integrate with HTML::Pipeline

Juanito Fatas edited this page Feb 13, 2018 · 6 revisions

Integrate with HTML::Pipeline

Create a filter HTML::Pipeline::Twitter::EmojiFilter like so:

module HTML
  class Pipeline
    module Twitter
      class EmojiFilter < HTML::Pipeline::Filter
        def call
          Twemoji.parse(doc,
            file_ext:   context[:file_ext]   || "svg",
            class_name: context[:class_name] || "emoji",
            img_attrs:  context[:img_attrs],
          )
        end
      end
    end
  end
end

Include the new filter in your filter chain:

HTML::Pipeline.new [
  HTML::Pipeline::MarkdownFilter,
  HTML::Pipeline::SanitizationFilter,
  ...
  HTML::Pipeline::Twitter::EmojiFilter
], { gfm: true, **options }

Then if your HTML String or Nokogiri DocumentFragment contains emoji:

I like chocolates :heart_eyes:

:heart_eyes: will be transformed into an image tag after passing through the HTML::Pipeline::Twitter::EmojiFilter:

<img class="emoji" draggable="false" alt=":heart_eyes:" src="https://twemoji.maxcdn.com/svg/1f60d.svg">