class Mdtoc::Markdown::Parser

Public Class Methods

new(depth, url) click to toggle source
# File lib/mdtoc/markdown/parser.rb, line 13
def initialize(depth, url)
  @depth = depth
  @url = url
end

Public Instance Methods

headers(lines) click to toggle source
# File lib/mdtoc/markdown/parser.rb, line 19
def headers(lines)
  # TODO: Skip headers within multi-line comments.
  # TODO: Handle --- and === style headers.
  skip = T.let(false, T::Boolean)
  lines.filter_map do |line|
    # Skip code blocks.
    if line.start_with?('```') && !T.must(line[3..]).strip.end_with?('```')
      skip = !skip
    end
    next if skip || !line.start_with?('#')

    header(line)
  end
end

Private Instance Methods

header(line) click to toggle source
# File lib/mdtoc/markdown/parser.rb, line 37
def header(line)
  m = T.must(line.strip.match(/^(#+)\s*(.*)$/))
  num_hashes = m[1]&.count('#') || 1
  depth = @depth + num_hashes - 1
  label = m[2] || ''
  HeaderWithFragment.new(depth, label, @url)
end