Class: MarkdownIt::RulesInline::FragmentsJoin

Inherits:
Object
  • Object
show all
Extended by:
Common::Utils
Defined in:
lib/motion-markdown-it/rules_inline/fragments_join.rb

Constant Summary

Constants included from Common::Utils

Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE

Class Method Summary collapse

Methods included from Common::Utils

arrayReplaceAt, assign, charCodeAt, escapeHtml, escapeRE, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd

Class Method Details

.fragments_join(state) ⇒ Object



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
46
# File 'lib/motion-markdown-it/rules_inline/fragments_join.rb', line 14

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

  last = 0      
  curr = 0
  while curr < max
    # re-calculate levels after emphasis/strikethrough turns some text nodes
    # into opening/closing tags
    level -= 1 if (tokens[curr].nesting < 0) # closing tag
    tokens[curr].level = level
    level +=1 if (tokens[curr].nesting > 0) # opening tag

    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.pop(tokens.length - last)
    end
end