Module: Jekyll::HoverPopup::Hooks

Defined in:
lib/jekyll/hover_popup/hooks.rb

Class Method Summary collapse

Class Method Details

.inject_assets(html, assets_path:, hover_delay_ms:, nav_hover_preview: true) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll/hover_popup/hooks.rb', line 24

def self.inject_assets(html, assets_path:, hover_delay_ms:, nav_hover_preview: true)
  return html if html.include?('data-hover-popup-root="true"')

  tags = <<~HTML
    <script>
      window.__JHP_CONFIG__ = #{{
        hoverDelayMs: hover_delay_ms,
        navHoverPreview: nav_hover_preview
      }.to_json};
    </script>
    <link rel="stylesheet" href="#{assets_path}/hover_popup.css" />
    <script defer src="#{assets_path}/hover_popup.js" data-hover-popup-root="true"></script>
  HTML

  if html.include?("</body>")
    html.sub("</body>", "#{tags}\n</body>")
  else
    "#{html}\n#{tags}\n"
  end
end

.register!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jekyll/hover_popup/hooks.rb', line 4

def self.register!
  Jekyll::Hooks.register(%i[pages documents], :post_render) do |doc|
    site = doc.site
    cfg = (site.config["hover_popup"] || {})
    next if cfg["enabled"] == false
    next unless doc.respond_to?(:output_ext) && doc.output_ext == ".html"

    assets_path = cfg["assets_path"] || "/assets/jekyll-hover-popup"
    assets_path = "/#{assets_path}" unless assets_path.start_with?("/")
    hover_delay_ms = cfg["hover_delay_ms"] || 0
    nav_hover_preview = cfg.fetch("nav_hover_preview", true)

    begin
      doc.output = inject_assets(doc.output.to_s, assets_path: assets_path, hover_delay_ms: hover_delay_ms, nav_hover_preview: nav_hover_preview)
    rescue StandardError => e
      Jekyll.logger.warn("jekyll-hover-popup:", "Failed to process #{doc.relative_path}: #{e.class}: #{e.message}")
    end
  end
end