Reintroduce linkify

This commit is contained in:
Chi Vinh Le
2017-09-12 21:31:59 +07:00
parent ee3cee2a50
commit 98b95cab86
7 changed files with 36 additions and 18 deletions
@@ -1,5 +1,5 @@
import React from 'react';
import {linkRegexp} from '../utils/regexp';
import {matchLinks} from '../utils';
const wordSeperator = /([.\s'"?!])/;
@@ -25,21 +25,18 @@ function markWords(body, words, keyPrefix) {
// markLinks looks for links inside `body` and highlights them by returning
// an array of React Elements.
function markLinks(body) {
const tokens = body.split(linkRegexp);
const matches = matchLinks(body);
const content = [];
let tmp = [];
tokens
.filter((token) => token)
.forEach((token, i) => {
if (token.match(linkRegexp)) {
content.push(...tmp);
tmp = [];
content.push(<mark key={i}>{token}</mark>);
return;
}
tmp.push(token);
});
content.push(...tmp);
let index = 0;
if (matches) {
matches
.forEach((match, i) => {
content.push(body.substring(index, match.index));
content.push(<mark key={i}>{match.text}</mark>);
index = match.lastIndex;
});
}
content.push(body.substring(index));
return content;
}
@@ -1,8 +1,8 @@
import React from 'react';
import {linkRegexp} from '../utils/regexp';
import {matchLinks} from '../utils';
export default ({text, children}) => {
const hasLinks = text.match(linkRegexp);
const hasLinks = !!matchLinks(text);
if (!hasLinks) {
return null;
@@ -0,0 +1,8 @@
import LinkifyIt from 'linkify-it';
import tlds from 'tlds';
export function createLinkify() {
const linkify = new LinkifyIt();
linkify.tlds(tlds);
return linkify;
}
+9
View File
@@ -1,3 +1,12 @@
import LinkifyIt from 'linkify-it';
import tlds from 'tlds';
const linkify = new LinkifyIt();
linkify.tlds(tlds);
export function matchLinks(text) {
return linkify.match(text);
}
export const isPremod = (mod) => mod === 'PRE';
export const getModPath = (type = 'all', assetId) =>
-1
View File
@@ -1 +0,0 @@
export const linkRegexp = /([-a-zA-Z0-9@:%_+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_+.~#?&//=]*)?)/;