Class: MarkdownIt::RulesInline::Backticks

Inherits:
Object
  • Object
show all
Extended by:
Common::Utils
Defined in:
lib/motion-markdown-it/rules_inline/backticks.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

.backtick(state, silent) ⇒ Object




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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/motion-markdown-it/rules_inline/backticks.rb', line 9

def self.backtick(state, silent)
  pos = state.pos
  ch = charCodeAt(state.src, pos)

  return false if (ch != 0x60)  #  `

  start = pos
  pos  += 1
  max  = state.posMax

  # scan marker length
  while (pos < max && charCodeAt(state.src, pos) == 0x60)  # `
    pos += 1
  end

  marker = state.src.slice(start...pos)
  openerLength = marker.length

  if (state.backticksScanned && (state.backticks[openerLength] || 0) <= start)
    state.pending += marker if (!silent)
    state.pos += openerLength
    return true
  end

  matchStart = matchEnd = pos

  # Nothing found in the cache, scan until the end of the line (or until marker is found)
  while ((matchStart = state.src.index('`', matchEnd)) != nil)
    matchEnd = matchStart + 1

    # scan marker length
    while (matchEnd < max && charCodeAt(state.src, matchEnd) == 0x60) # `
      matchEnd += 1
    end

    closerLength = matchEnd - matchStart

    if (closerLength == openerLength)
      # Found matching closer length.
      if (!silent)
        token         = state.push('code_inline', 'code', 0)
        token.markup  = marker
        token.content = state.src.slice(pos...matchStart)
          .gsub(/\n/, ' ')
          .gsub(/^ (.+) $/, '\1')
      end
      state.pos = matchEnd
      return true
    end

    # Some different length found, put it in cache as upper limit of where closer can be found
    state.backticks[closerLength] = matchStart
  end

  # Scanned through the end, didn't find anything
  state.backticksScanned = true

  state.pending += marker if (!silent)
  state.pos     += openerLength
  return true
end