Module: MarkdownIt::Helpers::ParseLinkDestination

Included in:
RulesBlock::Reference, RulesInline::Image, RulesInline::Link
Defined in:
lib/motion-markdown-it/helpers/parse_link_destination.rb

Instance Method Summary collapse

Instance Method Details

#parseLinkDestination(str, pos, max) ⇒ 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/motion-markdown-it/helpers/parse_link_destination.rb', line 8

def parseLinkDestination(str, pos, max)
  lines = 0
  start = pos
  result = {ok: false, pos: 0, lines: 0, str: ''}

  if (str.charCodeAt(pos) == 0x3C ) # < 
    pos += 1
    while (pos < max)
      code = str.charCodeAt(pos)
      return result if (code == 0x0A ) # \n
      if (code == 0x3E) #  >
        result[:pos] = pos + 1
        result[:str] = unescapeAll(str.slice((start + 1)...pos))
        result[:ok]  = true
        return result
      end
      if (code == 0x5C && pos + 1 < max)  # \
        pos += 2
        next
      end

      pos += 1
    end

    # no closing '>'
    return result
  end

  # this should be ... } else { ... branch

  level = 0
  while (pos < max) 
    code = str.charCodeAt(pos)

    break if (code == 0x20)

    # ascii control characters
    break if (code < 0x20 || code == 0x7F)

    if (code == 0x5C && pos + 1 < max) # \
      pos += 2
      next
    end

    if (code == 0x28) # (
      level += 1
      break if (level > 1)
    end

    if (code == 0x29) # )
      level -= 1
      break if (level < 0)
    end

    pos += 1
  end

  return result if (start == pos)

  result[:str]   = unescapeAll(str.slice(start...pos))
  result[:lines] = lines
  result[:pos]   = pos
  result[:ok]    = true
  return result
end