Class: MarkdownIt::RulesCore::Linkify

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

Constant Summary collapse

MAILTO_RE =
/^mailto\:/

Class Method Summary collapse

Class Method Details

.isLinkClose(str) ⇒ Object



14
15
16
# File 'lib/motion-markdown-it/rules_core/linkify.rb', line 14

def self.isLinkClose(str)
  return !(/^<\/a\s*>/i =~ str).nil?
end

.isLinkOpen(str) ⇒ Object




11
12
13
# File 'lib/motion-markdown-it/rules_core/linkify.rb', line 11

def self.isLinkOpen(str)
  return !(/^<a[>\s]/i =~ str).nil?
end

.linkify(state) ⇒ Object




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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/motion-markdown-it/rules_core/linkify.rb', line 19

def self.linkify(state)
  blockTokens = state.tokens

  return if (!state.md.options[:linkify])

  (0...blockTokens.length).each do |j|
    if (blockTokens[j].type != 'inline' || !state.md.linkify.pretest(blockTokens[j].content))
      next
    end

    tokens = blockTokens[j].children

    htmlLinkLevel = 0

    # We scan from the end, to keep position when new tags added.
    # Use reversed logic in links start/end match
    i = tokens.length - 1
    while i >= 0
      currentToken = tokens[i]

      # Skip content of markdown links
      if (currentToken.type == 'link_close')
        i -= 1
        while (tokens[i].level != currentToken.level && tokens[i].type != 'link_open')
          i -= 1
        end
        i -= 1 and next
      end

      # Skip content of html tag links
      if (currentToken.type == 'html_inline')
        if (isLinkOpen(currentToken.content) && htmlLinkLevel > 0)
          htmlLinkLevel -= 1
        end
        if (isLinkClose(currentToken.content))
          htmlLinkLevel += 1
        end
      end
      i -= 1 and next if (htmlLinkLevel > 0)

      if (currentToken.type == 'text' && state.md.linkify.test(currentToken.content))

        text = currentToken.content
        links = state.md.linkify.match(text)

        # Now split string to nodes
        nodes   = []
        level   = currentToken.level
        lastPos = 0
        
        (0...links.length).each do |ln|
          url = links[ln].url
          fullUrl = state.md.normalizeLink.call(url)
          next if (!state.md.validateLink.call(fullUrl))

          urlText = links[ln].text

          # Linkifier might send raw hostnames like "example.com", where url
          # starts with domain name. So we prepend http:// in those cases,
          # and remove it afterwards.
          if links[ln].schema.empty?
            urlText = state.md.normalizeLinkText.call("http://#{urlText}").sub(/^http:\/\//, '')
          elsif (links[ln].schema == 'mailto:' && !(MAILTO_RE =~ urlText))
            urlText = state.md.normalizeLinkText.call("mailto:#{urlText}").sub(MAILTO_RE, '')
          else
            urlText = state.md.normalizeLinkText.call(urlText)
          end

          pos = links[ln].index

          if (pos > lastPos)
            token         = Token.new('text', '', 0)
            token.content = text.slice(lastPos...pos)
            token.level   = level
            nodes.push(token)
          end

          token         = Token.new('link_open', 'a', 1)
          token.attrs   = [ [ 'href', fullUrl ] ]
          token.level   = level
          level        += 1
          token.markup  = 'linkify'
          token.info    = 'auto'
          nodes.push(token)

          token         = Token.new('text', '', 0)
          token.content = urlText
          token.level   = level
          nodes.push(token)

          token         = Token.new('link_close', 'a', -1)
          level        -= 1
          token.level   = level
          token.markup  = 'linkify'
          token.info    = 'auto'
          nodes.push(token)

          lastPos = links[ln].lastIndex
        end
        if (lastPos < text.length)
          token         = Token.new('text', '', 0)
          token.content = text.slice_to_end(lastPos)
          token.level   = level
          nodes.push(token)
        end

        # replace current node
        tokens[i] = nodes
        blockTokens[j].children = (tokens.flatten!(1))
      end
      i -= 1
    end
  end
end