Module: Jekyll::GlossaryTooltip::CssInjector

Defined in:
lib/jekyll-glossary_tooltip/css_injector.rb

Overview

Automatically injects the plugin’s bundled tooltip CSS into rendered HTML pages and documents. Injection can be disabled via site configuration:

glossary_tooltip:
  include_default_style: false

Constant Summary collapse

CSS_FILE =
File.join(__dir__, "jekyll-glossary_tooltip.css")
CSS_CONTENT =
File.read(CSS_FILE).freeze
STYLE_ID =
"jekyll-glossary-tooltip-style"
STYLE_BLOCK =
"<style id=\"#{STYLE_ID}\">\n#{CSS_CONTENT}</style>".freeze
HEAD_TAG_REGEX =

Matches an opening <head> tag, including any attributes (e.g. <head lang=“en”>).

/<head\b[^>]*>/i.freeze

Class Method Summary collapse

Class Method Details

.inject_style(doc) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/jekyll-glossary_tooltip/css_injector.rb', line 27

def inject_style(doc)
  return unless enabled?(doc.site)
  return unless doc.output.to_s.match?(HEAD_TAG_REGEX)
  return if doc.output.include?(STYLE_ID)

  doc.output = doc.output.sub(HEAD_TAG_REGEX) { |match| "#{match}\n#{STYLE_BLOCK}" }
end

.register_hooksObject



21
22
23
24
25
# File 'lib/jekyll-glossary_tooltip/css_injector.rb', line 21

def register_hooks
  Jekyll::Hooks.register(%i[pages documents], :post_render) do |doc|
    inject_style(doc)
  end
end