class MotionMarkdownItPlugins::HeaderSections

Public Class Methods

init_plugin(md, *options) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 11
def self.init_plugin(md, *options)
  headerSections = HeaderSections.new(md, options)
  md.core.ruler.push('header_sections', lambda { |state| headerSections.addSections(state) })
end
new(md, options) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 16
def initialize(md, options)
  reset
end

Public Instance Methods

addSections(state) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 60
def addSections(state)
  state.tokens.each do |token|
    # record level of nesting
    if (!token.type.start_with?('heading'))
      @nestedLevel += token.nesting
    end
    if (@sections.last && @nestedLevel < @sections.last[:nesting])
      closeSectionsToCurrentNesting(@nestedLevel)
    end

    # add sections before headers
    if (token.type == 'heading_open')
      section = {
        :header => token.tag[1,1].to_i,
        :nesting => @nestedLevel
      }
      if (@sections.last && section[:header] <= @sections.last[:header])
        closeSections(section)
      end
      @tokens.push(openSection(token.attrs))
      if (token.attrIndex('id') != -1)
        # remove ID from token
        token.attrs.splice(token.attrIndex('id'), 1)
      end
      @sections.push(section)
    end

    @tokens.push(token)
  end  # end for every token

  closeAllSections()

  state.tokens = @tokens
  reset
end
closeAllSections() click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 47
def closeAllSections()
  while (@sections.pop())
    @tokens.push(closeSection())
  end
end
closeSection() click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 34
def closeSection()
  t = MarkdownIt::Token.new('section_close', 'section', -1)
  t.block = true
  return t
end
closeSections(section) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 40
def closeSections(section)
  while (@sections.last && section[:header] <= @sections.last[:header])
    @sections.pop()
    @tokens.push(closeSection())
  end
end
closeSectionsToCurrentNesting(nesting) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 53
def closeSectionsToCurrentNesting(nesting)
  while (@sections.last && nesting < @sections.last[:nesting])
    @sections.pop()
    @tokens.push(closeSection())
  end
end
openSection(attrs) click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 26
    def openSection(attrs)
      t = MarkdownIt::Token.new('section_open', 'section', 1)
      t.block = true
      # This adds support for markdown-it-attrs.  It's still in JS.
#     t.attrs = attrs && attrs.map(function (attr) { return [attr[0], attr[1]] })
      return t
    end
reset() click to toggle source
# File lib/motion-markdown-it-plugins/header_sections/header_sections.rb, line 20
def reset()
  @tokens = []  # output
  @sections = []
  @nestedLevel = 0
end