Class: MarkdownIt::RulesInline::Backticks

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

Class Method Summary collapse

Class Method Details

.backtick(state, silent) ⇒ 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
38
39
40
41
42
43
44
45
46
47
# File 'lib/motion-markdown-it/rules_inline/backticks.rb', line 8

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

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

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

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

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

  matchStart = matchEnd = pos

  while ((matchStart = state.src.index('`', matchEnd)) != nil)
    matchEnd = matchStart + 1

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

    if (matchEnd - matchStart == marker.length)
      if (!silent)
        token         = state.push('code_inline', 'code', 0)
        token.markup  = marker
        token.content = state.src.slice(pos...matchStart).gsub(/[ \n]+/, ' ').strip
      end
      state.pos = matchEnd
      return true
    end
  end

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