Module: Jekyll::ImageLinks::Hooks

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

Class Method Summary collapse

Class Method Details

.build_jil_config(site, cfg) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/jekyll/image_links/hooks.rb', line 26

def self.build_jil_config(site, cfg)
  return { useHoverPopup: false } if cfg["use_hover_popup"] == false
  return { useHoverPopup: true } if cfg["use_hover_popup"] == true

  hover_cfg = site.config["hover_popup"] || {}
  return { useHoverPopup: false } if hover_cfg["enabled"] == false

  # Auto: omit useHoverPopup and detect window.JekyllHoverPopup at runtime.
  {}
end

.inject_assets(html, assets_path:, site:, cfg:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/jekyll/image_links/hooks.rb', line 37

def self.inject_assets(html, assets_path:, site:, cfg:)
  return html unless html.include?('data-jil-image-map="true"')
  return html if html.include?('data-jil-root="true"')

  jil_config = build_jil_config(site, cfg)

  tags = <<~HTML
    <script>
      window.__JIL_CONFIG__ = #{jil_config.to_json};
    </script>
    <link rel="stylesheet" href="#{assets_path}/image_links.css" />
    <script defer src="#{assets_path}/image_links.js" data-jil-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
23
24
# File 'lib/jekyll/image_links/hooks.rb', line 4

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

    html = doc.output.to_s
    next unless html.include?('data-jil-image-map="true"') || MapRenderer.portable_markup?(html)

    assets_path = cfg["assets_path"] || "/assets/jekyll-image-links"
    assets_path = "/#{assets_path}" unless assets_path.start_with?("/")

    begin
      html = MapRenderer.enhance_html(html, site: site, page: doc, cfg: cfg)
      doc.output = inject_assets(html, assets_path: assets_path, site: site, cfg: cfg)
    rescue StandardError => e
      Jekyll.logger.warn("jekyll-image-links:", "Failed to process #{doc.relative_path}: #{e.class}: #{e.message}")
    end
  end
end