class Qiita::Markdown::Filters::HeadingAnchor

Public Instance Methods

call() click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 7
def call
  doc.search("h1, h2, h3, h4, h5, h6").each do |heading|
    heading["id"] = suffixed_id(heading)
  end

  doc
end

Private Instance Methods

counter() click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 17
def counter
  @counter ||= ::Hash.new(0)
end
get_count(id) click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 21
def get_count(id)
  counter[id]
end
heading_id(node) click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 29
def heading_id(node)
  node.text.downcase.gsub(/[^\p{Word}\- ]/u, "").tr(" ", "-")
end
increment_count(id) click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 25
def increment_count(id)
  counter[id] += 1
end
suffixed_id(node) click to toggle source
# File lib/qiita/markdown/filters/heading_anchor.rb, line 33
def suffixed_id(node)
  id = heading_id(node)
  count = get_count(id)
  suffix = count.positive? ? "-#{count}" : ""
  increment_count(id)

  "#{id}#{suffix}"
end