Class: Willamette::Builders::Inspectors

Inherits:
Bridgetown::Builder
  • Object
show all
Defined in:
lib/willamette/builders/inspectors.rb

Instance Method Summary collapse

Instance Method Details

#buildObject

rubocop:disable Metrics



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/willamette/builders/inspectors.rb', line 3

def build # rubocop:disable Metrics
  # TODO: make these all optional/configurable

  # Open external links in new windows
  inspect_html do |document|
    document.query_selector_all("a").each do |anchor|
      next if anchor[:target]

      unless anchor[:href]&.starts_with?("http") && !anchor[:href]&.include?(site.config.url)
        next
      end

      anchor[:target] = "_blank"
    end
  end

  # Add visible hash anchors for headings
  inspect_html do |document|
    document.query_selector_all("article h2[id], article h3[id]").each do |heading|
      heading << document.create_text_node(" ")
      heading << document.create_element(
        "a", "#",
        href: "##{heading[:id]}",
        class: "heading-anchor"
      )
    end
  end

  # Lazy-load images by default
  inspect_html do |document|
    main = document.query_selector("main")

    main.query_selector_all("img").each do |img|
      # TODO: evaluate if this fix should belong here or in Bridgetown Core
      #if img[:src].start_with?("./")
      #  img[:src] = resource.relative_url + img[:src].delete_prefix(".")
      #end
      next if img[:loading]

      img[:loading] = :lazy
    end
  end
end