Class: MarkdownIt::RulesInline::TextCollapse

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-markdown-it/rules_inline/text_collapse.rb

Class Method Summary collapse

Class Method Details

.text_collapse(state) ⇒ Object




8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/motion-markdown-it/rules_inline/text_collapse.rb', line 8

def self.text_collapse(state)
  level   = 0
  tokens  = state.tokens
  max     = state.tokens.length

  last = curr = 0
  while curr < max
    # re-calculate levels
    level += tokens[curr].nesting
    tokens[curr].level = level

    if tokens[curr].type == 'text' &&
        curr + 1 < max &&
        tokens[curr + 1].type == 'text'

      # collapse two adjacent text nodes
      tokens[curr + 1].content = tokens[curr].content + tokens[curr + 1].content
    else
      tokens[last] = tokens[curr] if curr != last

      last += 1
    end

    curr += 1
  end

  if curr != last
    tokens.slice!(last..max)
  end
end