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
i = tokens.length - 1
while i >= 0
currentToken = tokens[i]
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
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)
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
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
tokens[i] = nodes
blockTokens[j].children = (tokens.flatten!(1))
end
i -= 1
end
end
end
|