Class: Markly::Renderer::Headings
- Inherits:
-
Object
- Object
- Markly::Renderer::Headings
- Defined in:
- lib/markly/renderer/headings.rb
Overview
Extracts headings from a markdown document with unique anchor IDs. Handles duplicate heading text by appending counters (e.g., "deployment", "deployment-2", "deployment-3").
Class Method Summary collapse
-
.extract(root, min_level: 1, max_level: 6) ⇒ Object
Extract all headings using a new heading-anchor registry.
Instance Method Summary collapse
-
#anchor_for(node) ⇒ Object
Generate a unique anchor for a node.
-
#extract(root, min_level: 1, max_level: 6) ⇒ Object
Extract all headings from a document root with unique anchors.
-
#initialize ⇒ Headings
constructor
Initializes an empty heading-anchor registry.
Constructor Details
#initialize ⇒ Headings
Initializes an empty heading-anchor registry.
12 13 14 |
# File 'lib/markly/renderer/headings.rb', line 12 def initialize @ids = {} end |
Class Method Details
Instance Method Details
#anchor_for(node) ⇒ Object
Generate a unique anchor for a node.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/markly/renderer/headings.rb', line 20 def anchor_for(node) base = base_anchor_for(node) if @ids.key?(base) @ids[base] += 1 "#{base}-#{@ids[base]}" else @ids[base] = 1 base end end |
#extract(root, min_level: 1, max_level: 6) ⇒ Object
Extract all headings from a document root with unique anchors.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/markly/renderer/headings.rb', line 38 def extract(root, min_level: 1, max_level: 6) headings = [] root.walk do |node| if node.type == :header level = node.header_level next if level < min_level || level > max_level headings << Heading.new( node: node, level: level, text: node.to_plaintext.chomp, anchor: anchor_for(node) ) end end headings end |