|
|
@ -1,6 +1,6 @@ |
|
|
|
// Replace link-like texts with link nodes.
|
|
|
|
//
|
|
|
|
// Currently restricted to http/https/ftp
|
|
|
|
// Currently restricted by `inline.validateLink()` to http/https/ftp
|
|
|
|
//
|
|
|
|
'use strict'; |
|
|
|
|
|
|
@ -10,6 +10,18 @@ var Autolinker = require('autolinker'); |
|
|
|
|
|
|
|
var LINK_SCAN_RE = /www|\:\/\//; |
|
|
|
|
|
|
|
|
|
|
|
function isLinkOpen(str) { |
|
|
|
return /^<a[>\s]/i.test(str); |
|
|
|
} |
|
|
|
function isLinkClose(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, |
|
|
@ -22,17 +34,17 @@ var autolinker = new Autolinker({ |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
function isLinkOpen(str) { |
|
|
|
return /^<a[>\s]/i.test(str); |
|
|
|
} |
|
|
|
function isLinkClose(str) { |
|
|
|
return /^<\/a\s*>/i.test(str); |
|
|
|
return { |
|
|
|
links: links, |
|
|
|
autolinker: autolinker |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = function linkify(state) { |
|
|
|
var i, j, l, tokens, token, text, nodes, ln, pos, level, htmlLinkLevel, |
|
|
|
blockTokens = state.tokens; |
|
|
|
blockTokens = state.tokens, |
|
|
|
linkifier = null, links, autolinker; |
|
|
|
|
|
|
|
if (!state.options.linkify) { return; } |
|
|
|
|
|
|
@ -69,6 +81,13 @@ module.exports = function linkify(state) { |
|
|
|
|
|
|
|
if (token.type === 'text' && LINK_SCAN_RE.test(token.content)) { |
|
|
|
|
|
|
|
// Init linkifier in lazy manner, only if required.
|
|
|
|
if (!linkifier) { |
|
|
|
linkifier = createLinkifier(); |
|
|
|
links = linkifier.links; |
|
|
|
autolinker = linkifier.autolinker; |
|
|
|
} |
|
|
|
|
|
|
|
text = token.content; |
|
|
|
links.length = 0; |
|
|
|
autolinker.link(text); |
|
|
|