Class: MotionMarkdownItPlugins::HeaderSections
- Inherits:
-
Object
- Object
- MotionMarkdownItPlugins::HeaderSections
- Defined in:
- lib/motion-markdown-it-plugins/header_sections/header_sections.rb
Class Method Summary collapse
Instance Method Summary collapse
- #addSections(state) ⇒ Object
- #closeAllSections ⇒ Object
- #closeSection ⇒ Object
- #closeSections(section) ⇒ Object
- #closeSectionsToCurrentNesting(nesting) ⇒ Object
-
#initialize(md, options) ⇒ HeaderSections
constructor
A new instance of HeaderSections.
- #openSection(attrs) ⇒ Object
- #reset ⇒ Object
Constructor Details
#initialize(md, options) ⇒ HeaderSections
Returns a new instance of HeaderSections.
16 17 18 |
# File 'lib/motion-markdown-it-plugins/header_sections/header_sections.rb', line 16 def initialize(md, ) reset end |
Class Method Details
.init_plugin(md, *options) ⇒ Object
11 12 13 14 |
# File 'lib/motion-markdown-it-plugins/header_sections/header_sections.rb', line 11 def self.init_plugin(md, *) headerSections = HeaderSections.new(md, ) md.core.ruler.push('header_sections', lambda { |state| headerSections.addSections(state) }) end |
Instance Method Details
#addSections(state) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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 ⇒ Object
47 48 49 50 51 |
# File 'lib/motion-markdown-it-plugins/header_sections/header_sections.rb', line 47 def closeAllSections() while (@sections.pop()) @tokens.push(closeSection()) end end |
#closeSection ⇒ Object
34 35 36 37 38 |
# 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) ⇒ Object
40 41 42 43 44 45 |
# 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) ⇒ Object
53 54 55 56 57 58 |
# 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) ⇒ Object
26 27 28 29 30 31 32 |
# 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 ⇒ Object
20 21 22 23 24 |
# File 'lib/motion-markdown-it-plugins/header_sections/header_sections.rb', line 20 def reset() @tokens = [] # output @sections = [] @nestedLevel = 0 end |