Class: MarkdownIt::RulesInline::BalancePairs

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

Class Method Summary collapse

Class Method Details




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
38
39
40
41
42
43
44
45
# File 'lib/motion-markdown-it/rules_inline/balance_pairs.rb', line 8

def self.link_pairs(state)
  delimiters = state.delimiters
  max = state.delimiters.length

  0.upto(max - 1) do |i|
    lastDelim = delimiters[i]

    next if !lastDelim[:close]

    j = i - lastDelim[:jump] - 1

    while j >= 0
      currDelim = delimiters[j]

      if currDelim[:open] &&
         currDelim[:marker] == lastDelim[:marker] &&
         currDelim[:end] < 0 &&
         currDelim[:level] == lastDelim[:level]

        # typeofs are for backward compatibility with plugins
        # not needed:  typeof currDelim.length !== 'undefined' &&
        #              typeof lastDelim.length !== 'undefined' &&
        odd_match = (currDelim[:close] || lastDelim[:open]) &&
                    (currDelim[:length] + lastDelim[:length]) % 3 == 0

        if !odd_match
          lastDelim[:jump] = i - j
          lastDelim[:open] = false
          currDelim[:end]  = i
          currDelim[:jump] = 0
          break
        end
      end

      j -= currDelim[:jump] + 1
    end
  end
end