Switch back to upstream autolinker

This commit is contained in:
Ken-Håvard Lieng 2017-04-16 06:08:45 +02:00
parent e294e109f8
commit 305c1fa08f
4 changed files with 59 additions and 38 deletions

View file

@ -3,17 +3,38 @@ import React from 'react';
const autolinker = new Autolinker({
stripPrefix: false,
doJoin: false,
replaceFn: (linker, match) => {
if (match.getType() === 'url') {
return <a target="_blank" rel="noopener noreferrer" href={match.getAnchorHref()}>{match.getAnchorText()}</a>;
}
return null;
},
React
stripTrailingSlash: false
});
export default function linkify(text) {
return autolinker.link(text);
const matches = autolinker.parseText(text);
const result = [];
let pos = 0;
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (match.offset > pos) {
result.push(text.slice(pos, match.offset));
pos = match.offset;
}
if (match.getType() === 'url') {
result.push(
<a target="_blank" rel="noopener noreferrer" href={match.getAnchorHref()}>
{match.matchedText}
</a>
);
} else {
result.push(match.matchedText);
}
pos += match.matchedText.length;
}
if (pos < text.length) {
result.push(text.slice(pos));
}
return result;
}