|
@ -5,11 +5,8 @@ |
|
|
'use strict'; |
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var Autolinker = require('autolinker'); |
|
|
|
|
|
var arrayReplaceAt = require('../common/utils').arrayReplaceAt; |
|
|
var arrayReplaceAt = require('../common/utils').arrayReplaceAt; |
|
|
|
|
|
var normalizeLink = require('../common/utils').normalizeLink; |
|
|
|
|
|
|
|
|
var LINK_SCAN_RE = /www|@|\:\/\//; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isLinkOpen(str) { |
|
|
function isLinkOpen(str) { |
|
@ -19,54 +16,17 @@ function isLinkClose(str) { |
|
|
return /^<\/a\s*>/i.test(str); |
|
|
return /^<\/a\s*>/i.test(str); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Stupid fabric to avoid singletons, for thread safety.
|
|
|
|
|
|
// Required for engines like Nashorn.
|
|
|
|
|
|
//
|
|
|
|
|
|
function createLinkifier() { |
|
|
|
|
|
var links = []; |
|
|
|
|
|
var autolinker = new Autolinker({ |
|
|
|
|
|
stripPrefix: false, |
|
|
|
|
|
url: true, |
|
|
|
|
|
email: true, |
|
|
|
|
|
twitter: false, |
|
|
|
|
|
replaceFn: function (__, match) { |
|
|
|
|
|
// Only collect matched strings but don't change anything.
|
|
|
|
|
|
switch (match.getType()) { |
|
|
|
|
|
/*eslint default-case:0*/ |
|
|
|
|
|
case 'url': |
|
|
|
|
|
links.push({ |
|
|
|
|
|
text: match.matchedText, |
|
|
|
|
|
url: match.getUrl() |
|
|
|
|
|
}); |
|
|
|
|
|
break; |
|
|
|
|
|
case 'email': |
|
|
|
|
|
links.push({ |
|
|
|
|
|
text: match.matchedText, |
|
|
|
|
|
// normalize email protocol
|
|
|
|
|
|
url: 'mailto:' + match.getEmail().replace(/^mailto:/i, '') |
|
|
|
|
|
}); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
links: links, |
|
|
|
|
|
autolinker: autolinker |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function linkify(state) { |
|
|
module.exports = function linkify(state) { |
|
|
var i, j, l, tokens, token, text, nodes, ln, pos, level, htmlLinkLevel, |
|
|
var i, j, l, tokens, token, nodes, ln, text, pos, lastPos, level, htmlLinkLevel, |
|
|
blockTokens = state.tokens, |
|
|
blockTokens = state.tokens, |
|
|
linkifier = null, links, autolinker; |
|
|
links; |
|
|
|
|
|
|
|
|
if (!state.md.options.linkify) { return; } |
|
|
if (!state.md.options.linkify) { return; } |
|
|
|
|
|
|
|
|
for (j = 0, l = blockTokens.length; j < l; j++) { |
|
|
for (j = 0, l = blockTokens.length; j < l; j++) { |
|
|
if (blockTokens[j].type !== 'inline') { continue; } |
|
|
if (blockTokens[j].type !== 'inline') { continue; } |
|
|
|
|
|
|
|
|
tokens = blockTokens[j].children; |
|
|
tokens = blockTokens[j].children; |
|
|
|
|
|
|
|
|
htmlLinkLevel = 0; |
|
|
htmlLinkLevel = 0; |
|
@ -96,42 +56,33 @@ module.exports = function linkify(state) { |
|
|
} |
|
|
} |
|
|
if (htmlLinkLevel > 0) { continue; } |
|
|
if (htmlLinkLevel > 0) { continue; } |
|
|
|
|
|
|
|
|
if (token.type === 'text' && LINK_SCAN_RE.test(token.content)) { |
|
|
if (token.type === 'text' && state.md.linkify.test(token.content)) { |
|
|
|
|
|
|
|
|
// Init linkifier in lazy manner, only if required.
|
|
|
|
|
|
if (!linkifier) { |
|
|
|
|
|
linkifier = createLinkifier(); |
|
|
|
|
|
links = linkifier.links; |
|
|
|
|
|
autolinker = linkifier.autolinker; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
text = token.content; |
|
|
text = token.content; |
|
|
links.length = 0; |
|
|
links = state.md.linkify.match(text); |
|
|
autolinker.link(text); |
|
|
|
|
|
|
|
|
|
|
|
if (!links.length) { continue; } |
|
|
|
|
|
|
|
|
|
|
|
// Now split string to nodes
|
|
|
// Now split string to nodes
|
|
|
nodes = []; |
|
|
nodes = []; |
|
|
level = token.level; |
|
|
level = token.level; |
|
|
|
|
|
lastPos = 0; |
|
|
|
|
|
|
|
|
for (ln = 0; ln < links.length; ln++) { |
|
|
for (ln = 0; ln < links.length; ln++) { |
|
|
|
|
|
|
|
|
if (!state.md.inline.validateLink(links[ln].url)) { continue; } |
|
|
if (!state.md.inline.validateLink(links[ln].url)) { continue; } |
|
|
|
|
|
|
|
|
pos = text.indexOf(links[ln].text); |
|
|
pos = links[ln].index; |
|
|
|
|
|
|
|
|
if (pos) { |
|
|
if (pos > lastPos) { |
|
|
level = level; |
|
|
level = level; |
|
|
nodes.push({ |
|
|
nodes.push({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: text.slice(0, pos), |
|
|
content: text.slice(lastPos, pos), |
|
|
level: level |
|
|
level: level |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
nodes.push({ |
|
|
nodes.push({ |
|
|
type: 'link_open', |
|
|
type: 'link_open', |
|
|
href: links[ln].url, |
|
|
href: normalizeLink(links[ln].url), |
|
|
target: '', |
|
|
target: '', |
|
|
title: '', |
|
|
title: '', |
|
|
level: level++ |
|
|
level: level++ |
|
@ -145,12 +96,12 @@ module.exports = function linkify(state) { |
|
|
type: 'link_close', |
|
|
type: 'link_close', |
|
|
level: --level |
|
|
level: --level |
|
|
}); |
|
|
}); |
|
|
text = text.slice(pos + links[ln].text.length); |
|
|
lastPos = links[ln].lastIndex; |
|
|
} |
|
|
} |
|
|
if (text.length) { |
|
|
if (lastPos < text.length) { |
|
|
nodes.push({ |
|
|
nodes.push({ |
|
|
type: 'text', |
|
|
type: 'text', |
|
|
content: text, |
|
|
content: text.slice(lastPos), |
|
|
level: level |
|
|
level: level |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|