Class: MarkdownIt::RulesCore::Linkify

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

Class Method Summary collapse

Class Method Details

.isLinkClose(str) ⇒ Object



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

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

.isLinkOpen(str) ⇒ Object




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

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

.linkify(state) ⇒ Object




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
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
133
134
# File 'lib/motion-markdown-it/rules_core/linkify.rb', line 18

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
          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
        next if (htmlLinkLevel > 0)

        if (currentToken.type == 'text' && state.md.linkify =~ 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.
            #

# TODO work on this when clearer
puts "Linkify requires work"
            # if (!links[ln].schema)
            #   urlText = state.md.normalizeLinkText.call('http://' + urlText).replace(/^http:\/\//, '')
            # elsif (links[ln].schema == 'mailto:' && !Regexp.new('^mailto:/i') =~ urlText)
            #   urlText = state.md.normalizeLinkText.call('mailto:' + urlText).replace(/^mailto:/, '');
            # } else {
            #   urlText = state.md.normalizeLinkText.call(urlText);
            # }

            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
          blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes)
        end
        i -= 1
      end
    end
end