Allow parentheses in certain links

Only links with either matching, or open LEFT parentheses are allowed. These are the same rules that github use for this as well.

https://cdn.discordapp.com/attachments/363049725374758921/662487292564340766/unknown.png
This commit is contained in:
Yamboy1 2020-01-03 16:23:11 +13:00 committed by Sean Yesmunt
parent 0345f2d294
commit 4a23ba525f

View file

@ -2,7 +2,7 @@ import { parseURI } from 'lbry-redux';
import visit from 'unist-util-visit';
const protocol = 'lbry://';
const uriRegex = /(lbry:\/\/)[^\s()"]*/g;
const uriRegex = /(lbry:\/\/)[^\s"]*/g;
const mentionToken = '@';
const mentionTokenCode = 64; // @
@ -78,13 +78,32 @@ function tokenizeMention(eat, value, silent) {
return validateURI(match, eat, self);
}
function onlyMatchingParens(string) {
if (!string) return null;
let parens = 0;
let i;
for (i = 0; i < string.length; i++) {
switch (string[i]) {
case '(':
parens++;
break;
case ')':
parens--;
break;
}
if (parens < 0) break;
}
return string.slice(0, i);
}
// Generate a markdown link from lbry url
function tokenizeURI(eat, value, silent) {
if (silent) {
return true;
}
const match = value.match(uriRegex);
const match = onlyMatchingParens(value.match(uriRegex));
return validateURI(match, eat);
}