Class: Rain::TOCNode

Inherits:
Antlers::LeafNode
  • Object
show all
Defined in:
lib/pages/raindown/nodes/toc_node.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, template:) ⇒ TOCNode

Returns a new instance of TOCNode.



8
9
10
11
12
# File 'lib/pages/raindown/nodes/toc_node.rb', line 8

def initialize(name:, template:)
  super(name:)

  @template = template
end

Class Method Details

.build(template:) ⇒ Object



36
37
38
# File 'lib/pages/raindown/nodes/toc_node.rb', line 36

def build(template:, **)
  new(name: :toc, template:)
end

.match?(segment:) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pages/raindown/nodes/toc_node.rb', line 32

def match?(segment:)
  segment[:toc]
end

Instance Method Details

#render(current_binding: nil, parent_binding: nil, slot_node: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pages/raindown/nodes/toc_node.rb', line 14

def render(current_binding: nil, parent_binding: nil, slot_node: nil)
  doc = Nokogiri::HTML(@template)

  output = <<~HTML
    <div class="floating">
      <details id="toc" open>
        <summary>Table of contents</summary>
        <ul>
          #{doc.css('h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]').map { |h|
            "<li class='#{h.name}'><a href='\##{h['id']}'>#{h.text.strip}</a></li>"
          }.join("\n")}
        </ul>
      </div>
    </details>
  HTML
end