Module: MarkdownIt::Helpers::ParseLinkDestination

Includes:
Common::Utils
Included in:
HelperWrapper
Defined in:
lib/motion-markdown-it/helpers/parse_link_destination.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

Instance Method Summary collapse

Methods included from Common::Utils

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

Instance Method Details

#parseLinkDestination(str, pos, max) ⇒ 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
70
71
72
73
# File 'lib/motion-markdown-it/helpers/parse_link_destination.rb', line 9

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 || isSpace(code)) # \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
    end

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

    pos += 1
  end

  return result if start == pos
  return result if level != 0

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