Skip to content

Instantly share code, notes, and snippets.

@brennandunn
Created March 23, 2020 22:24
Show Gist options
  • Save brennandunn/e6707e05c700d3d082bfdabf63bac8f8 to your computer and use it in GitHub Desktop.
Save brennandunn/e6707e05c700d3d082bfdabf63bac8f8 to your computer and use it in GitHub Desktop.
require "jekyll"
require "nokogiri"
module Jekyll
class PersonalizeText
MAPPINGS = {
'email marketing tool' => 's-esp',
'email marketing app' => 's-esp',
'your email marketing database' => 's-esp',
'email list' => 's-list',
'Email List' => 's-list-tc',
'your email database' => 's-esp',
'ersonaliz' => 's-personalize',
'ustomiz' => 's-customize',
'ptimize' => 's-optimize',
'ehavior' => 's-behavior',
'ocalize' => 's-localize',
'umor' => 's-humor',
'center' => 's-center',
}
class << self
def personalize(doc)
html = Nokogiri::HTML(doc.output)
html.search('.//text()').each do |node|
content = node.text
MAPPINGS.each do |key, value|
if content.include?(key)
next if node.ancestors('body').empty?
next if !node.ancestors('.skip-personalization').empty?
content.gsub! key, %{<span class="#{value}">#{key}</span>}
end
end
node.replace(content)
end
doc.output = html.to_s
end
def processable?(doc)
(doc.is_a?(Jekyll::Page) || doc.write?) &&
doc.output_ext == ".html" || (doc.permalink&.end_with?("/"))
end
end
end
end
Jekyll::Hooks.register [:pages, :documents], :post_render do |doc|
Jekyll::PersonalizeText.personalize(doc) if Jekyll::PersonalizeText.processable?(doc)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment