Class: Mdtoc::Markdown::Header

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mdtoc/markdown.rb

Direct Known Subclasses

HeaderWithFragment

Instance Method Summary collapse

Constructor Details

#initialize(depth, label, url) ⇒ Header



12
13
14
15
16
17
18
19
# File 'lib/mdtoc/markdown.rb', line 12

def initialize(depth, label, url)
  if depth < 0
    raise ArgumentError, "Header depth must be >= 0, but was #{depth}"
  end
  @depth = depth
  @label = label.strip.gsub(/\s+/, ' ')
  @url = url
end

Instance Method Details

#to_sObject



27
28
29
30
# File 'lib/mdtoc/markdown.rb', line 27

def to_s
  prefix = ' ' * 2 * @depth
  "#{prefix}* [#{@label}](#{@url})"
end

#top_level?(relative_to_depth) ⇒ Boolean



22
23
24
# File 'lib/mdtoc/markdown.rb', line 22

def top_level?(relative_to_depth)
  @depth == relative_to_depth
end